@navinc/base-react-components
Version:
Nav's Pattern Library
72 lines • 5.5 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressInput = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const utils_1 = require("@navinc/utils");
const react_1 = require("react");
const styled_components_1 = __importDefault(require("styled-components"));
const copy_js_1 = require("../copy.js");
const shared_js_1 = require("./shared.js");
const handlePreventSubmitOnEnter = (keyDownEvent) => {
if (keyDownEvent.keyCode === 13)
keyDownEvent.preventDefault();
};
const _AddressInput = (_a) => {
var { className, label, hasSpaceForErrors, isInvalid, value, required, type, errors = [], lede = '', onAddressSelected = utils_1.noop, touched, placeholder = 'Enter a location', isPrivate = true } = _a, props = __rest(_a, ["className", "label", "hasSpaceForErrors", "isInvalid", "value", "required", "type", "errors", "lede", "onAddressSelected", "touched", "placeholder", "isPrivate"]);
const inputRef = (0, react_1.useRef)();
(0, react_1.useEffect)(() => {
var _a, _b, _c, _d;
Promise.resolve((_d = (_c = (_b = (_a = utils_1.global === null || utils_1.global === void 0 ? void 0 : utils_1.global.google) === null || _a === void 0 ? void 0 : _a.maps) === null || _b === void 0 ? void 0 : _b.places) === null || _c === void 0 ? void 0 : _c.Autocomplete) !== null && _d !== void 0 ? _d : (0, utils_1.loadScript)('https://maps.googleapis.com/maps/api/js?key=AIzaSyAvfE0mS4qeMdHm2fsIfE2XowJj6XG2pW8&libraries=places').then(() => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = utils_1.global === null || utils_1.global === void 0 ? void 0 : utils_1.global.google) === null || _a === void 0 ? void 0 : _a.maps) === null || _b === void 0 ? void 0 : _b.places) === null || _c === void 0 ? void 0 : _c.Autocomplete) !== null && _d !== void 0 ? _d : Promise.reject(new Error('Error loading GoogleMaps script')); }))
.then((Autocomplete) => {
const autocomplete = new Autocomplete(inputRef.current, {
types: ['address'],
componentRestrictions: { country: 'us' },
});
autocomplete.addListener('place_changed', () => {
const place = autocomplete.getPlace();
// bail if there is a problem getting data from the api
if (!place || !Array.isArray(place.address_components))
return;
const { locality: city = '', sublocality_level_1: subcity = '', administrative_area_level_1: state = '', postal_code: zip = '', street_number: streetNumber = '', route: street = '', street1 = `${streetNumber} ${street}`.trim(), country = '', } = Object.assign({}, ...place.address_components
.filter(({ types: [type] }) => [
'street_number',
'route',
'locality',
'sublocality_level_1',
'administrative_area_level_1',
'postal_code',
'country',
].includes(type))
.map(({ types: [type], long_name: longName, short_name: shortName }) => ({
[type]: ['route', 'locality', 'sublocality_level_1', 'country'].includes(type) ? longName : shortName,
})));
onAddressSelected({
city: city || subcity,
state,
street1,
zip,
country: country === 'United States' ? 'United States of America' : country,
});
});
})
.catch((err) => console.error(err));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return ((0, jsx_runtime_1.jsxs)(shared_js_1.FieldWrapper, { className: className, children: [lede && (0, jsx_runtime_1.jsx)(copy_js_1.Copy, { bold: true, children: lede }), (0, jsx_runtime_1.jsxs)(shared_js_1.Field, Object.assign({}, (isPrivate && { className: 'js-private', 'data-heap-redact-text': 'true' }), { isVisited: touched || !!value || !!placeholder, onKeyDown: handlePreventSubmitOnEnter, children: [(0, jsx_runtime_1.jsx)(shared_js_1.Input, Object.assign({ ref: inputRef, type: type, required: required, value: value, isInvalid: isInvalid, placeholder: placeholder, "data-testid": `input:${props.name}` }, props)), (0, jsx_runtime_1.jsx)(shared_js_1.Label, { required: required, children: label })] })), (0, jsx_runtime_1.jsx)(shared_js_1.Errors, { hasSpaceForErrors: hasSpaceForErrors, children: !!errors.length && errors.map((err, i) => (0, jsx_runtime_1.jsx)(shared_js_1.Err, { children: err }, `err-${i}`)) })] }));
};
exports.AddressInput = (0, styled_components_1.default)(_AddressInput).withConfig({ displayName: "brc-sc-AddressInput", componentId: "brc-sc-h7fx4m" }) ``;
//# sourceMappingURL=address-input.js.map