2020-03-03 10:46:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
2020-03-04 15:13:38 +00:00
|
|
|
import {
|
|
|
|
BillingCountryInput,
|
|
|
|
ShippingCountryInput,
|
|
|
|
} from '@woocommerce/base-components/country-input';
|
|
|
|
import {
|
|
|
|
BillingStateInput,
|
|
|
|
ShippingStateInput,
|
|
|
|
} from '@woocommerce/base-components/state-input';
|
2020-03-03 10:46:53 +00:00
|
|
|
import {
|
|
|
|
COUNTRY_LOCALE,
|
|
|
|
DEFAULT_ADDRESS_FIELDS,
|
|
|
|
} from '@woocommerce/block-settings';
|
|
|
|
|
2020-03-04 15:13:38 +00:00
|
|
|
const defaultFieldNames = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'company',
|
|
|
|
'address_1',
|
|
|
|
'address_2',
|
|
|
|
'country',
|
|
|
|
'city',
|
|
|
|
'postcode',
|
|
|
|
'state',
|
|
|
|
];
|
|
|
|
|
2020-03-03 10:46:53 +00:00
|
|
|
const defaultFieldValues = {
|
|
|
|
first_name: {
|
|
|
|
autocomplete: 'given-name',
|
|
|
|
},
|
|
|
|
last_name: {
|
|
|
|
autocomplete: 'family-name',
|
|
|
|
},
|
|
|
|
company: {
|
|
|
|
autocomplete: 'organization',
|
|
|
|
},
|
|
|
|
address_1: {
|
|
|
|
autocomplete: 'address-line1',
|
|
|
|
},
|
|
|
|
address_2: {
|
|
|
|
autocomplete: 'address-line2',
|
|
|
|
},
|
|
|
|
country: {
|
|
|
|
autocomplete: 'country',
|
|
|
|
priority: 65,
|
|
|
|
},
|
|
|
|
city: {
|
|
|
|
autocomplete: 'address-level2',
|
|
|
|
},
|
|
|
|
postcode: {
|
|
|
|
autocomplete: 'postal-code',
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
autocomplete: 'address-level1',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddressForm = ( {
|
2020-03-04 15:13:38 +00:00
|
|
|
fields = defaultFieldNames,
|
2020-03-03 10:46:53 +00:00
|
|
|
onChange,
|
2020-03-04 15:13:38 +00:00
|
|
|
type = 'shipping',
|
2020-03-03 10:46:53 +00:00
|
|
|
values,
|
|
|
|
} ) => {
|
|
|
|
const countryLocale = COUNTRY_LOCALE[ values.country ] || {};
|
|
|
|
const addressFields = fields.map( ( field ) => ( {
|
|
|
|
key: field,
|
|
|
|
...DEFAULT_ADDRESS_FIELDS[ field ],
|
|
|
|
...countryLocale[ field ],
|
|
|
|
...defaultFieldValues[ field ],
|
|
|
|
} ) );
|
|
|
|
const sortedAddressFields = addressFields.sort(
|
|
|
|
( a, b ) => a.priority - b.priority
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="wc-block-address-form">
|
|
|
|
{ sortedAddressFields.map( ( addressField ) => {
|
|
|
|
if ( addressField.hidden ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( addressField.key === 'country' ) {
|
2020-03-04 15:13:38 +00:00
|
|
|
const Tag =
|
|
|
|
type === 'shipping'
|
|
|
|
? ShippingCountryInput
|
|
|
|
: BillingCountryInput;
|
2020-03-03 10:46:53 +00:00
|
|
|
return (
|
2020-03-04 15:13:38 +00:00
|
|
|
<Tag
|
2020-03-03 10:46:53 +00:00
|
|
|
key={ addressField.key }
|
|
|
|
label={ __(
|
|
|
|
'Country / Region',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ values.country }
|
|
|
|
autoComplete={ addressField.autocomplete }
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
onChange( {
|
|
|
|
...values,
|
|
|
|
country: newValue,
|
|
|
|
state: '',
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={ true }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( addressField.key === 'state' ) {
|
2020-03-04 15:13:38 +00:00
|
|
|
const Tag =
|
|
|
|
type === 'shipping'
|
|
|
|
? ShippingStateInput
|
|
|
|
: BillingStateInput;
|
2020-03-03 10:46:53 +00:00
|
|
|
return (
|
2020-03-04 15:13:38 +00:00
|
|
|
<Tag
|
2020-03-03 10:46:53 +00:00
|
|
|
key={ addressField.key }
|
|
|
|
country={ values.country }
|
|
|
|
label={ addressField.label }
|
|
|
|
value={ values.state }
|
|
|
|
autoComplete={ addressField.autocomplete }
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
onChange( {
|
|
|
|
...values,
|
|
|
|
state: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={
|
|
|
|
! values.country || addressField.required
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
key={ addressField.key }
|
|
|
|
className={ `wc-block-address-form__${ addressField.key }` }
|
|
|
|
label={ addressField.label || addressField.placeholder }
|
|
|
|
value={ values[ addressField.key ] }
|
|
|
|
autoComplete={ addressField.autocomplete }
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
onChange( {
|
|
|
|
...values,
|
|
|
|
[ addressField.key ]: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={ addressField.required }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
AddressForm.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
values: PropTypes.object.isRequired,
|
2020-03-04 15:13:38 +00:00
|
|
|
fields: PropTypes.arrayOf( PropTypes.oneOf( defaultFieldNames ) ),
|
|
|
|
type: PropTypes.oneOf( [ 'billing', 'shipping' ] ),
|
2020-03-03 10:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default AddressForm;
|