/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { SelectControl, TextControl } from '@wordpress/components'; import { getSetting } from '@woocommerce/settings'; /** * Internal dependencies */ import type { PickupLocation } from '../types'; import StateControl from './state-control'; const Form = ( { formRef, values, setValues, }: { formRef: React.RefObject< HTMLFormElement >; values: PickupLocation; setValues: React.Dispatch< React.SetStateAction< PickupLocation > >; } ) => { const countries = getSetting< Record< string, string > >( 'countries', [] ); const states = getSetting< Record< string, Record< string, string > > >( 'countryStates', [] ); const setLocationField = ( field: keyof PickupLocation ) => ( newValue: string | boolean ) => { setValues( ( prevValue: PickupLocation ) => ( { ...prevValue, [ field ]: newValue, } ) ); }; const setLocationAddressField = ( field: keyof PickupLocation[ 'address' ] ) => ( newValue: string | boolean ) => { setValues( ( prevValue ) => ( { ...prevValue, address: { ...prevValue.address, [ field ]: newValue, }, } ) ); }; return (
) => { event.target.setCustomValidity( __( 'A Location title is required', 'woo-gutenberg-products-block' ) ); } } onInput={ ( event: React.ChangeEvent< HTMLInputElement > ) => { event.target.setCustomValidity( '' ); } } /> { setLocationAddressField( 'state' )( '' ); setLocationAddressField( 'country' )( val ); } } autoComplete="off" options={ [ { value: '', disabled: true, label: __( 'Country', 'woo-gutenberg-products-block' ), }, ...Object.entries( countries ).map( ( [ code, country ] ) => ( { value: code, label: country, } ) ), ] } /> ); }; export default Form;