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 PropTypes from 'prop-types';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-03-10 12:27:58 +00:00
|
|
|
import { useCallback, useEffect } from '@wordpress/element';
|
2020-02-14 12:30:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-23 11:22:00 +00:00
|
|
|
import { ValidatedTextInput } from '../text-input';
|
|
|
|
import { ValidatedSelect } from '../select';
|
2020-02-14 12:30:33 +00:00
|
|
|
|
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,
|
2020-02-14 12:30:33 +00:00
|
|
|
} ) => {
|
2020-02-19 17:14:41 +00:00
|
|
|
const countryStates = states[ country ];
|
2020-03-03 10:46:53 +00:00
|
|
|
const options = countryStates
|
|
|
|
? Object.keys( countryStates ).map( ( key ) => ( {
|
|
|
|
key,
|
|
|
|
name: decodeEntities( countryStates[ key ] ),
|
|
|
|
} ) )
|
2020-03-06 15:40:07 +00:00
|
|
|
: [];
|
2020-03-10 12:27:58 +00:00
|
|
|
// @todo: remove this code block when issue https://github.com/woocommerce/woocommerce/issues/25854 is merged
|
|
|
|
// Defaults to the first state when selecting a country with states, this is here
|
|
|
|
// until a bug in Woo core is fixed.
|
|
|
|
// see: https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/1919
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! value && options.length ) {
|
|
|
|
onChangeState( options[ 0 ].key );
|
|
|
|
}
|
|
|
|
}, [ country ] );
|
2020-02-19 15:10:26 +00:00
|
|
|
/**
|
|
|
|
* Handles state selection onChange events. Finds a matching state by key or value.
|
|
|
|
*
|
|
|
|
* @param {Object} event event data.
|
|
|
|
*/
|
|
|
|
const onChangeState = useCallback(
|
|
|
|
( stateValue ) => {
|
|
|
|
if ( options.length > 0 ) {
|
|
|
|
const foundOption = options.find(
|
|
|
|
( option ) =>
|
|
|
|
option.key === stateValue || option.name === stateValue
|
|
|
|
);
|
|
|
|
|
|
|
|
onChange( foundOption ? foundOption.key : '' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange( stateValue );
|
|
|
|
},
|
|
|
|
[ onChange, options ]
|
|
|
|
);
|
|
|
|
|
2020-03-06 15:40:07 +00:00
|
|
|
if ( options.length > 0 ) {
|
2020-03-03 10:46:53 +00:00
|
|
|
return (
|
2020-03-06 15:40:07 +00:00
|
|
|
<>
|
2020-03-23 11:22:00 +00:00
|
|
|
<ValidatedSelect
|
2020-03-06 15:40:07 +00:00
|
|
|
className={ className }
|
2020-04-02 09:27:54 +00:00
|
|
|
id={ id }
|
2020-03-06 15:40:07 +00:00
|
|
|
label={ label }
|
|
|
|
onChange={ onChangeState }
|
|
|
|
options={ options }
|
|
|
|
value={ options.find( ( option ) => option.key === value ) }
|
2020-03-23 11:22:00 +00:00
|
|
|
errorMessage={ __(
|
|
|
|
'Please select a state.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-06 15:40:07 +00:00
|
|
|
required={ required }
|
|
|
|
/>
|
|
|
|
{ autoComplete !== 'off' && (
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
aria-hidden={ true }
|
|
|
|
autoComplete={ autoComplete }
|
|
|
|
value={ value }
|
|
|
|
onChange={ ( event ) =>
|
|
|
|
onChangeState( event.target.value )
|
|
|
|
}
|
|
|
|
style={ {
|
2020-03-13 12:02:08 +00:00
|
|
|
minHeight: '0',
|
2020-03-06 15:40:07 +00:00
|
|
|
height: '0',
|
|
|
|
border: '0',
|
|
|
|
padding: '0',
|
|
|
|
position: 'absolute',
|
|
|
|
} }
|
|
|
|
tabIndex={ -1 }
|
2020-03-03 10:46:53 +00:00
|
|
|
/>
|
2020-03-06 15:40:07 +00:00
|
|
|
) }
|
|
|
|
</>
|
2020-03-03 10:46:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2020-03-23 11:22:00 +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
|
|
|
StateInput.propTypes = {
|
2020-02-19 17:14:41 +00:00
|
|
|
states: PropTypes.objectOf(
|
2020-02-14 12:30:33 +00:00
|
|
|
PropTypes.oneOfType( [
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.objectOf( PropTypes.string ),
|
|
|
|
] )
|
|
|
|
).isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2020-02-19 15:10:26 +00:00
|
|
|
autoComplete: PropTypes.string,
|
2020-04-02 09:27:54 +00:00
|
|
|
id: PropTypes.string,
|
2020-02-14 12:30:33 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
country: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:12:15 +00:00
|
|
|
export default StateInput;
|