/
input.tsx
333 lines (306 loc) · 9.05 KB
/
input.tsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import React, { useCallback, useMemo, useRef, useState } from "react";
import Box, { BoxProps } from "../../atoms/box/box";
import { buildFontConfig } from "../../atoms/text/text";
import { useMolecularComponentConfig } from "../../../hooks/useMolecularComponentConfig";
import { useAtomicComponentConfig } from "../../../hooks/useAtomicComponentConfig";
import Icon from "../../atoms/icon/icon";
import {
NativeSyntheticEvent,
Platform,
TextInput,
TextInputChangeEventData,
TextInputFocusEventData,
TextInputProps,
} from "react-native";
import {
StyleFunctionContainer,
ResponsiveValue,
PaletteColors,
MoleculeComponentProps,
StateProps,
} from "../../../theme/src/types";
import {
boxStyleFunctions,
colorStyleFunction,
createStyleFunction,
transformColorValue,
typographyStyleFunction,
} from "../../../theme/src/style-functions";
import { useStyleProps } from "../../../hooks/useStyleProps";
import Pressable from "../../atoms/pressable/pressable";
import { useFocusedState } from "../../../hooks/state/useFocusedState";
import { useDisabledState } from "../../../hooks/state/useDisabledState";
import { useInvalidState } from "../../../hooks/state/useInvalidState";
import { HStack } from "../../atoms/stack/stack";
import { useTheme } from "../../../hooks";
import { InputAtoms } from "./input.config";
import _ from "lodash";
export type BaseInputProps = Omit<
TextInputProps,
"placeholderTextColor" | "selectionColor"
> &
BoxProps &
StateProps<"_focused" | "_disabled" | "_invalid"> & {
/**
* Whether the input field is disabled.
*
* @default false
*/
isDisabled?: boolean;
/**
* Whether the input field should span the entire width of the parent container.
*
* @default false
*/
isFullWidth?: boolean;
/**
* Whether there the input field is in an invalid state.
*
* @default false
*/
isInvalid?: boolean;
/**
* Whether the input field should display a clear button.
*
* @default false
*/
hasClearButton?: boolean;
/** Icon to display on the left side of the text input */
leftIcon?: React.ReactElement;
/** Icon to display on the right side of the text input */
rightIcon?: React.ReactElement;
/**
* Custom color of the placeholder text string.
*
* @default "neutral.500"
*/
placeholderTextColor?: ResponsiveValue<PaletteColors>;
/**
* Custom color of the highlight and cursor color.
*
* @default "primary.500"
*/
selectionColor?: ResponsiveValue<PaletteColors>;
};
// Additional style Functions used for this component
const placeholderTextColorStyleFunction = createStyleFunction({
property: "placeholderTextColor",
styleProperty: "placeholderTextColor",
themeKey: "palette",
transform: transformColorValue,
});
const selectionColorStyleFunction = createStyleFunction({
property: "selectionColor",
styleProperty: "selectionColor",
themeKey: "palette",
transform: transformColorValue,
});
const inputRootStyleFunction = [
...boxStyleFunctions,
placeholderTextColorStyleFunction,
selectionColorStyleFunction,
];
const inputTextStyleFunction = [
colorStyleFunction,
typographyStyleFunction,
] as StyleFunctionContainer[];
/** The Input component is a component that is used to get user input in a text field. **/
const Input = React.memo(
React.forwardRef(
(
{
children,
onFocus = undefined,
onBlur = undefined,
size = "m",
isDisabled = false,
isFullWidth = false,
hasClearButton = false,
isInvalid = false,
leftIcon = undefined,
rightIcon = undefined,
colorScheme = "primary",
onChangeText = () => {},
onChange = () => {},
...rest
}: Omit<
MoleculeComponentProps<"Input", BaseInputProps, InputAtoms>,
"atoms"
> & {
atoms?: Partial<InputAtoms>;
},
textInputRef: any
) => {
const { theme } = useTheme();
const [isCleared, setIsCleared] = useState(
rest.value && rest.value.length > 0 ? false : true
);
let molecularProps = useMolecularComponentConfig<InputAtoms>(
"Input",
rest,
{
size: size,
variant: rest["variant"],
},
colorScheme,
boxStyleFunctions,
"container",
"input",
"container"
);
const { atoms } = molecularProps;
const inputProps = useStyleProps(atoms.input, inputRootStyleFunction);
const { placeholderTextColor, selectionColor, ...finalInputStyle } =
inputProps.style;
const textProps = useAtomicComponentConfig(
"Text",
atoms.text,
{
size: size,
variant: atoms.text.variant,
},
"primary",
inputTextStyleFunction
);
const memoizedBuildFontConfig = useCallback(
() =>
buildFontConfig(
textProps.style,
atoms.input.allowFontScaling || true,
theme
),
[textProps.style, atoms.input.allowFontScaling]
);
const finalTextStyles = useMemo(
() => ({
...textProps.style,
...memoizedBuildFontConfig(),
}),
[textProps.style, memoizedBuildFontConfig]
);
// Use State for dynamic styles
const { focused, setFocused, propsWithFocusedStyles } = useFocusedState(
atoms.container,
boxStyleFunctions,
"molecule"
);
atoms.container = propsWithFocusedStyles;
const { propsWithDisabledStyles } = useDisabledState(
atoms.container,
boxStyleFunctions,
"molecule",
true,
isDisabled
);
atoms.container = propsWithDisabledStyles;
const { propsWithInvalidStyles } = useInvalidState(
atoms.container,
boxStyleFunctions,
"molecule",
true,
isInvalid
);
atoms.container = propsWithInvalidStyles;
// METHODS
const clearInputHandler = () => {
(textInputRef.current as any).clear();
setIsCleared(true);
onChangeText("");
};
const onChangeHandler = (
event: NativeSyntheticEvent<TextInputChangeEventData>
) => {
const { text } = event.nativeEvent;
if (text.length > 0) {
setIsCleared(false);
}
onChange(event);
};
const onChangeTextHandler = (value: string) => {
if (value.length > 0) {
setIsCleared(false);
}
onChangeText(value);
};
const focusInputHandler = (
event: NativeSyntheticEvent<TextInputFocusEventData>
) => {
setFocused(true);
if (onFocus) {
onFocus(event);
}
};
const blurInputHandler = (
event: NativeSyntheticEvent<TextInputFocusEventData>
) => {
setFocused(false);
if (onBlur) {
onBlur(event);
}
};
// Render Functions
const renderLeftIcon = useCallback(() => {
if (leftIcon) {
return React.cloneElement(leftIcon, {
...atoms.icon,
...leftIcon.props,
});
}
}, [leftIcon, atoms.icon]);
const renderRightIcon = useCallback(() => {
if (rightIcon) {
return React.cloneElement(rightIcon, {
...atoms.icon,
...rightIcon.props,
});
}
}, [rightIcon, atoms.icon]);
const renderClearIcon = useCallback(() => {
if (hasClearButton && !isCleared) {
return (
<Pressable testID="clear-icon" onPress={clearInputHandler}>
<Icon
{...atoms.icon}
iconFamily="Ionicons"
iconName="close"
marginLeft="2"
/>
</Pressable>
);
}
}, [hasClearButton, isCleared, clearInputHandler, atoms.icon]);
return (
<Box {...atoms.container} w={isFullWidth ? "100%" : undefined}>
{renderLeftIcon()}
<TextInput
{...inputProps}
ref={textInputRef}
editable={!isDisabled}
onFocus={focusInputHandler}
onBlur={blurInputHandler}
onChange={onChangeHandler}
onChangeText={onChangeTextHandler}
allowFontScaling={true}
placeholderTextColor={placeholderTextColor ?? "#a7a7a7"}
selectionColor={selectionColor ?? null}
accessibilityLabel={rest.accessibilityLabel ?? rest.placeholder}
accessibilityState={{ disabled: isDisabled, selected: focused }}
style={[
finalInputStyle,
finalTextStyles,
Platform.OS === "web" ? { outlineStyle: "none" } : {},
{ flex: 1 },
]}
/>
<HStack spacing="0" alignSelf="center" alignItems="center">
{renderRightIcon()}
{renderClearIcon()}
</HStack>
</Box>
);
}
)
);
export type InputProps = React.ComponentProps<typeof Input>;
Input.displayName = "Input";
export default Input;