2020-01-17 16:58:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-09-03 08:02:26 +00:00
|
|
|
import { useMemo } from '@wordpress/element';
|
2020-01-17 16:58:08 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2020-01-17 16:58:08 +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 { CountryInputWithCountriesProps } from './CountryInputProps';
|
2024-07-24 15:59:15 +00:00
|
|
|
import { Select, SelectOption } from '../select';
|
2020-01-17 16:58:08 +00:00
|
|
|
|
2021-12-07 15:07:21 +00:00
|
|
|
export const CountryInput = ( {
|
2020-01-17 16:58:08 +00:00
|
|
|
className,
|
|
|
|
countries,
|
2020-04-02 09:27:54 +00:00
|
|
|
id,
|
2020-01-17 16:58:08 +00:00
|
|
|
label,
|
|
|
|
onChange,
|
|
|
|
value = '',
|
2020-02-19 15:10:26 +00:00
|
|
|
autoComplete = 'off',
|
2020-03-03 10:46:53 +00:00
|
|
|
required = false,
|
2021-06-17 08:35:24 +00:00
|
|
|
}: CountryInputWithCountriesProps ): JSX.Element => {
|
2024-07-24 15:59:15 +00:00
|
|
|
const options = useMemo< SelectOption[] >( () => {
|
2024-07-26 16:38:44 +00:00
|
|
|
return Object.entries( countries ).map(
|
|
|
|
( [ countryCode, countryName ] ) => ( {
|
|
|
|
value: countryCode,
|
|
|
|
label: decodeEntities( countryName ),
|
|
|
|
} )
|
2024-07-24 15:59:15 +00:00
|
|
|
);
|
|
|
|
}, [ countries ] );
|
|
|
|
|
2020-01-17 16:58:08 +00:00
|
|
|
return (
|
2024-07-26 16:38:44 +00:00
|
|
|
<Select
|
|
|
|
className={ clsx( className, 'wc-block-components-country-input' ) }
|
|
|
|
id={ id }
|
|
|
|
label={ label || '' }
|
|
|
|
onChange={ onChange }
|
|
|
|
options={ options }
|
|
|
|
value={ value }
|
|
|
|
required={ required }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
/>
|
2020-01-17 16:58:08 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CountryInput;
|