2020-02-14 12:30:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-23 11:22:00 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-02-14 12:30:33 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2021-09-21 10:48:39 +00:00
|
|
|
import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element';
|
2020-08-11 09:00:53 +00:00
|
|
|
import classnames from 'classnames';
|
2022-11-10 10:05:41 +00:00
|
|
|
import { ValidatedTextInput } from '@woocommerce/blocks-checkout';
|
2020-02-14 12:30:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-08-12 16:30:42 +00:00
|
|
|
import Combobox from '../combobox';
|
2020-08-11 09:00:53 +00:00
|
|
|
import './style.scss';
|
2021-06-17 08:35:24 +00:00
|
|
|
import type { StateInputWithStatesProps } from './StateInputProps';
|
2020-02-14 12:30:33 +00:00
|
|
|
|
2021-09-21 10:48:39 +00:00
|
|
|
const optionMatcher = (
|
|
|
|
value: string,
|
|
|
|
options: { label: string; value: string }[]
|
|
|
|
): string => {
|
|
|
|
const foundOption = options.find(
|
|
|
|
( option ) =>
|
|
|
|
option.label.toLocaleUpperCase() === value.toLocaleUpperCase() ||
|
|
|
|
option.value.toLocaleUpperCase() === value.toLocaleUpperCase()
|
|
|
|
);
|
|
|
|
return foundOption ? foundOption.value : '';
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:12:15 +00:00
|
|
|
const StateInput = ( {
|
2020-02-14 12:30:33 +00:00
|
|
|
className,
|
2020-04-02 09:27:54 +00:00
|
|
|
id,
|
2020-02-19 17:14:41 +00:00
|
|
|
states,
|
2020-02-14 12:30:33 +00:00
|
|
|
country,
|
|
|
|
label,
|
|
|
|
onChange,
|
2020-02-19 15:10:26 +00:00
|
|
|
autoComplete = 'off',
|
2020-02-14 12:30:33 +00:00
|
|
|
value = '',
|
2020-03-03 10:46:53 +00:00
|
|
|
required = false,
|
2021-06-17 08:35:24 +00:00
|
|
|
}: StateInputWithStatesProps ): JSX.Element => {
|
2020-02-19 17:14:41 +00:00
|
|
|
const countryStates = states[ country ];
|
2020-09-03 08:02:26 +00:00
|
|
|
const options = useMemo(
|
|
|
|
() =>
|
|
|
|
countryStates
|
|
|
|
? Object.keys( countryStates ).map( ( key ) => ( {
|
2021-08-12 16:30:42 +00:00
|
|
|
value: key,
|
|
|
|
label: decodeEntities( countryStates[ key ] ),
|
2020-09-03 08:02:26 +00:00
|
|
|
} ) )
|
|
|
|
: [],
|
|
|
|
[ countryStates ]
|
|
|
|
);
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-02-19 15:10:26 +00:00
|
|
|
/**
|
|
|
|
* Handles state selection onChange events. Finds a matching state by key or value.
|
|
|
|
*/
|
|
|
|
const onChangeState = useCallback(
|
2021-09-21 10:48:39 +00:00
|
|
|
( stateValue: string ) => {
|
|
|
|
onChange(
|
|
|
|
options.length > 0
|
|
|
|
? optionMatcher( stateValue, options )
|
|
|
|
: stateValue
|
|
|
|
);
|
2020-02-19 15:10:26 +00:00
|
|
|
},
|
|
|
|
[ onChange, options ]
|
|
|
|
);
|
|
|
|
|
2021-09-21 10:48:39 +00:00
|
|
|
/**
|
|
|
|
* Track value changes.
|
|
|
|
*/
|
|
|
|
const valueRef = useRef< string >( value );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( valueRef.current !== value ) {
|
|
|
|
valueRef.current = value;
|
|
|
|
}
|
|
|
|
}, [ value ] );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If given a list of options, ensure the value matches those options or trigger change.
|
|
|
|
*/
|
|
|
|
useEffect( () => {
|
|
|
|
if ( options.length > 0 && valueRef.current ) {
|
|
|
|
const match = optionMatcher( valueRef.current, options );
|
|
|
|
if ( match !== valueRef.current ) {
|
|
|
|
onChangeState( match );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ options, onChangeState ] );
|
|
|
|
|
2020-03-06 15:40:07 +00:00
|
|
|
if ( options.length > 0 ) {
|
2020-03-03 10:46:53 +00:00
|
|
|
return (
|
2022-12-19 12:26:02 +00:00
|
|
|
<Combobox
|
|
|
|
className={ classnames(
|
|
|
|
className,
|
|
|
|
'wc-block-components-state-input'
|
2020-03-06 15:40:07 +00:00
|
|
|
) }
|
2022-12-19 12:26:02 +00:00
|
|
|
id={ id }
|
|
|
|
label={ label }
|
|
|
|
onChange={ onChangeState }
|
|
|
|
options={ options }
|
|
|
|
value={ value }
|
|
|
|
errorMessage={ __(
|
|
|
|
'Please select a state.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
required={ required }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
/>
|
2020-03-03 10:46:53 +00:00
|
|
|
);
|
|
|
|
}
|
2021-09-21 10:48:39 +00:00
|
|
|
|
2020-03-03 10:46:53 +00:00
|
|
|
return (
|
2020-12-17 14:52:44 +00:00
|
|
|
<ValidatedTextInput
|
2020-02-14 12:30:33 +00:00
|
|
|
className={ className }
|
2020-04-02 09:27:54 +00:00
|
|
|
id={ id }
|
2020-02-14 12:30:33 +00:00
|
|
|
label={ label }
|
2020-02-19 15:10:26 +00:00
|
|
|
onChange={ onChangeState }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
value={ value }
|
2020-03-03 10:46:53 +00:00
|
|
|
required={ required }
|
2020-02-14 12:30:33 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:12:15 +00:00
|
|
|
export default StateInput;
|