/
CardField.tsx
210 lines (197 loc) · 6.35 KB
/
CardField.tsx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { CardFieldInput, CardBrand } from '../types';
import React, {
forwardRef,
useCallback,
useImperativeHandle,
useLayoutEffect,
useRef,
} from 'react';
import {
AccessibilityProps,
NativeSyntheticEvent,
requireNativeComponent,
UIManager,
StyleProp,
findNodeHandle,
ViewStyle,
} from 'react-native';
import {
currentlyFocusedInput,
focusInput,
registerInput,
unregisterInput,
} from '../helpers';
const CardFieldNative =
requireNativeComponent<CardFieldInput.NativeProps>('CardField');
/**
* Card Field Component Props
*/
export interface Props extends AccessibilityProps {
style?: StyleProp<ViewStyle>;
/** Controls if a postal code entry field can be displayed to the user. If true, the type of code entry shown is controlled by the set countryCode prop. Some country codes may result in no postal code entry being shown if those countries do not commonly use postal codes. If false, no postal code entry will ever be displayed. Defaults to true.*/
postalCodeEnabled?: boolean;
/** Controls the postal code entry shown (if the postalCodeEnabled prop is set to true). Defaults to the device's default locale. */
countryCode?: string;
/** Applies a disabled state such that user input is not accepted. Defaults to false. */
disabled?: boolean;
cardStyle?: CardFieldInput.Styles;
placeholders?: CardFieldInput.Placeholders;
autofocus?: boolean;
onCardChange?(card: CardFieldInput.Details): void;
onBlur?(): void;
onFocus?(focusedField: CardFieldInput.FieldName | null): void;
testID?: string;
/** The list of preferred networks that should be used to process payments made with a co-branded card.
* This value will only be used if your user hasn't selected a network themselves. */
preferredNetworks?: Array<CardBrand>;
/**
* WARNING: If set to `true` the full card number will be returned in the `onCardChange` handler.
* Only do this if you're certain that you fulfill the necessary PCI compliance requirements.
* Make sure that you're not mistakenly logging or storing full card details!
* See the docs for details: https://stripe.com/docs/security/guide#validating-pci-compliance
*/
dangerouslyGetFullCardDetails?: boolean;
}
/**
* Card Field Component
*
* @example
* ```ts
* <CardField
* postalCodeEnabled={false}
* onCardChange={(cardDetails) => {
* console.log('card details', cardDetails);
* setCard(cardDetails);
* }}
* style={{height: 50}}
* />
* ```
* @param __namedParameters Props
* @returns JSX.Element
* @category ReactComponents
*/
export const CardField = forwardRef<CardFieldInput.Methods, Props>(
(
{
onCardChange,
onFocus,
onBlur,
cardStyle,
placeholders,
postalCodeEnabled,
countryCode,
...props
},
ref
) => {
const inputRef = useRef<any>(null);
const onCardChangeHandler = useCallback(
(event: NativeSyntheticEvent<CardFieldInput.Details>) => {
const card = event.nativeEvent;
const data: CardFieldInput.Details = {
last4: card.last4,
expiryMonth: card.expiryMonth,
expiryYear: card.expiryYear,
complete: card.complete,
brand: card.brand,
validExpiryDate: card.validExpiryDate,
validNumber: card.validNumber,
validCVC: card.validCVC,
};
if (card.hasOwnProperty('postalCode')) {
data.postalCode = card.postalCode || '';
}
if (card.hasOwnProperty('number') || card.hasOwnProperty('cvc')) {
data.number = card.number || '';
data.cvc = card.cvc || '';
if (__DEV__ && onCardChange && card.complete) {
console.warn(
`[stripe-react-native] ⚠️ WARNING: You've enabled \`dangerouslyGetFullCardDetails\`, meaning full card details are being returned. Only do this if you're certain that you fulfill the necessary PCI compliance requirements. Make sure that you're not mistakenly logging or storing full card details! See the docs for details: https://stripe.com/docs/security/guide#validating-pci-compliance`
);
}
}
onCardChange?.(data);
},
[onCardChange]
);
const onFocusHandler = useCallback(
(event) => {
const { focusedField } = event.nativeEvent;
if (focusedField) {
focusInput(inputRef.current);
onFocus?.(focusedField);
} else {
onBlur?.();
}
},
[onFocus, onBlur]
);
const focus = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(inputRef.current),
'focus' as any,
[]
);
};
const blur = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(inputRef.current),
'blur' as any,
[]
);
};
const clear = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(inputRef.current),
'clear' as any,
[]
);
};
useImperativeHandle(ref, () => ({
focus,
blur,
clear,
}));
useLayoutEffect(() => {
const inputRefValue = inputRef.current;
if (inputRefValue !== null) {
registerInput(inputRefValue);
return () => {
unregisterInput(inputRefValue);
if (currentlyFocusedInput() === inputRefValue) {
inputRefValue.blur();
}
};
}
return () => {};
}, [inputRef]);
return (
<CardFieldNative
ref={inputRef}
onCardChange={onCardChangeHandler}
onFocusChange={onFocusHandler}
postalCodeEnabled={postalCodeEnabled ?? true}
countryCode={countryCode ?? null}
cardStyle={{
backgroundColor: cardStyle?.backgroundColor,
borderColor: cardStyle?.borderColor,
borderWidth: cardStyle?.borderWidth,
borderRadius: cardStyle?.borderRadius,
cursorColor: cardStyle?.cursorColor,
fontSize: cardStyle?.fontSize,
placeholderColor: cardStyle?.placeholderColor,
textColor: cardStyle?.textColor,
textErrorColor: cardStyle?.textErrorColor,
fontFamily: cardStyle?.fontFamily,
}}
placeholders={{
number: placeholders?.number,
expiration: placeholders?.expiration,
cvc: placeholders?.cvc,
postalCode: placeholders?.postalCode,
}}
{...props}
/>
);
}
);