You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Input.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, { forwardRef, useCallback, useState } from 'react';
  2. import {
  3. KeyboardTypeOptions,
  4. NativeSyntheticEvent, ReturnKeyTypeOptions,
  5. StyleProp,
  6. Text,
  7. TextInput,
  8. TextInputChangeEventData,
  9. TextInputFocusEventData, TextInputKeyPressEventData,
  10. TextInputSubmitEditingEventData,
  11. TouchableOpacity,
  12. View,
  13. ViewStyle
  14. } from 'react-native';
  15. import Icon from '../../../icons/components/Icon';
  16. import { IconCloseCircle } from '../../../icons/svg';
  17. import BaseTheme from '../../../ui/components/BaseTheme.native';
  18. import { IInputProps } from '../types';
  19. import styles from './inputStyles';
  20. interface IProps extends IInputProps {
  21. accessibilityLabel?: any;
  22. autoCapitalize?: 'none' | 'sentences' | 'words' | 'characters' | undefined;
  23. autoFocus?: boolean;
  24. blurOnSubmit?: boolean | undefined;
  25. customStyles?: ICustomStyles;
  26. editable?: boolean | undefined;
  27. keyboardType?: KeyboardTypeOptions;
  28. maxLength?: number | undefined;
  29. minHeight?: number | string | undefined;
  30. multiline?: boolean | undefined;
  31. numberOfLines?: number | undefined;
  32. onBlur?: ((e: NativeSyntheticEvent<TextInputFocusEventData>) => void) | undefined;
  33. onFocus?: ((e: NativeSyntheticEvent<TextInputFocusEventData>) => void) | undefined;
  34. onKeyPress?: ((e: NativeSyntheticEvent<TextInputKeyPressEventData>) => void) | undefined;
  35. onSubmitEditing?: (value: string) => void;
  36. pointerEvents?: 'box-none' | 'none' | 'box-only' | 'auto' | undefined;
  37. returnKeyType?: ReturnKeyTypeOptions | undefined;
  38. secureTextEntry?: boolean | undefined;
  39. textContentType?: any;
  40. }
  41. interface ICustomStyles {
  42. container?: Object;
  43. input?: Object;
  44. }
  45. const Input = forwardRef<TextInput, IProps>(({
  46. accessibilityLabel,
  47. autoCapitalize,
  48. autoFocus,
  49. blurOnSubmit,
  50. clearable,
  51. customStyles,
  52. disabled,
  53. error,
  54. icon,
  55. keyboardType,
  56. label,
  57. maxLength,
  58. minHeight,
  59. multiline,
  60. numberOfLines,
  61. onBlur,
  62. onChange,
  63. onFocus,
  64. onKeyPress,
  65. onSubmitEditing,
  66. placeholder,
  67. pointerEvents,
  68. returnKeyType,
  69. secureTextEntry,
  70. textContentType,
  71. value
  72. }: IProps, ref) => {
  73. const [ focused, setFocused ] = useState(false);
  74. const handleChange = useCallback((e: NativeSyntheticEvent<TextInputChangeEventData>) => {
  75. const { nativeEvent: { text } } = e;
  76. onChange?.(text);
  77. }, []);
  78. const clearInput = useCallback(() => {
  79. onChange?.('');
  80. }, []);
  81. const handleBlur = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => {
  82. setFocused(false);
  83. onBlur?.(e);
  84. }, []);
  85. const handleFocus = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => {
  86. setFocused(true);
  87. onFocus?.(e);
  88. }, []);
  89. const handleKeyPress = useCallback((e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
  90. onKeyPress?.(e);
  91. }, []);
  92. const handleSubmitEditing = useCallback((e: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => {
  93. const { nativeEvent: { text } } = e;
  94. onSubmitEditing?.(text);
  95. }, []);
  96. return (<View style = { [ styles.inputContainer, customStyles?.container ] }>
  97. {label && <Text style = { styles.label }>{ label }</Text>}
  98. <View style = { styles.fieldContainer as StyleProp<ViewStyle> }>
  99. {icon && <Icon
  100. size = { 22 }
  101. src = { icon }
  102. style = { styles.icon } />}
  103. <TextInput
  104. accessibilityLabel = { accessibilityLabel }
  105. autoCapitalize = { autoCapitalize }
  106. autoComplete = { 'off' }
  107. autoCorrect = { false }
  108. autoFocus = { autoFocus }
  109. blurOnSubmit = { blurOnSubmit }
  110. editable = { !disabled }
  111. keyboardType = { keyboardType }
  112. maxLength = { maxLength }
  113. // @ts-ignore
  114. minHeight = { minHeight }
  115. multiline = { multiline }
  116. numberOfLines = { numberOfLines }
  117. onBlur = { handleBlur }
  118. onChange = { handleChange }
  119. onFocus = { handleFocus }
  120. onKeyPress = { handleKeyPress }
  121. onSubmitEditing = { handleSubmitEditing }
  122. placeholder = { placeholder }
  123. placeholderTextColor = { BaseTheme.palette.text02 }
  124. pointerEvents = { pointerEvents }
  125. ref = { ref }
  126. returnKeyType = { returnKeyType }
  127. secureTextEntry = { secureTextEntry }
  128. spellCheck = { false }
  129. style = { [
  130. styles.input,
  131. clearable && styles.clearableInput,
  132. customStyles?.input,
  133. disabled && styles.inputDisabled,
  134. error && styles.inputError,
  135. focused && styles.inputFocused,
  136. icon && styles.iconInput,
  137. multiline && styles.inputMultiline
  138. ] }
  139. textContentType = { textContentType }
  140. value = { typeof value === 'number' ? `${value}` : value } />
  141. { clearable && !disabled && value !== '' && (
  142. <TouchableOpacity
  143. onPress = { clearInput }
  144. style = { styles.clearButton as StyleProp<ViewStyle> }>
  145. <Icon
  146. size = { 22 }
  147. src = { IconCloseCircle }
  148. style = { styles.clearIcon } />
  149. </TouchableOpacity>
  150. )}
  151. </View>
  152. </View>);
  153. });
  154. export default Input;