diff --git a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/address-form/index.js b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/address-form/index.js index 43e79758fff..52fc82c3ba4 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/address-form/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/address-form/index.js @@ -2,7 +2,7 @@ * External dependencies */ import PropTypes from 'prop-types'; -import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; +import { DebouncedValidatedTextInput } from '@woocommerce/base-components/text-input'; import { BillingCountryInput, ShippingCountryInput, @@ -172,7 +172,7 @@ const AddressForm = ( { } return ( - { + // Keep a local copy of the value so we can debounce updates. + const [ inputValue, setInputValue ] = useState( value ); + const [ debouncedCallback ] = useDebouncedCallback( ( newValue ) => { + onChange( newValue ); + }, 400 ); + const debouncedOnChange = useShallowEqual( debouncedCallback ); + useEffect( () => { + debouncedOnChange( inputValue ); + }, [ debouncedOnChange, inputValue ] ); + + return ( + + ); +}; + +DebouncedValidatedTextInput.propTypes = { + onChange: PropTypes.func.isRequired, + value: PropTypes.string, +}; + +export default DebouncedValidatedTextInput; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js b/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js index abf65f397d6..6ae562d79cf 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js @@ -110,3 +110,4 @@ TextInput.propTypes = { export default TextInput; export { default as ValidatedTextInput } from './validated'; +export { default as DebouncedValidatedTextInput } from './debounced-validated'; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js index fca1f7b9b55..92923b67889 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { FormStep } from '@woocommerce/base-components/cart-checkout'; -import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; +import { DebouncedValidatedTextInput } from '@woocommerce/base-components/text-input'; import { useCheckoutContext } from '@woocommerce/base-context'; /** @@ -29,7 +29,7 @@ const ContactFieldsStep = ( { emailValue, onChangeEmail } ) => { ) } stepHeadingContent={ () => } > - { showPhoneField && ( -