index.tsx 2.4 KB

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