-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
87 lines (82 loc) · 3.78 KB
/
index.tsx
File metadata and controls
87 lines (82 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Copyright (c) 2022 Skyflow, Inc.
*/
import React, { useEffect, useRef } from "react";
import { Text, TextInput, View } from "react-native";
import type CollectElement from "../../core/CollectElement";
import { CollectElementProps, ElementType, ELEMENT_REQUIRED_ASTERISK, REQUIRED_MARK_DEFAULT_STYLE, ContainerType } from "../../utils/constants";
import SkyflowError from "../../utils/skyflow-error";
import SKYFLOW_ERROR_CODE from "../../utils/skyflow-error-code";
import uuid from 'react-native-uuid';
/**
* Element to collect arbitrary values.
*/
const InputFieldElement: React.FC<CollectElementProps> = ({ container, options = { required: false }, ...rest }) => {
const [element, setElement] = React.useState<CollectElement>(undefined);
const [elementValue, setElementValue] = React.useState<string>('');
const [errorText, setErrorText] = React.useState<string>('');
const [labelStyles, setLabelStyles] = React.useState(rest?.labelStyles?.base || {});
const [inputStyles, setInputStyles] = React.useState(rest?.inputStyles?.base || {});
const textInputRef = useRef();
const uniqueElementID = useRef(uuid.v4() as string);
useEffect(() => {
if (container) {
const element: CollectElement = container.create({ ...rest, type: ElementType.INPUT_FIELD, containerType: container.type }, options);
setElement(element);
if (container.type === ContainerType.COLLECT) {
element.setMethods(setErrorText, { setInputStyles: setInputStyles, setLabelStyles: setLabelStyles });
}
else if (container.type === ContainerType.COMPOSABLE) {
element.setMethods(rest.containerMethods.setErrorText, { setInputStyles: setInputStyles, setLabelStyles: setLabelStyles })
rest.containerMethods.setRef(textInputRef, uniqueElementID.current);
}
if (rest.onReady) {
rest.onReady(element.getClientState());
}
} else {
throw new SkyflowError(SKYFLOW_ERROR_CODE.CONTAINER_OBJECT_IS_REQUIRED, [ElementType.INPUT_FIELD, 'useCollectContainer()'], true)
}
}, []);
return (<View>
{
rest.label && (<Text style={labelStyles}>
{rest.label}
<Text style={{ ...REQUIRED_MARK_DEFAULT_STYLE, ...rest?.labelStyles?.requiredAsterisk }}>
{options.required ? ELEMENT_REQUIRED_ASTERISK : ''}
</Text>
</Text>)
}
<TextInput
ref={textInputRef}
value={elementValue}
placeholder={rest.placeholder}
onChangeText={(text) => {
element?.onChangeElement(text);
setElementValue(element.getInternalState().value)
}}
onFocus={() => {
element?.onFocusElement();
setLabelStyles(element.updateLabelStyles());
setInputStyles(element.updateInputStyles());
}}
onBlur={() => {
element?.onBlurElement();
if (container.type === ContainerType.COLLECT) {
setErrorText(element?.getErrorText() || '');
} else if (container.type === ContainerType.COMPOSABLE) {
rest.containerMethods.setErrorText(element?.getErrorText() || '')
}
setElementValue(element.getInternalState().value);
setLabelStyles(element.updateLabelStyles());
setInputStyles(element.updateInputStyles());
}}
style={inputStyles}
/>
{
container && container?.type === ContainerType.COLLECT
&&
<Text style={rest?.errorTextStyles?.base || {}}>{errorText}</Text>
}
</View>);
}
export default InputFieldElement;