Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Clear hidden fields on load
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jun 3, 2021
1 parent d409276 commit 60e5232
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
33 changes: 21 additions & 12 deletions assets/js/base/context/hooks/use-checkout-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,29 @@ export const useCheckoutAddress = () => {
}
}, [ shippingAsBilling, setBillingData, shippingAddress, billingData ] );

const setEmail = ( value ) =>
void setBillingData( {
email: value,
} );
const setEmail = useCallback(
( value ) =>
void setBillingData( {
email: value,
} ),
[ setBillingData ]
);

const setPhone = ( value ) =>
void setBillingData( {
phone: value,
} );
const setPhone = useCallback(
( value ) =>
void setBillingData( {
phone: value,
} ),
[ setBillingData ]
);

const setShippingPhone = ( value ) =>
void setShippingFields( {
phone: value,
} );
const setShippingPhone = useCallback(
( value ) =>
void setShippingFields( {
phone: value,
} ),
[ setShippingFields ]
);

// Note that currentShippingAsBilling is returned rather than the current state of shippingAsBilling--this is so that
// the billing fields are not rendered before sync (billing field values are debounced and would be outdated)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { useMemo } from '@wordpress/element';
import { useMemo, useEffect } from '@wordpress/element';
import {
useStoreEvents,
useCheckoutAddress,
Expand Down Expand Up @@ -34,6 +34,13 @@ const Block = ( {
} = useCheckoutAddress();
const { dispatchCheckoutEvent } = useStoreEvents();

// Clears data if fields are hidden.
useEffect( () => {
if ( ! showPhoneField ) {
setPhone( '' );
}
}, [ showPhoneField, setPhone ] );

const addressFieldsConfig = useMemo( () => {
return {
company: {
Expand All @@ -51,7 +58,7 @@ const Block = ( {
<AddressForm
id="billing"
type="billing"
onChange={ ( values ) => {
onChange={ ( values: Record< string, unknown > ) => {
setBillingFields( values );
dispatchCheckoutEvent( 'set-billing-address' );
} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useMemo, useEffect } from '@wordpress/element';
import { AddressForm } from '@woocommerce/base-components/cart-checkout';
import { useCheckoutAddress, useStoreEvents } from '@woocommerce/base-context';
import CheckboxControl from '@woocommerce/base-components/checkbox-control';
Expand Down Expand Up @@ -35,6 +35,13 @@ const Block = ( {
} = useCheckoutAddress();
const { dispatchCheckoutEvent } = useStoreEvents();

// Clears data if fields are hidden.
useEffect( () => {
if ( ! showPhoneField ) {
setShippingPhone( '' );
}
}, [ showPhoneField, setShippingPhone ] );

const addressFieldsConfig = useMemo( () => {
return {
company: {
Expand All @@ -52,7 +59,10 @@ const Block = ( {
<AddressForm
id="shipping"
type="shipping"
onChange={ setShippingFields }
onChange={ ( values: Record< string, unknown > ) => {
setShippingFields( values );
dispatchCheckoutEvent( 'set-shipping-address' );
} }
values={ shippingFields }
fields={ Object.keys( defaultAddressFields ) }
fieldConfig={ addressFieldsConfig }
Expand Down

0 comments on commit 60e5232

Please sign in to comment.