Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 58x 58x 514x 18x 24x 58x 58x 58x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 755x 755x 755x 16x 739x 58x 58x 58x 18x 18x 18x 18x 18x 204x 18x 6x 18x 24x 24x | import { Address, Country, FormField } from '@bigcommerce/checkout-sdk'; import { memoize } from '@bigcommerce/memoize'; import { forIn, noop } from 'lodash'; import React, { createRef, Component, ReactNode, RefObject } from 'react'; import { withLanguage, TranslatedString, WithLanguageProps } from '../locale'; import { AutocompleteItem } from '../ui/autocomplete'; import { CheckboxFormField, DynamicFormField, DynamicFormFieldType, Fieldset } from '../ui/form'; import { AddressKeyMap } from './address'; import { getAddressFormFieldInputId, getAddressFormFieldLegacyName } from './getAddressFormFieldInputId'; import { mapToAddress, GoogleAutocompleteFormField } from './googleAutocomplete'; import './AddressForm.scss'; export interface AddressFormProps { fieldName?: string; countryCode?: string; countriesWithAutocomplete?: string[]; countries?: Country[]; formFields: FormField[]; googleMapsApiKey?: string; shouldShowSaveAddress?: boolean; onAutocompleteSelect?(address: Partial<Address>): void; onAutocompleteToggle?(state: { inputValue: string; isOpen: boolean }): void; onChange?(fieldName: string, value: string | string[]): void; setFieldValue?(fieldName: string, value: string | string[]): void; } const LABEL: AddressKeyMap = { address1: 'address.address_line_1_label', address2: 'address.address_line_2_label', city: 'address.city_label', company: 'address.company_name_label', countryCode: 'address.country_label', firstName: 'address.first_name_label', lastName: 'address.last_name_label', phone: 'address.phone_number_label', postalCode: 'address.postal_code_label', stateOrProvince: 'address.state_label', stateOrProvinceCode: 'address.state_label', }; const AUTOCOMPLETE: AddressKeyMap = { address1: 'address-line1', address2: 'address-line2', city: 'address-level2', company: 'organization', countryCode: 'country', firstName: 'given-name', lastName: 'family-name', phone: 'tel', postalCode: 'postal-code', stateOrProvince: 'address-level1', stateOrProvinceCode: 'address-level1', }; const PLACEHOLDER: AddressKeyMap = { countryCode: 'address.select_country_action', stateOrProvince: 'address.select_state_action', stateOrProvinceCode: 'address.select_state_action', }; const AUTOCOMPLETE_FIELD_NAME = 'address1'; class AddressForm extends Component<AddressFormProps & WithLanguageProps> { private containerRef: RefObject<HTMLElement> = createRef(); private nextElement?: HTMLElement | null; private handleDynamicFormFieldChange: (name: string) => (value: string | string[]) => void = memoize(name => value => { this.syncNonFormikValue(name, value); }); componentDidMount(): void { const { current } = this.containerRef; Eif (current) { this.nextElement = current.querySelector<HTMLElement>('[autocomplete="address-line2"]'); } } render(): ReactNode { const { formFields, fieldName, language, countriesWithAutocomplete, countryCode, googleMapsApiKey, onAutocompleteToggle, shouldShowSaveAddress, } = this.props; return (<> <Fieldset> <div className="checkout-address" ref={ this.containerRef as RefObject<HTMLDivElement> }> { formFields.map(field => { const addressFieldName = field.name; const translatedPlaceholderId = PLACEHOLDER[addressFieldName]; if (addressFieldName === 'address1' && googleMapsApiKey && countriesWithAutocomplete) { return ( <GoogleAutocompleteFormField apiKey={ googleMapsApiKey } countryCode={ countryCode } field={ field } key={ field.id } nextElement={ this.nextElement || undefined } onChange={ this.handleAutocompleteChange } onSelect={ this.handleAutocompleteSelect } onToggleOpen={ onAutocompleteToggle } parentFieldName={ fieldName } supportedCountries={ countriesWithAutocomplete } /> ); } return ( <DynamicFormField autocomplete={ AUTOCOMPLETE[field.name] } extraClass={ `dynamic-form-field--${getAddressFormFieldLegacyName(addressFieldName)}` } field={ field } inputId={ getAddressFormFieldInputId(addressFieldName) } // stateOrProvince can sometimes be a dropdown or input, so relying on id is not sufficient key={ `${field.id}-${field.name}` } label={ field.custom ? field.label : <TranslatedString id={ LABEL[field.name] } /> } onChange={ this.handleDynamicFormFieldChange(addressFieldName) } parentFieldName={ field.custom ? (fieldName ? `${fieldName}.customFields` : 'customFields') : fieldName } placeholder={ translatedPlaceholderId && language.translate(translatedPlaceholderId) } /> ); }) } </div> </Fieldset> { shouldShowSaveAddress && <CheckboxFormField labelContent={ <TranslatedString id="address.save_in_addressbook" /> } name={ fieldName ? `${fieldName}.shouldSaveAddress` : 'shouldSaveAddress' } /> } </>); } private handleAutocompleteChange: (value: string, isOpen: boolean) => void = (value, isOpen) => { if (!isOpen) { this.syncNonFormikValue(AUTOCOMPLETE_FIELD_NAME, value); } }; private handleAutocompleteSelect: ( place: google.maps.places.PlaceResult, item: AutocompleteItem ) => void = (place, { value: autocompleteValue }) => { const { countries, setFieldValue = noop, onChange = noop, } = this.props; const address = mapToAddress(place, countries); forIn(address, (value, fieldName) => { setFieldValue(fieldName, value as string); onChange(fieldName, value as string); }); if (autocompleteValue) { this.syncNonFormikValue(AUTOCOMPLETE_FIELD_NAME, autocompleteValue); } }; // because autocomplete state is controlled by Downshift, we need to manually keep formik // value in sync when autocomplete value changes private syncNonFormikValue: ( fieldName: string, value: string | string[] ) => void = (fieldName, value) => { const { formFields, setFieldValue = noop, onChange = noop, } = this.props; const dateFormFieldNames = formFields .filter(field => field.custom && field.fieldType === DynamicFormFieldType.date) .map(field => field.name); if (fieldName === AUTOCOMPLETE_FIELD_NAME || dateFormFieldNames.indexOf(fieldName) > -1) { setFieldValue(fieldName, value); } onChange(fieldName, value); }; } export default withLanguage(AddressForm); |