-
Notifications
You must be signed in to change notification settings - Fork 709
Expand file tree
/
Copy pathCreditCardInput.tsx
More file actions
179 lines (169 loc) · 4.37 KB
/
Copy pathCreditCardInput.tsx
File metadata and controls
179 lines (169 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { useEffect, useRef, type RefObject } from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
type TextStyle,
type ViewStyle,
} from 'react-native';
import {
useCreditCardForm,
type CreditCardFormData,
type CreditCardFormField,
} from './useCreditCardForm';
interface Props {
autoFocus?: boolean;
style?: ViewStyle;
labelStyle?: TextStyle;
numberInputStyle?: TextStyle;
expiryInputStyle?: TextStyle;
cvcInputStyle?: TextStyle;
numberInputRef?: RefObject<TextInput>;
expiryInputRef?: RefObject<TextInput>;
cvcInputRef?: RefObject<TextInput>;
placeholderColor?: string;
labels?: {
number: string;
expiry: string;
cvc: string;
};
placeholders?: {
number: string;
expiry: string;
cvc: string;
};
onChange: (formData: CreditCardFormData) => void;
onFocusField?: (field: CreditCardFormField) => void;
testID?: string;
}
const s = StyleSheet.create({
container: {
paddingVertical: 15,
paddingHorizontal: 15,
},
icon: {
width: 48,
height: 40,
resizeMode: 'contain',
},
numberInput: {},
extraContainer: {
flexDirection: 'row',
marginTop: 15,
},
expiryInputContainer: {
flex: 1,
marginRight: 5,
},
cvcInputContainer: {
flex: 1,
marginLeft: 5,
},
input: {
height: 40,
fontSize: 16,
borderBottomColor: 'darkgray',
borderBottomWidth: 1,
// @ts-expect-error outlineWidth is used to hide the text-input outline on react-native-web
outlineWidth: 0,
},
inputLabel: {
fontSize: 14,
fontWeight: 600,
},
});
const CreditCardInput = (props: Props) => {
const {
autoFocus,
style,
labelStyle,
numberInputStyle,
expiryInputStyle,
cvcInputStyle,
numberInputRef,
expiryInputRef,
cvcInputRef,
placeholderColor = 'darkgray',
labels = {
number: 'CARD NUMBER',
expiry: 'EXPIRY',
cvc: 'CVC/CVV',
},
placeholders = {
number: '1234 5678 1234 5678',
expiry: 'MM/YY',
cvc: 'CVC',
},
onChange = () => {},
onFocusField = () => {},
testID,
} = props;
const { values, onChangeValue } = useCreditCardForm(onChange);
const numberInput = useRef<TextInput>(null);
useEffect(() => {
if (autoFocus) {
(numberInputRef)
? numberInputRef.current?.focus()
: numberInput.current?.focus();
}
}, [autoFocus]);
return (
<View
style={[s.container, style]}
testID={testID}
>
<View style={[s.numberInput]}>
<Text style={[s.inputLabel, labelStyle]}>{labels.number}</Text>
<TextInput
ref={numberInputRef || numberInput}
keyboardType="numeric"
style={[s.input, numberInputStyle]}
placeholderTextColor={placeholderColor}
placeholder={placeholders.number}
value={values.number}
onChangeText={(v) => onChangeValue('number', v)}
onFocus={() => onFocusField('number')}
autoCorrect={false}
underlineColorAndroid={'transparent'}
testID="CC_NUMBER"
/>
</View>
<View style={[s.extraContainer]}>
<View style={s.expiryInputContainer}>
<Text style={[s.inputLabel, labelStyle]}>{labels.expiry}</Text>
<TextInput
ref={expiryInputRef}
keyboardType="numeric"
style={[s.input, expiryInputStyle]}
placeholderTextColor={placeholderColor}
placeholder={placeholders.expiry}
value={values.expiry}
onChangeText={(v) => onChangeValue('expiry', v)}
onFocus={() => onFocusField('expiry')}
autoCorrect={false}
underlineColorAndroid={'transparent'}
testID="CC_EXPIRY"
/>
</View>
<View style={s.cvcInputContainer}>
<Text style={[s.inputLabel, labelStyle]}>{labels.cvc}</Text>
<TextInput
ref={cvcInputRef}
keyboardType="numeric"
style={[s.input, cvcInputStyle]}
placeholderTextColor={placeholderColor}
placeholder={placeholders.cvc}
value={values.cvc}
onChangeText={(v) => onChangeValue('cvc', v)}
onFocus={() => onFocusField('cvc')}
autoCorrect={false}
underlineColorAndroid={'transparent'}
testID="CC_CVC"
/>
</View>
</View>
</View>
);
};
export default CreditCardInput;