/** * External dependencies */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { CardElement, CardNumberElement, CardExpiryElement, CardCvcElement, } from '@stripe/react-stripe-js'; /** * Internal dependencies */ import { useElementOptions } from './use-element-options'; const baseTextInputStyles = 'wc-block-gateway-input'; /** * InlineCard component */ export const InlineCard = ( { inputErrorComponent: ValidationInputError, onChange, } ) => { const [ isEmpty, setIsEmpty ] = useState( true ); const { options, onActive, error, setError } = useElementOptions( { hidePostalCode: true, } ); const errorCallback = ( event ) => { if ( event.error ) { setError( event.error.message ); } else { setError( '' ); } setIsEmpty( event.empty ); onChange( event ); }; return ( <>
onActive( isEmpty ) } onFocus={ () => onActive( isEmpty ) } onChange={ errorCallback } />
); }; /** * CardElements component. */ export const CardElements = ( { onChange, inputErrorComponent: ValidationInputError, } ) => { const [ isEmpty, setIsEmpty ] = useState( true ); const { options: cardNumOptions, onActive: cardNumOnActive, error: cardNumError, setError: cardNumSetError, } = useElementOptions( { showIcon: false } ); const { options: cardExpiryOptions, onActive: cardExpiryOnActive, error: cardExpiryError, setError: cardExpirySetError, } = useElementOptions(); const { options: cardCvcOptions, onActive: cardCvcOnActive, error: cardCvcError, setError: cardCvcSetError, } = useElementOptions(); const errorCallback = ( errorSetter ) => ( event ) => { if ( event.error ) { errorSetter( event.error.message ); } else { errorSetter( '' ); } setIsEmpty( event.empty ); onChange( event ); }; return (
cardNumOnActive( isEmpty ) } onBlur={ () => cardNumOnActive( isEmpty ) } />
cardExpiryOnActive( isEmpty ) } onBlur={ () => cardExpiryOnActive( isEmpty ) } id="wc-stripe-card-expiry-element" />
cardCvcOnActive( isEmpty ) } onBlur={ () => cardCvcOnActive( isEmpty ) } id="wc-stripe-card-code-element" />
); };