2020-02-14 12:30:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2021-09-21 10:48:39 +00:00
|
|
|
import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element';
|
2024-07-26 16:38:44 +00:00
|
|
|
import { ValidatedTextInput } from '@woocommerce/blocks-components';
|
2024-07-24 15:59:15 +00:00
|
|
|
import { clsx } from 'clsx';
|
2020-02-14 12:30:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-11 09:00:53 +00:00
|
|
|
import './style.scss';
|
2021-06-17 08:35:24 +00:00
|
|
|
import type { StateInputWithStatesProps } from './StateInputProps';
|
2024-07-24 15:59:15 +00:00
|
|
|
import { Select, SelectOption } from '../select';
|
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 ];
|
2024-07-24 15:59:15 +00:00
|
|
|
const options = useMemo< SelectOption[] >( () => {
|
|
|
|
if ( countryStates && Object.keys( countryStates ).length > 0 ) {
|
2024-07-26 16:38:44 +00:00
|
|
|
return Object.keys( countryStates ).map( ( key ) => ( {
|
|
|
|
value: key,
|
|
|
|
label: decodeEntities( countryStates[ key ] ),
|
|
|
|
} ) );
|
2024-07-24 15:59:15 +00:00
|
|
|
}
|
|
|
|
return [];
|
2024-07-26 16:38:44 +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 ) => {
|
2023-08-09 17:24:51 +00:00
|
|
|
const newValue =
|
2021-09-21 10:48:39 +00:00
|
|
|
options.length > 0
|
|
|
|
? optionMatcher( stateValue, options )
|
2023-08-09 17:24:51 +00:00
|
|
|
: stateValue;
|
|
|
|
if ( newValue !== value ) {
|
|
|
|
onChange( newValue );
|
|
|
|
}
|
2020-02-19 15:10:26 +00:00
|
|
|
},
|
2023-08-09 17:24:51 +00:00
|
|
|
[ onChange, options, value ]
|
2020-02-19 15:10:26 +00:00
|
|
|
);
|
|
|
|
|
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 (
|
2024-07-26 16:38:44 +00:00
|
|
|
<Select
|
2024-07-24 15:59:15 +00:00
|
|
|
className={ clsx(
|
|
|
|
className,
|
2024-07-26 16:38:44 +00:00
|
|
|
'wc-block-components-state-input'
|
2024-07-24 15:59:15 +00:00
|
|
|
) }
|
2024-07-26 16:38:44 +00:00
|
|
|
options={ options }
|
|
|
|
label={ label || '' }
|
|
|
|
id={ id }
|
|
|
|
onChange={ onChangeState }
|
|
|
|
value={ value }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
required={ required }
|
|
|
|
/>
|
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;
|