/** * External dependencies */ import PropTypes from 'prop-types'; import { __ } from '@wordpress/i18n'; import { Button } from '@woocommerce/base-components/cart-checkout'; import { useState } from '@wordpress/element'; import isShallowEqual from '@wordpress/is-shallow-equal'; import { useValidationContext } from '@woocommerce/base-context'; /** * Internal dependencies */ import './style.scss'; import AddressForm from '../address-form'; const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate, addressFields, } ) => { const [ address, setAddress ] = useState( initialAddress ); const { hasValidationErrors, showAllValidationErrors, } = useValidationContext(); const validateSubmit = () => { showAllValidationErrors(); if ( hasValidationErrors() ) { return false; } return true; }; // Make all fields optional except 'country'. const fieldConfig = {}; addressFields.forEach( ( field ) => { if ( field === 'country' ) { fieldConfig[ field ] = { ...fieldConfig[ field ], errorMessage: __( 'Please select a country to calculate rates.', 'woo-gutenberg-products-block' ), required: true, }; } else { fieldConfig[ field ] = { ...fieldConfig[ field ], required: false, }; } } ); return (
); }; ShippingCalculatorAddress.propTypes = { address: PropTypes.object.isRequired, onUpdate: PropTypes.func.isRequired, addressFields: PropTypes.array.isRequired, }; export default ShippingCalculatorAddress;