import React, { forwardRef, useCallback, useState } from 'react'; import { KeyboardTypeOptions, NativeSyntheticEvent, ReturnKeyTypeOptions, StyleProp, Text, TextInput, TextInputChangeEventData, TextInputFocusEventData, TextInputKeyPressEventData, TextInputSubmitEditingEventData, TextStyle, TouchableOpacity, View, ViewStyle } from 'react-native'; import Icon from '../../../icons/components/Icon'; import { IconCloseCircle } from '../../../icons/svg'; import BaseTheme from '../../../ui/components/BaseTheme.native'; import { IInputProps } from '../types'; import styles from './inputStyles'; interface IProps extends IInputProps { accessibilityLabel?: any; autoCapitalize?: 'none' | 'sentences' | 'words' | 'characters' | undefined; autoFocus?: boolean; blurOnSubmit?: boolean | undefined; bottomLabel?: string; customStyles?: ICustomStyles; editable?: boolean | undefined; /** * The id to set on the input element. * This is required because we need it internally to tie the input to its * info (label, error) so that screen reader users don't get lost. */ id?: string; keyboardType?: KeyboardTypeOptions; maxLength?: number | undefined; minHeight?: number | string | undefined; multiline?: boolean | undefined; numberOfLines?: number | undefined; onBlur?: ((e: NativeSyntheticEvent) => void) | undefined; onFocus?: ((e: NativeSyntheticEvent) => void) | undefined; onKeyPress?: ((e: NativeSyntheticEvent) => void) | undefined; onSubmitEditing?: (value: string) => void; pointerEvents?: 'box-none' | 'none' | 'box-only' | 'auto' | undefined; returnKeyType?: ReturnKeyTypeOptions | undefined; secureTextEntry?: boolean | undefined; textContentType?: any; } interface ICustomStyles { container?: Object; input?: Object; } const Input = forwardRef(({ accessibilityLabel, autoCapitalize, autoFocus, blurOnSubmit, bottomLabel, clearable, customStyles, disabled, error, icon, id, keyboardType, label, maxLength, minHeight, multiline, numberOfLines, onBlur, onChange, onFocus, onKeyPress, onSubmitEditing, placeholder, pointerEvents, returnKeyType, secureTextEntry, textContentType, value }: IProps, ref) => { const [ focused, setFocused ] = useState(false); const handleChange = useCallback((e: NativeSyntheticEvent) => { const { nativeEvent: { text } } = e; onChange?.(text); }, [ onChange ]); const clearInput = useCallback(() => { onChange?.(''); }, [ onChange ]); const handleBlur = useCallback((e: NativeSyntheticEvent) => { setFocused(false); onBlur?.(e); }, [ onBlur ]); const handleFocus = useCallback((e: NativeSyntheticEvent) => { setFocused(true); onFocus?.(e); }, [ onFocus ]); const handleKeyPress = useCallback((e: NativeSyntheticEvent) => { onKeyPress?.(e); }, [ onKeyPress ]); const handleSubmitEditing = useCallback((e: NativeSyntheticEvent) => { const { nativeEvent: { text } } = e; onSubmitEditing?.(text); }, [ onSubmitEditing ]); return ( }> {label && { label }} }> {icon && } } textContentType = { textContentType } value = { typeof value === 'number' ? `${value}` : value } /> { clearable && !disabled && value !== '' && ( }> )} { bottomLabel && ( { bottomLabel } ) } ); }); export default Input;