Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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