import React, { forwardRef, useCallback, useImperativeHandle, useRef, } from 'react'; import { NativeSyntheticEvent, ViewProps, StyleProp, ViewStyle, } from 'react-native'; import { requireNativeViewManager } from 'expo-modules-core'; import type { CardFieldDetails, CardFieldFocusField, CardFieldMethods, } from '../types'; type NativeCardChangeEvent = { card: CardFieldDetails; }; type NativeFocusChangeEvent = { focusedField: string; }; export interface CardFieldProps extends ViewProps { style?: StyleProp; autofocus?: boolean; disabled?: boolean; onCardChange?(card: CardFieldDetails): void; onFocus?(focusedField: CardFieldFocusField): void; onBlur?(): void; } type NativeProps = ViewProps & { ref?: React.Ref; autofocus?: boolean; disabled?: boolean; onCardChange?: (event: NativeSyntheticEvent) => void; onFocusChange?: (event: NativeSyntheticEvent) => void; }; type NativeCardFieldRef = { focus(): Promise; blur(): Promise; clear(): Promise; }; const NativeCardField: React.ComponentType = requireNativeViewManager('SFPYCardField'); const ignore = () => {}; export const CardField = forwardRef( ( { onCardChange, onFocus, onBlur, autofocus, disabled, ...props }, ref ) => { const nativeRef = useRef(null); const onCardChangeHandler = useCallback( (event: NativeSyntheticEvent) => { onCardChange?.(event.nativeEvent.card); }, [onCardChange] ); const onFocusChangeHandler = useCallback( (event: NativeSyntheticEvent) => { const focusedField = event.nativeEvent.focusedField; if (focusedField) { onFocus?.(focusedField as CardFieldFocusField); } else { onBlur?.(); } }, [onFocus, onBlur] ); useImperativeHandle(ref, () => ({ focus: () => { nativeRef.current?.focus().catch(ignore); }, blur: () => { nativeRef.current?.blur().catch(ignore); }, clear: () => { nativeRef.current?.clear().catch(ignore); }, })); return ( ); } );