From 69f49760a8f27d5a04978c86117bf0f449fbe559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 14 Feb 2020 13:30:33 +0100 Subject: [PATCH] Create CountyInput component (https://github.com/woocommerce/woocommerce-blocks/pull/1727) * Split CountryInput and Select * Create County Input * Show text input when there are no county options * Reset county value when changing country * Fix keyboard navigation * Hide checkmark * Add reset styles for several popular themes * Add country prop to ShippingCountyInput --- .../components/country-input/country-input.js | 19 ++---- .../county-input/billing-county-input.js | 23 +++++++ .../components/county-input/county-input.js | 63 +++++++++++++++++++ .../js/base/components/county-input/index.js | 3 + .../county-input/shipping-county-input.js | 23 +++++++ .../js/base/components/input-row/style.scss | 2 +- .../assets/js/base/components/select/index.js | 45 +++++++++++++ .../{country-input => select}/style.scss | 12 +++- .../js/blocks/cart-checkout/checkout/block.js | 5 +- .../assets/js/settings/blocks/constants.js | 2 + .../src/BlockTypes/Checkout.php | 2 + 11 files changed, 182 insertions(+), 17 deletions(-) create mode 100644 plugins/woocommerce-blocks/assets/js/base/components/county-input/billing-county-input.js create mode 100644 plugins/woocommerce-blocks/assets/js/base/components/county-input/county-input.js create mode 100644 plugins/woocommerce-blocks/assets/js/base/components/county-input/index.js create mode 100644 plugins/woocommerce-blocks/assets/js/base/components/county-input/shipping-county-input.js create mode 100644 plugins/woocommerce-blocks/assets/js/base/components/select/index.js rename plugins/woocommerce-blocks/assets/js/base/components/{country-input => select}/style.scss (80%) 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 d5beb9ee0b2..7038b1648ee 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 @@ -2,14 +2,12 @@ * External dependencies */ import PropTypes from 'prop-types'; -import classnames from 'classnames'; -import { CustomSelectControl } from 'wordpress-components'; import { decodeEntities } from '@wordpress/html-entities'; /** * Internal dependencies */ -import './style.scss'; +import Select from '../select'; const CountryInput = ( { className, @@ -24,19 +22,12 @@ const CountryInput = ( { } ) ); return ( - { - onChange( selectedItem.key ); - } } - value={ { - key: value, - name: decodeEntities( countries[ value ] ), - } } + value={ options.find( ( option ) => option.key === value ) } /> ); }; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/county-input/billing-county-input.js b/plugins/woocommerce-blocks/assets/js/base/components/county-input/billing-county-input.js new file mode 100644 index 00000000000..c01a1ca0675 --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/base/components/county-input/billing-county-input.js @@ -0,0 +1,23 @@ +/** + * External dependencies + */ +import PropTypes from 'prop-types'; +import { ALLOWED_COUNTIES } from '@woocommerce/block-settings'; + +/** + * Internal dependencies + */ +import CountyInput from './county-input.js'; + +const BillingCountyInput = ( props ) => { + return ; +}; + +BillingCountyInput.propTypes = { + onChange: PropTypes.func.isRequired, + className: PropTypes.string, + label: PropTypes.string, + value: PropTypes.string, +}; + +export default BillingCountyInput; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/county-input/county-input.js b/plugins/woocommerce-blocks/assets/js/base/components/county-input/county-input.js new file mode 100644 index 00000000000..bf7ca001b12 --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/base/components/county-input/county-input.js @@ -0,0 +1,63 @@ +/** + * External dependencies + */ +import PropTypes from 'prop-types'; +import { decodeEntities } from '@wordpress/html-entities'; + +/** + * Internal dependencies + */ +import TextInput from '../text-input'; +import Select from '../select'; + +const CountyInput = ( { + className, + counties, + country, + label, + onChange, + value = '', +} ) => { + const countryCounties = counties[ country ]; + if ( ! countryCounties || Object.keys( countryCounties ).length === 0 ) { + return ( + + ); + } + + const options = Object.keys( countryCounties ).map( ( key ) => ( { + key, + name: decodeEntities( countryCounties[ key ] ), + } ) ); + + return ( +