/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { SelectControl, TextControl } from '@wordpress/components'; /** * Internal dependencies */ import type { PickupLocation } from '../types'; import { countryStateOptions, states } from '../utils'; const Form = ( { formRef, values, setValues, }: { formRef: React.RefObject< HTMLFormElement >; values: PickupLocation; setValues: React.Dispatch< React.SetStateAction< PickupLocation > >; } ) => { const { country: selectedCountry, state: selectedState } = values.address; 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, }, } ) ); }; const countryHasStates = states[ selectedCountry ] && Object.keys( states[ selectedCountry ] ).length > 0; return (
) => { event.target.setCustomValidity( __( 'A Location title is required', 'woo-gutenberg-products-block' ) ); } } onInput={ ( event: React.ChangeEvent< HTMLInputElement > ) => { event.target.setCustomValidity( '' ); } } /> { ! countryHasStates && ( ) } { if ( ! selectedState && countryHasStates ) { return `${ selectedCountry }:${ Object.keys( states[ selectedCountry ] )[ 0 ] }`; } return `${ selectedCountry }${ selectedState && states[ selectedCountry ]?.[ selectedState ] ? ':' + selectedState : '' }`; } )() } onChange={ ( val: string ) => { const [ country, state = '' ] = val.split( ':' ); setLocationAddressField( 'country' )( country ); setLocationAddressField( 'state' )( state ); } } > { countryStateOptions.options.map( ( option ) => { if ( option.label ) { return ( { option.options.map( ( subOption ) => ( ) ) } ); } return ( ); } ) } ); }; export default Form;