index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { FC, ReactNode, useState } from 'react';
  2. import {
  3. TextInput,
  4. Text,
  5. View,
  6. InputModeOptions,
  7. NativeSyntheticEvent,
  8. TextInputFocusEventData,
  9. TouchableOpacity
  10. } from 'react-native';
  11. import { styling } from './style';
  12. import { Colors } from 'src/theme';
  13. type Props = {
  14. placeholder?: string;
  15. onChange?: (text: string) => void;
  16. header?: string;
  17. isPrivate?: boolean;
  18. inputMode?: InputModeOptions;
  19. isFocused?: (boolean: boolean) => void;
  20. value?: string;
  21. active?: boolean;
  22. onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
  23. formikError?: string | boolean;
  24. icon?: ReactNode;
  25. multiline?: boolean;
  26. editable?: boolean;
  27. height?: number;
  28. backgroundColor?: string;
  29. clearIcon?: ReactNode;
  30. setValue?: (value: string) => void;
  31. };
  32. export const Input: FC<Props> = ({
  33. onChange,
  34. placeholder,
  35. header,
  36. isPrivate,
  37. inputMode,
  38. isFocused,
  39. onBlur,
  40. value,
  41. formikError,
  42. icon,
  43. multiline,
  44. editable,
  45. height,
  46. backgroundColor = Colors.FILL_LIGHT,
  47. clearIcon,
  48. setValue
  49. }) => {
  50. const [focused, setFocused] = useState(false);
  51. const styles = styling(focused);
  52. return (
  53. <View>
  54. {header ? <Text style={styles.text}>{header}</Text> : null}
  55. <View
  56. style={[
  57. [styles.wrapper, formikError ? styles.inputError : null, { backgroundColor }],
  58. { flexDirection: 'row', alignItems: 'center' },
  59. multiline ? { minHeight: height ?? 100, height: 'auto' } : { height: height ?? 44 }
  60. ]}
  61. >
  62. {icon ? (
  63. <View
  64. style={{
  65. height: 44,
  66. width: 44,
  67. display: 'flex',
  68. justifyContent: 'center',
  69. alignItems: 'center'
  70. }}
  71. >
  72. {icon}
  73. </View>
  74. ) : null}
  75. <TextInput
  76. editable={editable}
  77. value={value}
  78. inputMode={inputMode ?? 'text'}
  79. secureTextEntry={isPrivate ?? false}
  80. placeholder={placeholder}
  81. onChangeText={onChange}
  82. multiline={multiline}
  83. contextMenuHidden={inputMode === 'none'}
  84. caretHidden={inputMode === 'none'}
  85. onPress={() => {
  86. setFocused(true);
  87. if (isFocused) {
  88. isFocused(true);
  89. }
  90. }}
  91. onFocus={() => {
  92. if (inputMode !== 'none') {
  93. setFocused(true);
  94. if (isFocused) {
  95. isFocused(true);
  96. }
  97. }
  98. }}
  99. onBlur={(e) => {
  100. setFocused(false);
  101. if (onBlur) {
  102. onBlur(e);
  103. }
  104. }}
  105. style={[{ height: '100%', width: '100%', flex: 1 }, !icon ? { padding: 10 } : null]}
  106. />
  107. {clearIcon && value && value?.length > 0 ? (
  108. <TouchableOpacity
  109. style={styles.clearIcon}
  110. onPress={() => {
  111. setValue && setValue('');
  112. }}
  113. >
  114. {clearIcon}
  115. </TouchableOpacity>
  116. ) : null}
  117. </View>
  118. {formikError ? <Text style={styles.errorText}>{formikError}</Text> : null}
  119. </View>
  120. );
  121. };