您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Input.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, { useCallback, useState } from 'react';
  2. import {
  3. NativeSyntheticEvent,
  4. StyleProp,
  5. Text,
  6. TextInput,
  7. TextInputChangeEventData,
  8. TouchableOpacity,
  9. View,
  10. ViewStyle
  11. } from 'react-native';
  12. import Icon from '../../../icons/components/Icon';
  13. import { IconCloseCircle } from '../../../icons/svg';
  14. import BaseTheme from '../../../ui/components/BaseTheme.native';
  15. import { InputProps } from '../types';
  16. import styles from './inputStyles';
  17. interface IInputProps extends InputProps {
  18. /**
  19. * Custom styles to be applied to the component.
  20. */
  21. customStyles?: CustomStyles;
  22. }
  23. interface CustomStyles {
  24. container?: Object;
  25. input?: Object;
  26. }
  27. const Input = ({
  28. clearable,
  29. customStyles,
  30. disabled,
  31. error,
  32. icon,
  33. label,
  34. onChange,
  35. placeholder,
  36. value
  37. }: IInputProps) => {
  38. const [ focused, setFocused ] = useState(false);
  39. const handleChange = useCallback((e: NativeSyntheticEvent<TextInputChangeEventData>) => {
  40. const { nativeEvent: { text } } = e;
  41. onChange(text);
  42. }, []);
  43. const clearInput = useCallback(() => {
  44. onChange('');
  45. }, []);
  46. const blur = useCallback(() => {
  47. setFocused(false);
  48. }, []);
  49. const focus = useCallback(() => {
  50. setFocused(true);
  51. }, []);
  52. return (<View style = { [ styles.inputContainer, customStyles?.container ] }>
  53. {label && <Text style = { styles.label }>{label}</Text>}
  54. <View style = { styles.fieldContainer as StyleProp<ViewStyle> }>
  55. {icon && <Icon
  56. size = { 22 }
  57. src = { icon }
  58. style = { styles.icon } />}
  59. <TextInput
  60. editable = { !disabled }
  61. onBlur = { blur }
  62. onChange = { handleChange }
  63. onFocus = { focus }
  64. placeholder = { placeholder }
  65. placeholderTextColor = { BaseTheme.palette.text02 }
  66. style = { [ styles.input,
  67. disabled && styles.inputDisabled,
  68. clearable && styles.clearableInput,
  69. icon && styles.iconInput,
  70. focused && styles.inputFocused,
  71. error && styles.inputError,
  72. customStyles?.input
  73. ] }
  74. value = { `${value}` } />
  75. {clearable && !disabled && value !== '' && (
  76. <TouchableOpacity
  77. onPress = { clearInput }
  78. style = { styles.clearButton as StyleProp<ViewStyle> }>
  79. <Icon
  80. size = { 22 }
  81. src = { IconCloseCircle }
  82. style = { styles.clearIcon } />
  83. </TouchableOpacity>
  84. )}
  85. </View>
  86. </View>);
  87. };
  88. export default Input;