2022-11-08 15:46:46 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
|
|
|
|
|
|
|
import React, { forwardRef, useCallback, useState } from 'react';
|
2022-07-26 10:58:28 +00:00
|
|
|
import {
|
2022-11-08 15:46:46 +00:00
|
|
|
KeyboardTypeOptions,
|
|
|
|
NativeSyntheticEvent, ReturnKeyTypeOptions,
|
2022-07-26 10:58:28 +00:00
|
|
|
StyleProp,
|
|
|
|
Text,
|
|
|
|
TextInput,
|
|
|
|
TextInputChangeEventData,
|
2022-11-08 15:46:46 +00:00
|
|
|
TextInputFocusEventData, TextInputKeyPressEventData,
|
|
|
|
TextInputSubmitEditingEventData,
|
2022-07-26 10:58:28 +00:00
|
|
|
TouchableOpacity,
|
|
|
|
View,
|
|
|
|
ViewStyle
|
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
import Icon from '../../../icons/components/Icon';
|
|
|
|
import { IconCloseCircle } from '../../../icons/svg';
|
|
|
|
import BaseTheme from '../../../ui/components/BaseTheme.native';
|
2022-10-20 09:11:27 +00:00
|
|
|
import { IInputProps } from '../types';
|
2022-07-26 10:58:28 +00:00
|
|
|
|
|
|
|
import styles from './inputStyles';
|
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
interface IProps extends IInputProps {
|
2022-11-08 15:46:46 +00:00
|
|
|
accessibilityLabel?: any;
|
|
|
|
autoCapitalize?: string | undefined;
|
|
|
|
autoFocus?: boolean;
|
|
|
|
blurOnSubmit?: boolean | undefined;
|
2022-10-20 09:11:27 +00:00
|
|
|
customStyles?: ICustomStyles;
|
2022-11-08 15:46:46 +00:00
|
|
|
editable?: boolean | undefined;
|
|
|
|
keyboardType?: KeyboardTypeOptions;
|
|
|
|
maxLength?: number | undefined;
|
|
|
|
minHeight?: number | string | undefined;
|
|
|
|
multiline?: boolean | undefined;
|
|
|
|
numberOfLines?: number | undefined;
|
|
|
|
onBlur?: ((e: NativeSyntheticEvent<TextInputFocusEventData>) => void) | undefined;
|
|
|
|
onFocus?: ((e: NativeSyntheticEvent<TextInputFocusEventData>) => void) | undefined;
|
|
|
|
onKeyPress?: ((e: NativeSyntheticEvent<TextInputKeyPressEventData>) => void) | undefined;
|
|
|
|
onSubmitEditing?: (value: string) => void;
|
|
|
|
returnKeyType?: ReturnKeyTypeOptions | undefined;
|
|
|
|
secureTextEntry?: boolean | undefined;
|
|
|
|
textContentType?: any;
|
2022-07-26 10:58:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
interface ICustomStyles {
|
2022-07-26 10:58:28 +00:00
|
|
|
container?: Object;
|
|
|
|
input?: Object;
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:46:46 +00:00
|
|
|
const Input = forwardRef<TextInput, IInputProps>(({
|
|
|
|
accessibilityLabel,
|
|
|
|
autoCapitalize,
|
|
|
|
autoFocus,
|
|
|
|
blurOnSubmit,
|
2022-07-26 10:58:28 +00:00
|
|
|
clearable,
|
|
|
|
customStyles,
|
|
|
|
disabled,
|
|
|
|
error,
|
|
|
|
icon,
|
2022-11-08 15:46:46 +00:00
|
|
|
keyboardType,
|
2022-07-26 10:58:28 +00:00
|
|
|
label,
|
2022-11-08 15:46:46 +00:00
|
|
|
maxLength,
|
|
|
|
minHeight,
|
|
|
|
multiline,
|
|
|
|
numberOfLines,
|
|
|
|
onBlur,
|
2022-07-26 10:58:28 +00:00
|
|
|
onChange,
|
2022-11-08 15:46:46 +00:00
|
|
|
onFocus,
|
|
|
|
onKeyPress,
|
|
|
|
onSubmitEditing,
|
2022-07-26 10:58:28 +00:00
|
|
|
placeholder,
|
2022-11-08 15:46:46 +00:00
|
|
|
returnKeyType,
|
|
|
|
secureTextEntry,
|
|
|
|
textContentType,
|
2022-07-26 10:58:28 +00:00
|
|
|
value
|
2022-11-08 15:46:46 +00:00
|
|
|
}: IProps, ref) => {
|
2022-07-26 10:58:28 +00:00
|
|
|
const [ focused, setFocused ] = useState(false);
|
|
|
|
const handleChange = useCallback((e: NativeSyntheticEvent<TextInputChangeEventData>) => {
|
|
|
|
const { nativeEvent: { text } } = e;
|
|
|
|
|
2022-10-27 08:36:50 +00:00
|
|
|
onChange?.(text);
|
2022-07-26 10:58:28 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const clearInput = useCallback(() => {
|
2022-10-27 08:36:50 +00:00
|
|
|
onChange?.('');
|
2022-07-26 10:58:28 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-11-08 15:46:46 +00:00
|
|
|
const handleBlur = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => {
|
2022-07-26 10:58:28 +00:00
|
|
|
setFocused(false);
|
2022-11-08 15:46:46 +00:00
|
|
|
onBlur?.(e);
|
2022-07-26 10:58:28 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-11-08 15:46:46 +00:00
|
|
|
const handleFocus = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => {
|
2022-07-26 10:58:28 +00:00
|
|
|
setFocused(true);
|
2022-11-08 15:46:46 +00:00
|
|
|
onFocus?.(e);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleKeyPress = useCallback((e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
|
|
|
|
onKeyPress?.(e);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleSubmitEditing = useCallback((e: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => {
|
|
|
|
const { nativeEvent: { text } } = e;
|
|
|
|
|
|
|
|
onSubmitEditing?.(text);
|
2022-07-26 10:58:28 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (<View style = { [ styles.inputContainer, customStyles?.container ] }>
|
2022-11-08 15:46:46 +00:00
|
|
|
{label && <Text style = { styles.label }>{ label }</Text>}
|
2022-07-26 10:58:28 +00:00
|
|
|
<View style = { styles.fieldContainer as StyleProp<ViewStyle> }>
|
|
|
|
{icon && <Icon
|
|
|
|
size = { 22 }
|
|
|
|
src = { icon }
|
|
|
|
style = { styles.icon } />}
|
|
|
|
<TextInput
|
2022-11-08 15:46:46 +00:00
|
|
|
accessibilityLabel = { accessibilityLabel }
|
|
|
|
// @ts-ignore
|
|
|
|
autoCapitalize = { autoCapitalize }
|
|
|
|
autoComplete = { 'off' }
|
|
|
|
autoCorrect = { false }
|
|
|
|
autoFocus = { autoFocus }
|
|
|
|
blurOnSubmit = { blurOnSubmit }
|
2022-07-26 10:58:28 +00:00
|
|
|
editable = { !disabled }
|
2022-11-08 15:46:46 +00:00
|
|
|
keyboardType = { keyboardType }
|
|
|
|
maxLength = { maxLength }
|
|
|
|
minHeight = { minHeight }
|
|
|
|
multiline = { multiline }
|
|
|
|
numberOfLines = { numberOfLines }
|
|
|
|
onBlur = { handleBlur }
|
2022-07-26 10:58:28 +00:00
|
|
|
onChange = { handleChange }
|
2022-11-08 15:46:46 +00:00
|
|
|
onFocus = { handleFocus }
|
|
|
|
onKeyPress = { handleKeyPress }
|
|
|
|
onSubmitEditing = { handleSubmitEditing }
|
2022-07-26 10:58:28 +00:00
|
|
|
placeholder = { placeholder }
|
|
|
|
placeholderTextColor = { BaseTheme.palette.text02 }
|
2022-11-08 15:46:46 +00:00
|
|
|
ref = { ref }
|
|
|
|
returnKeyType = { returnKeyType }
|
|
|
|
secureTextEntry = { secureTextEntry }
|
|
|
|
spellCheck = { false }
|
|
|
|
style = { [
|
|
|
|
styles.input,
|
2022-07-26 10:58:28 +00:00
|
|
|
clearable && styles.clearableInput,
|
2022-11-08 15:46:46 +00:00
|
|
|
customStyles?.input,
|
|
|
|
disabled && styles.inputDisabled,
|
2022-07-26 10:58:28 +00:00
|
|
|
error && styles.inputError,
|
2022-11-08 15:46:46 +00:00
|
|
|
focused && styles.inputFocused,
|
|
|
|
icon && styles.iconInput,
|
|
|
|
multiline && styles.inputMultiline
|
2022-07-26 10:58:28 +00:00
|
|
|
] }
|
2022-11-08 15:46:46 +00:00
|
|
|
textContentType = { textContentType }
|
|
|
|
value = { typeof value === 'number' ? `${value}` : value } />
|
|
|
|
{ clearable && !disabled && value !== '' && (
|
2022-07-26 10:58:28 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress = { clearInput }
|
|
|
|
style = { styles.clearButton as StyleProp<ViewStyle> }>
|
|
|
|
<Icon
|
|
|
|
size = { 22 }
|
|
|
|
src = { IconCloseCircle }
|
|
|
|
style = { styles.clearIcon } />
|
|
|
|
</TouchableOpacity>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
</View>);
|
2022-11-08 15:46:46 +00:00
|
|
|
});
|
2022-07-26 10:58:28 +00:00
|
|
|
|
|
|
|
export default Input;
|