import React, {useState} from 'react'; import {useFormContext} from 'react-hook-form'; import './ShippingAddressForm.scss'; import 'shared/form_and_inputs.scss'; import {Link} from 'display-library/Link'; import ShippingAddressSelect from './ShippingAddressSelect'; import {FormProps} from 'checkout/ShippingAddressProps'; import ShippingAddressTextInput from './ShippingAddressTextInput'; import GooglePlacesAutocompleteWrapper from './GooglePlacesAutocompleteWrapper'; interface InputProps { label: string; } interface PostalCodeProps extends InputProps { validateUsPostalCode(code: string): boolean; } interface Address2LinkProps { onClick(bool: boolean): void; } interface PhoneProps extends InputProps { showChinese?: boolean; } const FirstName: React.FC = ({label}) => ( ); const LastName: React.FC = ({label}) => ( ); const City: React.FC = ({label}) => ( ); const Phone: React.FC = ({label, showChinese}) => { const validationMessage = 'Phone must be at least 7 digits'; return ( <>
Only used in case of delivery issues. {showChinese && ( <>
请提供电话号码,以便在需要时与您商讨订单和配送事宜。 )}
); }; const AddressLine2Link: React.FC = ({onClick}) => (
{ ev.preventDefault(); onClick(true); }} />
); const AddressLine2: React.FC = () => ( ); const PostalCode: React.FC = ({ label, validateUsPostalCode }) => { return ( validateUsPostalCode(code) || 'Please enter a valid postal code' }} /> ); }; const ShippingAddressForm: React.FC = ({ states, countries, handleSubmit, currentState, currentCountry, setCurrentState, setCurrentCountry }): JSX.Element => { const {reset} = useFormContext(); const [showAddressLine2, setShowAddressLine2] = useState(true); const showChinese = currentCountry.value === 'CN'; const country = (label: string) => ( reset()} options={countries} name="country_code" label={label} /> ); const dontAllowPoBoxes = (address: string) => { if ( ['QA', 'BH', 'JO', 'KW', 'LB', 'MP', 'SA', 'AE', 'OM'].includes( currentCountry.value ) ) return true; // match variations of "po box", "P.O. box", etc. const r1 = new RegExp( /(?:^|\s)p(?:\.-|\.\s|\.|-|\s)?o(?:\.-|\.\s|\.|-|\s)?b(?:ox)?(?:$|\s|\.)/i ); // matches "post office box" const r2 = new RegExp(/(?:^|\s)post office box(?:$|\s)/i); // matches "box", but not strings like "boxing" or "inbox" const r3 = new RegExp(/(?:^|\s)box(?:$|\s)/i); return !(r1.test(address) || r2.test(address) || r3.test(address)); }; const addressLine1 = (label: string) => ( dontAllowPoBoxes(address) || 'Please enter an address that is not a PO box', required: 'Required' }} name="address_line1" label={label} /> ); const validateUsPostalCode = (code: string) => { return currentCountry.value === 'US' ? !!code.match(/^\d\d\d\d\d(-\d\d\d\d)?$/) : true; }; const state = (label: string) => Object.keys(states).includes(currentCountry.value) ? ( ) : ( ); const englishInputs = ( <> {country('Country')} {addressLine1('Address')} {!showAddressLine2 && } {showAddressLine2 && }
{state('State/Region')}
); const chineseInputs = ( <>
为了简化并保证国际配送的通畅,请您用汉语拼音填写收件人信息和地址。
{country('Country or Region 国家/地区')}
{state('State/Region 省份')}
{addressLine1('Address 地址')} ); return (
{showChinese ? chineseInputs : englishInputs}
); }; export default ShippingAddressForm;