-
Notifications
You must be signed in to change notification settings - Fork 957
/
Copy pathinput.component.tsx
335 lines (312 loc) · 9.09 KB
/
input.component.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
334
335
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import React from 'react';
import {
ImageProps,
NativeSyntheticEvent,
Platform,
StyleProp,
StyleSheet,
TextInput,
TextInputFocusEventData,
TextInputProps,
TextStyle,
View,
ViewProps,
ViewStyle,
} from 'react-native';
import {
EvaSize,
EvaStatus,
FalsyFC,
FalsyText,
FlexViewCrossStyleProps,
PropsService,
RenderProp,
WebEventResponder,
WebEventResponderCallbacks,
WebEventResponderInstance,
Overwrite,
LiteralUnion,
TouchableWithoutFeedback,
} from '../../devsupport';
import {
Interaction,
styled,
StyledComponentProps,
StyleType,
} from '../../theme';
import { TextProps } from '../text/text.component';
type InputStyledProps = Overwrite<StyledComponentProps, {
appearance?: LiteralUnion<'default'>;
}>;
export interface InputProps extends TextInputProps, InputStyledProps {
status?: EvaStatus;
size?: EvaSize;
disabled?: boolean;
label?: RenderProp<TextProps> | React.ReactText;
caption?: RenderProp<TextProps> | React.ReactText;
accessoryLeft?: RenderProp<Partial<ImageProps>>;
accessoryRight?: RenderProp<Partial<ImageProps>>;
textStyle?: StyleProp<TextStyle>;
}
export type InputElement = React.ReactElement<InputProps>;
/**
* Inputs let users enter and edit text.
*
* @extends React.Component
*
* @property {string} value - A value displayed in input field.
*
* @property {(string) => void} onChangeText - Called when the value should be changed.
*
* @property {() => void} onFocus - Called when input field becomes focused.
*
* @property {() => void} onBlur - Called when input field looses focus.
*
* @property {string} placeholder - A string to be displayed when there is no value.
*
* @property {boolean} disabled - Whether input field is disabled.
* This property overrides `editable` property of TextInput.
*
* @property {ReactElement | ReactText | (TextProps) => ReactElement} label - String, number or a function component
* to render above the input field.
* If it is a function, expected to return a Text.
*
* @property {ReactElement | ReactText | (TextProps) => ReactElement} caption - Function component to render below
* Input view.
* Expected to return View.
*
* @property {ReactElement | (ImageProps) => ReactElement} accessoryLeft - Function component
* to render to start of the text.
* Expected to return an Image.
*
* @property {ReactElement | (ImageProps) => ReactElement} accessoryRight - Function component
* to render to end of the text.
* Expected to return an Image.
*
* @property {string} status - Status of the component.
* Can be `basic`, `primary`, `success`, `info`, `warning`, `danger` or `control`.
* Defaults to *basic*.
* Useful for giving user a hint on the input validity.
* Use *control* status when needed to display within a contrast container.
*
* @property {string} size - Size of the component.
* Can be `small`, `medium` or `large`.
* Defaults to *medium*.
*
* @property {StyleProp<TextStyle>} textStyle - Customizes the style of the text field.
*
* @property {TextInputProps} ...TextInputProps - Any props applied to TextInput component.
*
* @overview-example InputSimpleUsage
*
* @overview-example InputStates
* Input can be disabled with `disabled` property.
*
* @overview-example InputStatus
* Or marked with `status` property, which is useful within forms validation.
* An extra status is `control`, which is designed to be used on high-contrast backgrounds.
*
* @overview-example InputAccessories
* Input may contain labels, captions and inner views by configuring `accessoryLeft` or `accessoryRight` properties.
* Within Eva, Input accessories are expected to be images or [svg icons](guides/icon-packages).
*
* @overview-example InputSize
* To resize Input, a `size` property may be used.
*
* @overview-example InputStyling
* Input and it's inner views can be styled by passing them as function components.
* ```
* import { Input, Text } from '@ui-kitten/components';
*
* <Input
* textStyle={{ ... }}
* label={evaProps => <Text {...evaProps}>Label</Text>}
* caption={evaProps => <Text {...evaProps}>Caption</Text>}
* />
* ```
*
* @overview-example InputTheming
* In most cases this is redundant, if [custom theme is configured](guides/branding).
*/
@styled('Input')
export class Input extends React.Component<InputProps> implements WebEventResponderCallbacks {
private textInputRef = React.createRef<TextInput>();
private webEventResponder: WebEventResponderInstance = WebEventResponder.create(this);
public focus = (): void => {
this.textInputRef.current?.focus();
};
public blur = (): void => {
this.textInputRef.current?.blur();
};
public isFocused = (): boolean => {
return this.textInputRef.current?.isFocused();
};
public clear = (): void => {
this.textInputRef.current?.clear();
};
// WebEventResponderCallbacks
public onMouseEnter = (): void => {
this.props.eva.dispatch([Interaction.HOVER]);
};
public onMouseLeave = (): void => {
this.props.eva.dispatch([]);
};
private onTextFieldFocus = (event: NativeSyntheticEvent<TextInputFocusEventData>): void => {
this.props.eva.dispatch([Interaction.FOCUSED]);
this.props.onFocus?.(event);
};
private onTextFieldBlur = (event: NativeSyntheticEvent<TextInputFocusEventData>): void => {
this.props.eva.dispatch([]);
this.props.onBlur?.(event);
};
private getComponentStyle = (source: StyleType): StyleType => {
const flatStyles: ViewStyle = StyleSheet.flatten(this.props.style);
const { rest: inputContainerStyle, ...containerStyle } =
PropsService.allWithRest(flatStyles, FlexViewCrossStyleProps);
const {
textMarginHorizontal,
textFontFamily,
textFontSize,
textFontWeight,
textColor,
placeholderColor,
iconWidth,
iconHeight,
iconMarginHorizontal,
iconTintColor,
labelColor,
labelFontSize,
labelMarginBottom,
labelFontWeight,
labelFontFamily,
captionMarginTop,
captionColor,
captionFontSize,
captionFontWeight,
captionFontFamily,
...containerParameters
} = source;
return {
container: containerStyle,
inputContainer: {
...containerParameters,
...inputContainerStyle,
},
text: {
marginHorizontal: textMarginHorizontal,
fontFamily: textFontFamily,
fontSize: textFontSize,
fontWeight: textFontWeight,
color: textColor,
},
placeholder: {
color: placeholderColor,
},
icon: {
width: iconWidth,
height: iconHeight,
marginHorizontal: iconMarginHorizontal,
tintColor: iconTintColor,
},
label: {
color: labelColor,
fontSize: labelFontSize,
marginBottom: labelMarginBottom,
fontWeight: labelFontWeight,
fontFamily: labelFontFamily,
},
captionLabel: {
fontSize: captionFontSize,
fontWeight: captionFontWeight,
fontFamily: captionFontFamily,
color: captionColor,
},
};
};
public render(): React.ReactElement<ViewProps> {
const {
eva,
textStyle,
label,
caption,
accessoryLeft,
accessoryRight,
testID,
...textInputProps
} = this.props;
const evaStyle = this.getComponentStyle(eva.style);
return (
<TouchableWithoutFeedback
testID={`@${testID}/container`}
style={evaStyle.container}
focusable={false}
onPress={this.focus}
>
<FalsyText
style={[evaStyle.label, styles.label]}
component={label}
/>
<View style={[evaStyle.inputContainer, styles.inputContainer]}>
<FalsyFC
style={evaStyle.icon}
component={accessoryLeft}
/>
<TextInput
ref={this.textInputRef}
placeholderTextColor={evaStyle.placeholder.color}
{...textInputProps}
{...this.webEventResponder.eventHandlers}
testID={`@${testID}/input`}
style={[evaStyle.text, styles.text, platformStyles.text, textStyle]}
editable={!textInputProps.disabled}
onFocus={this.onTextFieldFocus}
onBlur={this.onTextFieldBlur}
/>
<FalsyFC
style={evaStyle.icon}
component={accessoryRight}
/>
</View>
<FalsyText
style={[evaStyle.captionLabel, styles.captionLabel]}
component={caption}
/>
</TouchableWithoutFeedback>
);
}
}
const styles = StyleSheet.create({
inputContainer: {
flexDirection: 'row',
alignItems: 'center',
width: '100%',
},
text: {
flexGrow: 1,
flexShrink: 1,
flexBasis: 'auto',
},
label: {
textAlign: 'left',
},
captionLabel: {
textAlign: 'left',
},
});
const platformStyles = StyleSheet.create({
text: Platform.select({
default: null,
android: {
paddingVertical: 0,
marginVertical: -2,
},
web: {
outlineWidth: 0,
},
}),
});