/** * External dependencies */ import PropTypes from 'prop-types'; import TextInput from '@woocommerce/base-components/text-input'; import { BillingCountryInput, ShippingCountryInput, } from '@woocommerce/base-components/country-input'; import { BillingStateInput, ShippingStateInput, } from '@woocommerce/base-components/state-input'; /** * Internal dependencies */ import defaultAddressFields from './default-address-fields'; import countryAddressFields from './country-address-fields'; /** * Checkout address form. */ const AddressForm = ( { fields = Object.keys( defaultAddressFields ), fieldConfig = {}, onChange, type = 'shipping', values, } ) => { const countryLocale = countryAddressFields[ values.country ] || {}; const addressFields = fields.map( ( field ) => ( { key: field, ...defaultAddressFields[ field ], ...fieldConfig[ field ], ...countryLocale[ field ], } ) ); const sortedAddressFields = addressFields.sort( ( a, b ) => a.index - b.index ); return (
{ sortedAddressFields.map( ( field ) => { if ( field.hidden ) { return null; } if ( field.key === 'country' ) { const Tag = type === 'shipping' ? ShippingCountryInput : BillingCountryInput; return ( onChange( { ...values, country: newValue, state: '', } ) } required={ field.required } /> ); } if ( field.key === 'state' ) { const Tag = type === 'shipping' ? ShippingStateInput : BillingStateInput; return ( onChange( { ...values, state: newValue, } ) } required={ field.required } /> ); } return ( onChange( { ...values, [ field.key ]: newValue, } ) } required={ field.required } /> ); } ) }
); }; AddressForm.propTypes = { onChange: PropTypes.func.isRequired, values: PropTypes.object.isRequired, fields: PropTypes.arrayOf( PropTypes.oneOf( Object.keys( defaultAddressFields ) ) ), fieldConfig: PropTypes.object, type: PropTypes.oneOf( [ 'billing', 'shipping' ] ), }; export default AddressForm;