diff --git a/plugins/woocommerce-blocks/assets/js/base/components/country-input/country-input.js b/plugins/woocommerce-blocks/assets/js/base/components/country-input/country-input.js index 7038b1648ee..54fe973fca4 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/country-input/country-input.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/country-input/country-input.js @@ -15,6 +15,7 @@ const CountryInput = ( { label, onChange, value = '', + autoComplete = 'off', } ) => { const options = Object.keys( countries ).map( ( key ) => ( { key, @@ -22,13 +23,36 @@ const CountryInput = ( { } ) ); return ( - option.key === value ) } + /> + { autoComplete !== 'off' && ( + { + const textValue = event.target.value; + const foundOption = options.find( + ( option ) => option.key === textValue + ); + onChange( foundOption ? foundOption.key : '' ); + } } + style={ { + height: '0', + border: '0', + padding: '0', + position: 'absolute', + } } + /> + ) } + ); }; @@ -38,6 +62,7 @@ CountryInput.propTypes = { className: PropTypes.string, label: PropTypes.string, value: PropTypes.string, + autoComplete: PropTypes.string, }; export default CountryInput; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/shipping-calculator/address.js b/plugins/woocommerce-blocks/assets/js/base/components/shipping-calculator/address.js index e54214652fe..eee06d34926 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/shipping-calculator/address.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/shipping-calculator/address.js @@ -19,7 +19,7 @@ const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate } ) => { const [ address, setAddress ] = useState( initialAddress ); return ( -
+
{ 'woo-gutenberg-products-block' ) } value={ address.country } + autoComplete="country" onChange={ ( newValue ) => setAddress( { ...address, @@ -40,6 +41,7 @@ const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate } ) => { country={ address.country } label={ __( 'State / County', 'woo-gutenberg-products-block' ) } value={ address.state } + autoComplete="address-level1" onChange={ ( newValue ) => setAddress( { ...address, @@ -51,6 +53,7 @@ const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate } ) => { className="wc-block-shipping-calculator-address__input" label={ __( 'City', 'woo-gutenberg-products-block' ) } value={ address.city } + autoComplete="address-level2" onChange={ ( newValue ) => setAddress( { ...address, @@ -62,6 +65,7 @@ const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate } ) => { className="wc-block-shipping-calculator-address__input" label={ __( 'Postal code', 'woo-gutenberg-products-block' ) } value={ address.postcode } + autoComplete="postal-code" onChange={ ( newValue ) => setAddress( { ...address, @@ -76,7 +80,7 @@ const ShippingCalculatorAddress = ( { address: initialAddress, onUpdate } ) => { > { __( 'Update', 'woo-gutenberg-products-block' ) } -
+ ); }; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/state-input/state-input.js b/plugins/woocommerce-blocks/assets/js/base/components/state-input/state-input.js index bec20282e05..fe29ac8e2d2 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/state-input/state-input.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/state-input/state-input.js @@ -3,6 +3,7 @@ */ import PropTypes from 'prop-types'; import { decodeEntities } from '@wordpress/html-entities'; +import { useCallback } from '@wordpress/element'; /** * Internal dependencies @@ -16,32 +17,73 @@ const StateInput = ( { country, label, onChange, + autoComplete = 'off', value = '', } ) => { const countryCounties = counties[ country ]; - if ( ! countryCounties || Object.keys( countryCounties ).length === 0 ) { - return ( - 0 + ? Object.keys( countryCounties ).map( ( key ) => ( { + key, + name: decodeEntities( countryCounties[ key ] ), + } ) ) + : []; + + /** + * 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 ] + ); + + return options.length > 0 ? ( + <> + + onChangeState( event.target.value ) + } + style={ { + height: '0', + border: '0', + padding: '0', + position: 'absolute', + } } + /> + ) } + + ) : ( + option.key === value ) } + onChange={ onChangeState } + autoComplete={ autoComplete } + value={ value } /> ); }; @@ -54,6 +96,7 @@ StateInput.propTypes = { ] ) ).isRequired, onChange: PropTypes.func.isRequired, + autoComplete: PropTypes.string, className: PropTypes.string, country: PropTypes.string, label: PropTypes.string, diff --git a/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js b/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js index 7d4018a1757..5c568af0a9c 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js +++ b/plugins/woocommerce-blocks/assets/js/base/components/text-input/index.js @@ -22,12 +22,13 @@ const TextInput = ( { screenReaderLabel, disabled, help, + autoComplete = 'off', value = '', onChange, } ) => { const [ isActive, setIsActive ] = useState( false ); const onChangeValue = ( event ) => onChange( event.target.value ); - const textInputId = id || componentId; + const textInputId = id || 'textinput-' + componentId; return (
-