2020-01-17 16:58:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-09-03 08:02:26 +00:00
|
|
|
import { useMemo } from '@wordpress/element';
|
2020-03-23 11:22:00 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-01-17 16:58:08 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-03-17 11:45:33 +00:00
|
|
|
import classnames from 'classnames';
|
2020-01-17 16:58:08 +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 { CountryInputWithCountriesProps } from './CountryInputProps';
|
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,
|
2020-03-23 11:22:00 +00:00
|
|
|
errorId,
|
|
|
|
errorMessage = __(
|
|
|
|
'Please select a country.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
2021-06-17 08:35:24 +00:00
|
|
|
}: CountryInputWithCountriesProps ): JSX.Element => {
|
2020-09-03 08:02:26 +00:00
|
|
|
const options = useMemo(
|
|
|
|
() =>
|
2021-12-07 15:07:21 +00:00
|
|
|
Object.entries( countries ).map(
|
|
|
|
( [ countryCode, countryName ] ) => ( {
|
|
|
|
value: countryCode,
|
|
|
|
label: decodeEntities( countryName ),
|
|
|
|
} )
|
|
|
|
),
|
2020-09-03 08:02:26 +00:00
|
|
|
[ countries ]
|
|
|
|
);
|
2020-01-17 16:58:08 +00:00
|
|
|
|
|
|
|
return (
|
2020-06-17 09:53:42 +00:00
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
className,
|
|
|
|
'wc-block-components-country-input'
|
|
|
|
) }
|
|
|
|
>
|
2021-08-12 16:30:42 +00:00
|
|
|
<Combobox
|
2020-04-02 09:27:54 +00:00
|
|
|
id={ id }
|
2020-02-19 15:10:26 +00:00
|
|
|
label={ label }
|
|
|
|
onChange={ onChange }
|
|
|
|
options={ options }
|
2021-08-12 16:30:42 +00:00
|
|
|
value={ value }
|
2020-03-23 11:22:00 +00:00
|
|
|
errorId={ errorId }
|
|
|
|
errorMessage={ errorMessage }
|
2020-03-03 10:46:53 +00:00
|
|
|
required={ required }
|
2021-08-12 16:30:42 +00:00
|
|
|
autoComplete={ autoComplete }
|
2020-02-19 15:10:26 +00:00
|
|
|
/>
|
|
|
|
{ autoComplete !== 'off' && (
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
aria-hidden={ true }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
value={ value }
|
|
|
|
onChange={ ( event ) => {
|
2022-06-15 09:56:52 +00:00
|
|
|
const textValue =
|
|
|
|
event.target.value.toLocaleUpperCase();
|
2020-02-19 15:10:26 +00:00
|
|
|
const foundOption = options.find(
|
2021-08-12 16:30:42 +00:00
|
|
|
( option ) =>
|
|
|
|
( textValue.length !== 2 &&
|
|
|
|
option.label.toLocaleUpperCase() ===
|
|
|
|
textValue ) ||
|
|
|
|
( textValue.length === 2 &&
|
|
|
|
option.value.toLocaleUpperCase() ===
|
|
|
|
textValue )
|
2020-02-19 15:10:26 +00:00
|
|
|
);
|
2021-08-12 16:30:42 +00:00
|
|
|
onChange( foundOption ? foundOption.value : '' );
|
2020-02-19 15:10:26 +00:00
|
|
|
} }
|
|
|
|
style={ {
|
2020-03-13 12:02:08 +00:00
|
|
|
minHeight: '0',
|
2020-02-19 15:10:26 +00:00
|
|
|
height: '0',
|
|
|
|
border: '0',
|
|
|
|
padding: '0',
|
|
|
|
position: 'absolute',
|
|
|
|
} }
|
2020-03-04 14:35:18 +00:00
|
|
|
tabIndex={ -1 }
|
2020-02-19 15:10:26 +00:00
|
|
|
/>
|
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
</div>
|
2020-01-17 16:58:08 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CountryInput;
|