-
Notifications
You must be signed in to change notification settings - Fork 266
/
CardForm.tsx
200 lines (187 loc) · 5.86 KB
/
CardForm.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
import type { CardFormView, 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 CardFormNative =
requireNativeComponent<CardFormView.NativeProps>('CardForm');
/**
* Card Form Component Props
*/
export interface Props extends AccessibilityProps {
style?: StyleProp<ViewStyle>;
autofocus?: boolean;
testID?: string;
/** Applies a disabled state such that user input is not accepted. Defaults to false. */
disabled?: boolean;
/** All styles except backgroundColor, cursorColor, borderColor, and borderRadius are Android only */
cardStyle?: CardFormView.Styles;
/** 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>;
// TODO: will make it public when iOS SDK allows for this
// postalCodeEnabled?: boolean;
/** Android only */
placeholders?: CardFormView.Placeholders;
/** Android only */
defaultValues?: CardFormView.DefaultValues;
// onBlur?(): void;
// onFocus?(focusedField: CardFormView.FieldNames | null): void;
onFormComplete?(card: CardFormView.Details): void;
/**
* WARNING: If set to `true` the full card number will be returned in the `onFormComplete` 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 Form Component
*
* @example
* ```ts
* <CardForm
* onFormComplete={(cardDetails) => {
* console.log('card details', cardDetails);
* setCard(cardDetails);
* }}
* style={{height: 200}}
* />
* ```
* @param __namedParameters Props
* @returns JSX.Element
* @category ReactComponents
*/
export const CardForm = forwardRef<CardFormView.Methods, Props>(
(
{
onFormComplete,
cardStyle,
// postalCodeEnabled = true,
// onFocus,
// onBlur,
placeholders,
defaultValues,
...props
},
ref
) => {
const inputRef = useRef<any>(null);
const onFormCompleteHandler = useCallback(
(event: NativeSyntheticEvent<CardFormView.Details>) => {
const card = event.nativeEvent;
const data: CardFormView.Details = {
last4: card.last4,
expiryMonth: card.expiryMonth,
expiryYear: card.expiryYear,
complete: card.complete,
brand: card.brand,
country: card.country,
postalCode: card.postalCode,
};
if (card.hasOwnProperty('number') || card.hasOwnProperty('cvc')) {
data.number = card.number || '';
data.cvc = card.cvc || '';
if (__DEV__ && onFormComplete && 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`
);
}
}
onFormComplete?.(data);
},
[onFormComplete]
);
const focus = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(inputRef.current),
'focus' as any,
[]
);
};
const blur = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(inputRef.current),
'blur' as any,
[]
);
};
useImperativeHandle(ref, () => ({
focus,
blur,
}));
const onFocusHandler = useCallback((event) => {
const { focusedField } = event.nativeEvent;
if (focusedField) {
focusInput(inputRef.current);
// onFocus?.(focusedField);
} else {
// onBlur?.();
}
}, []);
useLayoutEffect(() => {
const inputRefValue = inputRef.current;
if (inputRefValue !== null) {
registerInput(inputRefValue);
return () => {
unregisterInput(inputRefValue);
if (currentlyFocusedInput() === inputRefValue) {
inputRefValue.blur();
}
};
}
return () => {};
}, [inputRef]);
return (
<CardFormNative
ref={inputRef}
onFormComplete={onFormCompleteHandler}
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,
// disabledBackgroundColor: cardStyle?.disabledBackgroundColor,
// type: cardStyle?.type,
}}
placeholders={{
number: placeholders?.number,
expiration: placeholders?.expiration,
cvc: placeholders?.cvc,
postalCode: placeholders?.postalCode,
}}
defaultValues={{
...(defaultValues ?? {}),
}}
onFocusChange={onFocusHandler}
// postalCodeEnabled={postalCodeEnabled}
{...props}
/>
);
}
);