Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Create County Input
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Feb 13, 2020
1 parent bb1efa4 commit 884c8ee
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
23 changes: 23 additions & 0 deletions assets/js/base/components/county-input/billing-county-input.js
Original file line number Diff line number Diff line change
@@ -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 <CountyInput counties={ ALLOWED_COUNTIES } { ...props } />;
};

BillingCountyInput.propTypes = {
onChange: PropTypes.func.isRequired,
className: PropTypes.string,
label: PropTypes.string,
value: PropTypes.string,
};

export default BillingCountyInput;
61 changes: 61 additions & 0 deletions assets/js/base/components/county-input/county-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import Select from '../select';

const CountyInput = ( {
className,
counties,
country,
label,
onChange,
value = '',
} ) => {
const countryCounties = counties[ country ];
if ( ! countryCounties || Object.keys( countryCounties ).length === 0 ) {
return null;
}

const options = Object.keys( countryCounties ).map( ( key ) => ( {
key,
name: decodeEntities( countryCounties[ key ] ),
} ) );
const formattedValue = countryCounties[ value ]
? {
key: value,
name: decodeEntities( countryCounties[ value ] ),
}
: null;

return (
<Select
className={ className }
label={ label }
onChange={ onChange }
options={ options }
value={ formattedValue }
/>
);
};

CountyInput.propTypes = {
counties: PropTypes.objectOf(
PropTypes.oneOfType( [
PropTypes.array,
PropTypes.objectOf( PropTypes.string ),
] )
).isRequired,
onChange: PropTypes.func.isRequired,
className: PropTypes.string,
country: PropTypes.string,
label: PropTypes.string,
value: PropTypes.string,
};

export default CountyInput;
3 changes: 3 additions & 0 deletions assets/js/base/components/county-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as CountyInput } from './county-input';
export { default as BillingCountyInput } from './billing-county-input';
export { default as ShippingCountyInput } from './shipping-county-input';
23 changes: 23 additions & 0 deletions assets/js/base/components/county-input/shipping-county-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { SHIPPING_COUNTIES } from '@woocommerce/block-settings';

/**
* Internal dependencies
*/
import CountyInput from './county-input.js';

const ShippingCountyInput = ( props ) => {
return <CountyInput counties={ SHIPPING_COUNTIES } { ...props } />;
};

ShippingCountyInput.propTypes = {
onChange: PropTypes.func.isRequired,
className: PropTypes.string,
label: PropTypes.string,
value: PropTypes.string,
};

export default ShippingCountyInput;
4 changes: 3 additions & 1 deletion assets/js/blocks/cart-checkout/checkout/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CheckoutForm from '@woocommerce/base-components/checkout/form';
import NoShipping from '@woocommerce/base-components/checkout/no-shipping';
import TextInput from '@woocommerce/base-components/text-input';
import { ShippingCountryInput } from '@woocommerce/base-components/country-input';
import { ShippingCountyInput } from '@woocommerce/base-components/county-input';
import RadioControl from '@woocommerce/base-components/radio-control';
import InputRow from '@woocommerce/base-components/input-row';
import { CheckboxControl } from '@wordpress/components';
Expand Down Expand Up @@ -208,7 +209,8 @@ const Block = ( { shippingMethods = [], isEditor = false } ) => {
/>
</InputRow>
<InputRow>
<TextInput
<ShippingCountyInput
country={ shippingFields.country }
label={ __(
'County',
'woo-gutenberg-products-block'
Expand Down
2 changes: 2 additions & 0 deletions assets/js/settings/blocks/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ export const ATTRIBUTES = getSetting( 'attributes', [] );
export const WC_BLOCKS_ASSET_URL = getSetting( 'wcBlocksAssetUrl', '' );
export const SHIPPING_COUNTRIES = getSetting( 'shippingCountries', {} );
export const ALLOWED_COUNTRIES = getSetting( 'allowedCountries', {} );
export const SHIPPING_COUNTIES = getSetting( 'shippingCounties', {} );
export const ALLOWED_COUNTIES = getSetting( 'allowedCounties', {} );
2 changes: 2 additions & 0 deletions src/BlockTypes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function render( $attributes = array(), $content = '' ) {
);
$data_registry->add( 'allowedCountries', WC()->countries->get_allowed_countries() );
$data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() );
$data_registry->add( 'allowedCounties', WC()->countries->get_allowed_country_states() );
$data_registry->add( 'shippingCounties', WC()->countries->get_shipping_country_states() );
\Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' );
return $content;
}
Expand Down

0 comments on commit 884c8ee

Please sign in to comment.