|
1 | 1 | import React from 'react'; |
2 | | -import {ActivityIndicator, Text, View, Pressable} from 'react-native'; |
| 2 | +import {Text, View, TextInput} from 'react-native'; |
3 | 3 |
|
4 | | -const Button = ({ |
5 | | - disabled = false, |
6 | | - onPress, |
7 | | - busy = false, |
8 | | - loaderIcon = <ActivityIndicator color="#fff" />, |
9 | | - busyText = 'Processing..', |
| 4 | +const Input = ({ |
| 5 | + onChangeText, |
| 6 | + iconPosition, |
| 7 | + icon, |
| 8 | + value, |
| 9 | + placeholder = 'Enter Text', |
| 10 | + label = null, |
| 11 | + error, |
10 | 12 | ...props |
11 | 13 | }) => { |
12 | | - const textStyles = { |
13 | | - ...(props.textStyles || { |
14 | | - fontWeight: 'bold', |
| 14 | + const [focused, setFocused] = React.useState(false); |
| 15 | + const textInputStyles = { |
| 16 | + ...(props.textInputStyles || { |
| 17 | + flex: 1, |
| 18 | + width: '100%', |
15 | 19 | color: props.textColor || '#fff', |
16 | 20 | letterSpacing: 0, |
17 | 21 | }), |
18 | 22 | }; |
| 23 | + const getFlexDirection = () => { |
| 24 | + if (icon && iconPosition) { |
| 25 | + if (iconPosition === 'left') { |
| 26 | + return 'row'; |
| 27 | + } else if (iconPosition === 'right') { |
| 28 | + return 'row-reverse'; |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
19 | 32 |
|
20 | | - return ( |
21 | | - <Pressable |
22 | | - disabledd={disabled} |
23 | | - onPress={() => { |
24 | | - if (!disabled) { |
25 | | - onPress(); |
26 | | - } |
27 | | - }} |
28 | | - style={({pressed}) => [ |
29 | | - { |
30 | | - backgroundColor: pressed |
31 | | - ? props.onPressColor || '#27272A' |
32 | | - : props.backgroundColor || '#18181B', |
33 | | - width: '100%', |
34 | | - borderColor: props.borderColor || '#27272A', |
35 | | - borderWidth: 1, |
36 | | - borderRadius: 4, |
37 | | - paddingVertical: 8, |
38 | | - paddingHorizontal: 4, |
39 | | - alignItems: 'center', |
40 | | - justifyContent: 'center', |
41 | | - }, |
42 | | - ]}> |
43 | | - {busy === false && ( |
44 | | - <View> |
45 | | - {typeof props.children === 'string' ? ( |
46 | | - <Text style={textStyles}>{props.children}</Text> |
47 | | - ) : ( |
48 | | - props.children |
49 | | - )} |
50 | | - </View> |
51 | | - )} |
| 33 | + const getBorderColor = () => { |
| 34 | + if (error) { |
| 35 | + return props.borderColor || '#DC2626'; |
| 36 | + } |
| 37 | + |
| 38 | + if (focused) { |
| 39 | + return props.focusColor || '#52525B'; |
| 40 | + } |
| 41 | + }; |
52 | 42 |
|
53 | | - {busy === true && ( |
54 | | - <View |
55 | | - style={{ |
56 | | - ...(props.loaderIconWrapperStyles || { |
57 | | - alignItems: 'center', |
58 | | - flexDirection: 'row', |
59 | | - justifyContent: 'center', |
60 | | - }), |
61 | | - }}> |
62 | | - <View |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <View style={{...props.labelStyles}}> |
| 46 | + {label && <Text>{label}</Text>} |
| 47 | + </View> |
| 48 | + <View |
| 49 | + style={{ |
| 50 | + ...(props.wrapperStyles || { |
| 51 | + height: 42, |
| 52 | + borderWidth: 1, |
| 53 | + borderRadius: 4, |
| 54 | + paddingHorizontal: 5, |
| 55 | + marginTop: 5, |
| 56 | + alignItems: icon ? 'center' : 'baseline', |
| 57 | + borderColor: getBorderColor(), |
| 58 | + flexDirection: getFlexDirection(), |
| 59 | + }), |
| 60 | + }}> |
| 61 | + <View>{icon && icon}</View> |
| 62 | + <TextInput |
| 63 | + placeholder={placeholder} |
| 64 | + style={textInputStyles} |
| 65 | + onChangeText={onChangeText} |
| 66 | + value={value} |
| 67 | + onFocus={() => { |
| 68 | + setFocused(true); |
| 69 | + }} |
| 70 | + onBlur={() => { |
| 71 | + setFocused(false); |
| 72 | + }} |
| 73 | + {...props} |
| 74 | + /> |
| 75 | + </View> |
| 76 | + <View> |
| 77 | + {error && ( |
| 78 | + <Text |
63 | 79 | style={{ |
64 | | - ...(props.loaderStyles || { |
65 | | - marginRight: 20, |
| 80 | + ...(props.errorTextStyles || { |
| 81 | + color: '#DC2626', |
| 82 | + paddingTop: 4, |
| 83 | + fontSize: 12, |
66 | 84 | }), |
67 | 85 | }}> |
68 | | - {loaderIcon} |
69 | | - </View> |
70 | | - |
71 | | - <Text style={textStyles}>{busyText}</Text> |
72 | | - </View> |
73 | | - )} |
74 | | - </Pressable> |
| 86 | + {error} |
| 87 | + </Text> |
| 88 | + )} |
| 89 | + </View> |
| 90 | + </> |
75 | 91 | ); |
76 | 92 | }; |
77 | 93 |
|
78 | | -export default Button; |
| 94 | +export default Input; |
0 commit comments