@dfds-ui/google-places 1.1.12-alpha.d54268bb → 1.1.12-alpha.f3c16129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ /// <reference types="react" />
2
+ import { ILocation, IPrediction } from './GooglePlaces';
3
+ export declare type AddressSelectProps = {
4
+ name: string;
5
+ disabled?: boolean;
6
+ error?: string;
7
+ size?: 'small' | 'medium';
8
+ types?: string[];
9
+ onFocus?: (event: FocusEvent) => void;
10
+ onBlur?: (event: FocusEvent) => void;
11
+ onChange?: (changed: {
12
+ value: string;
13
+ label: string;
14
+ }, predictions: IPrediction[]) => void;
15
+ location?: ILocation;
16
+ radius?: number;
17
+ countries?: string[] | string;
18
+ placeholder?: string;
19
+ assistiveText?: string;
20
+ autoFocus?: boolean;
21
+ isClearable?: boolean;
22
+ /**
23
+ * Class name to be assigned to the component
24
+ */
25
+ className?: string;
26
+ };
27
+ declare const AddressSelect: ({ name, disabled, error, size, types, onFocus, onBlur, onChange, location, radius, countries, ...rest }: AddressSelectProps) => JSX.Element;
28
+ export default AddressSelect;
@@ -0,0 +1,115 @@
1
+ var _excluded = ["name", "disabled", "error", "size", "types", "onFocus", "onBlur", "onChange", "location", "radius", "countries"];
2
+
3
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4
+
5
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6
+
7
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
8
+
9
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
10
+
11
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+
13
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
+
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
16
+
17
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
18
+
19
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
20
+
21
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
22
+
23
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
24
+
25
+ import React, { useState } from 'react';
26
+ import { AsyncSearchableSelect } from '@dfds-ui/react-components';
27
+ import debounce from 'lodash.debounce';
28
+ import { useAddressPredictions } from './GooglePlaces';
29
+ import { jsx as ___EmotionJSX } from "@emotion/react";
30
+
31
+ var AddressSelect = function AddressSelect(_ref) {
32
+ var name = _ref.name,
33
+ disabled = _ref.disabled,
34
+ error = _ref.error,
35
+ _ref$size = _ref.size,
36
+ size = _ref$size === void 0 ? 'medium' : _ref$size,
37
+ types = _ref.types,
38
+ onFocus = _ref.onFocus,
39
+ onBlur = _ref.onBlur,
40
+ onChange = _ref.onChange,
41
+ location = _ref.location,
42
+ radius = _ref.radius,
43
+ _ref$countries = _ref.countries,
44
+ countries = _ref$countries === void 0 ? [] : _ref$countries,
45
+ rest = _objectWithoutProperties(_ref, _excluded);
46
+
47
+ var _useState = useState([]),
48
+ _useState2 = _slicedToArray(_useState, 2),
49
+ predictions = _useState2[0],
50
+ setPredictions = _useState2[1];
51
+
52
+ var _useAddressPrediction = useAddressPredictions('', {
53
+ types: types,
54
+ location: location,
55
+ radius: radius,
56
+ components: {
57
+ countries: countries
58
+ }
59
+ }),
60
+ available = _useAddressPrediction.available,
61
+ getPlacePredictions = _useAddressPrediction.getPlacePredictions;
62
+
63
+ var loadOptions = /*#__PURE__*/function () {
64
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(input, callback) {
65
+ var placePredictions, options;
66
+ return regeneratorRuntime.wrap(function _callee$(_context) {
67
+ while (1) {
68
+ switch (_context.prev = _context.next) {
69
+ case 0:
70
+ _context.next = 2;
71
+ return getPlacePredictions(input);
72
+
73
+ case 2:
74
+ placePredictions = _context.sent;
75
+ setPredictions(placePredictions);
76
+ options = placePredictions.map(function (prediction) {
77
+ return {
78
+ value: prediction.description,
79
+ label: prediction.description
80
+ };
81
+ });
82
+ callback(options);
83
+
84
+ case 6:
85
+ case "end":
86
+ return _context.stop();
87
+ }
88
+ }
89
+ }, _callee);
90
+ }));
91
+
92
+ return function loadOptions(_x, _x2) {
93
+ return _ref2.apply(this, arguments);
94
+ };
95
+ }();
96
+
97
+ var handleChange = function handleChange(value) {
98
+ if (onChange) {
99
+ onChange(value, predictions);
100
+ }
101
+ };
102
+
103
+ return ___EmotionJSX(AsyncSearchableSelect, _extends({
104
+ disabled: disabled || !available,
105
+ name: name,
106
+ error: error,
107
+ size: size,
108
+ onChange: handleChange,
109
+ loadOptions: debounce(loadOptions, 500),
110
+ onFocus: onFocus,
111
+ onBlur: onBlur
112
+ }, rest));
113
+ };
114
+
115
+ export default AddressSelect;
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+ export declare const GooglePlacesAPIContext: React.Context<{
3
+ available: boolean;
4
+ }>;
5
+ export interface ILocation {
6
+ lat: number;
7
+ lng: number;
8
+ }
9
+ interface IPredictionsOptions {
10
+ types?: string[] | undefined;
11
+ location?: ILocation | undefined;
12
+ radius?: number | undefined;
13
+ components: {
14
+ countries: string | string[] | undefined;
15
+ };
16
+ }
17
+ interface IPredictionSubstring {
18
+ length: number;
19
+ offset: number;
20
+ }
21
+ interface IAutocompleteStructuredFormatting {
22
+ main_text: string;
23
+ main_text_matched_substrings: IPredictionSubstring[];
24
+ secondary_text: string;
25
+ }
26
+ interface IPredictionTerm {
27
+ offset: number;
28
+ value: string;
29
+ }
30
+ export interface IPrediction {
31
+ description: string;
32
+ matched_substrings: IPredictionSubstring[];
33
+ place_id: string;
34
+ reference: string;
35
+ structured_formatting: IAutocompleteStructuredFormatting;
36
+ terms: IPredictionTerm[];
37
+ types: string[];
38
+ }
39
+ export interface IGooglePlacesAPIProviderProps {
40
+ children: React.ReactNode;
41
+ apiKey: string;
42
+ language?: string;
43
+ libraries?: string;
44
+ }
45
+ export declare const GooglePlacesAPIProvider: ({ children, apiKey, language, libraries, }: IGooglePlacesAPIProviderProps) => JSX.Element;
46
+ export declare const useGooglePlacesAPI: () => {
47
+ available: boolean;
48
+ };
49
+ export declare const useAddressPredictions: (input: string, { types, location, radius, components }: IPredictionsOptions) => {
50
+ available: boolean;
51
+ predictions: IPrediction[];
52
+ getPlacePredictions: (input: string) => Promise<IPrediction[]>;
53
+ };
54
+ export {};
@@ -0,0 +1,123 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+
3
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
5
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+
7
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
+
9
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
10
+
11
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
12
+
13
+ /// <reference types="@types/googlemaps" />
14
+ import React, { createContext, useState, useContext, useRef, useEffect } from 'react';
15
+ import { useScript } from '@dfds-ui/hooks';
16
+ import { jsx as ___EmotionJSX } from "@emotion/react";
17
+ export var GooglePlacesAPIContext = /*#__PURE__*/createContext({
18
+ available: false
19
+ });
20
+ export var GooglePlacesAPIProvider = function GooglePlacesAPIProvider(_ref) {
21
+ var _window, _window$google;
22
+
23
+ var children = _ref.children,
24
+ apiKey = _ref.apiKey,
25
+ language = _ref.language,
26
+ _ref$libraries = _ref.libraries,
27
+ libraries = _ref$libraries === void 0 ? 'places' : _ref$libraries;
28
+ // We want to prevent the google maps api to be loaded more than once
29
+ var isGoogleMapsDefined = typeof window !== 'undefined' && Boolean((_window = window) === null || _window === void 0 ? void 0 : (_window$google = _window.google) === null || _window$google === void 0 ? void 0 : _window$google.maps);
30
+ var languageParam = language ? "&language=".concat(language) : '';
31
+ var scriptUrl = "https://maps.googleapis.com/maps/api/js?key=".concat(apiKey, "&libraries=").concat(libraries).concat(languageParam);
32
+
33
+ var _useScript = useScript(scriptUrl, isGoogleMapsDefined),
34
+ _useScript2 = _slicedToArray(_useScript, 2),
35
+ loaded = _useScript2[0],
36
+ error = _useScript2[1];
37
+
38
+ var _useState = useState(false),
39
+ _useState2 = _slicedToArray(_useState, 2),
40
+ available = _useState2[0],
41
+ setAvailable = _useState2[1];
42
+
43
+ useEffect(function () {
44
+ setAvailable(loaded && !error);
45
+
46
+ if (error) {
47
+ throw new Error('Google Places API script failed to load');
48
+ }
49
+ }, [loaded, error]);
50
+ return ___EmotionJSX(GooglePlacesAPIContext.Provider, {
51
+ value: {
52
+ available: available
53
+ }
54
+ }, children);
55
+ };
56
+ export var useGooglePlacesAPI = function useGooglePlacesAPI() {
57
+ var context = useContext(GooglePlacesAPIContext);
58
+
59
+ if (context === undefined) {
60
+ throw new Error('useGooglePlaces must be used inside of GooglePlacesAPIProvider');
61
+ }
62
+
63
+ return context;
64
+ };
65
+ export var useAddressPredictions = function useAddressPredictions(input, _ref2) {
66
+ var types = _ref2.types,
67
+ location = _ref2.location,
68
+ radius = _ref2.radius,
69
+ components = _ref2.components;
70
+
71
+ var _useState3 = useState([]),
72
+ _useState4 = _slicedToArray(_useState3, 2),
73
+ predictions = _useState4[0],
74
+ setPredictions = _useState4[1];
75
+
76
+ var _useGooglePlacesAPI = useGooglePlacesAPI(),
77
+ available = _useGooglePlacesAPI.available;
78
+
79
+ var autocomplete = useRef();
80
+
81
+ if (!autocomplete.current && available && typeof window !== 'undefined') {
82
+ autocomplete.current = new google.maps.places.AutocompleteService();
83
+ }
84
+
85
+ function getPlacePredictions(input) {
86
+ return new Promise(function (resolve) {
87
+ autocomplete.current.getPlacePredictions({
88
+ input: input,
89
+ types: types,
90
+ radius: radius,
91
+ location: location !== undefined ? {
92
+ lat: function lat() {
93
+ return location !== undefined ? location.lat : undefined;
94
+ },
95
+ lng: function lng() {
96
+ return location !== undefined ? location.lng : undefined;
97
+ }
98
+ } : undefined,
99
+ componentRestrictions: {
100
+ country: components.countries
101
+ }
102
+ }, function (predictions) {
103
+ var _predictions;
104
+
105
+ predictions = (_predictions = predictions) !== null && _predictions !== void 0 ? _predictions : [];
106
+ setPredictions(predictions);
107
+ resolve(predictions);
108
+ });
109
+ });
110
+ }
111
+
112
+ useEffect(function () {
113
+ if (input !== '') {
114
+ void getPlacePredictions(input);
115
+ } // eslint-disable-next-line react-hooks/exhaustive-deps
116
+
117
+ }, [input]);
118
+ return {
119
+ available: available,
120
+ predictions: predictions,
121
+ getPlacePredictions: getPlacePredictions
122
+ };
123
+ };
@@ -0,0 +1,28 @@
1
+ /// <reference types="react" />
2
+ import { ILocation, IPrediction } from './GooglePlaces';
3
+ export declare type AddressSelectProps = {
4
+ name: string;
5
+ disabled?: boolean;
6
+ error?: string;
7
+ size?: 'small' | 'medium';
8
+ types?: string[];
9
+ onFocus?: (event: FocusEvent) => void;
10
+ onBlur?: (event: FocusEvent) => void;
11
+ onChange?: (changed: {
12
+ value: string;
13
+ label: string;
14
+ }, predictions: IPrediction[]) => void;
15
+ location?: ILocation;
16
+ radius?: number;
17
+ countries?: string[] | string;
18
+ placeholder?: string;
19
+ assistiveText?: string;
20
+ autoFocus?: boolean;
21
+ isClearable?: boolean;
22
+ /**
23
+ * Class name to be assigned to the component
24
+ */
25
+ className?: string;
26
+ };
27
+ declare const AddressSelect: ({ name, disabled, error, size, types, onFocus, onBlur, onChange, location, radius, countries, ...rest }: AddressSelectProps) => JSX.Element;
28
+ export default AddressSelect;
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _react = _interopRequireWildcard(require("react"));
9
+
10
+ var _reactComponents = require("@dfds-ui/react-components");
11
+
12
+ var _lodash = _interopRequireDefault(require("lodash.debounce"));
13
+
14
+ var _GooglePlaces = require("./GooglePlaces");
15
+
16
+ var _react2 = require("@emotion/react");
17
+
18
+ const _excluded = ["name", "disabled", "error", "size", "types", "onFocus", "onBlur", "onChange", "location", "radius", "countries"];
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
+
24
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
+
26
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
27
+
28
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
29
+
30
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
31
+
32
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
33
+
34
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
35
+
36
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
37
+
38
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
39
+
40
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
41
+
42
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
43
+
44
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
45
+
46
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
47
+
48
+ const AddressSelect = _ref => {
49
+ let name = _ref.name,
50
+ disabled = _ref.disabled,
51
+ error = _ref.error,
52
+ _ref$size = _ref.size,
53
+ size = _ref$size === void 0 ? 'medium' : _ref$size,
54
+ types = _ref.types,
55
+ onFocus = _ref.onFocus,
56
+ onBlur = _ref.onBlur,
57
+ onChange = _ref.onChange,
58
+ location = _ref.location,
59
+ radius = _ref.radius,
60
+ _ref$countries = _ref.countries,
61
+ countries = _ref$countries === void 0 ? [] : _ref$countries,
62
+ rest = _objectWithoutProperties(_ref, _excluded);
63
+
64
+ const _useState = (0, _react.useState)([]),
65
+ _useState2 = _slicedToArray(_useState, 2),
66
+ predictions = _useState2[0],
67
+ setPredictions = _useState2[1];
68
+
69
+ const _useAddressPrediction = (0, _GooglePlaces.useAddressPredictions)('', {
70
+ types,
71
+ location,
72
+ radius,
73
+ components: {
74
+ countries
75
+ }
76
+ }),
77
+ available = _useAddressPrediction.available,
78
+ getPlacePredictions = _useAddressPrediction.getPlacePredictions;
79
+
80
+ const loadOptions = /*#__PURE__*/function () {
81
+ var _ref2 = _asyncToGenerator(function* (input, callback) {
82
+ const placePredictions = yield getPlacePredictions(input);
83
+ setPredictions(placePredictions);
84
+ const options = placePredictions.map(prediction => ({
85
+ value: prediction.description,
86
+ label: prediction.description
87
+ }));
88
+ callback(options);
89
+ });
90
+
91
+ return function loadOptions(_x, _x2) {
92
+ return _ref2.apply(this, arguments);
93
+ };
94
+ }();
95
+
96
+ const handleChange = value => {
97
+ if (onChange) {
98
+ onChange(value, predictions);
99
+ }
100
+ };
101
+
102
+ return (0, _react2.jsx)(_reactComponents.AsyncSearchableSelect, _extends({
103
+ disabled: disabled || !available,
104
+ name: name,
105
+ error: error,
106
+ size: size,
107
+ onChange: handleChange,
108
+ loadOptions: (0, _lodash.default)(loadOptions, 500),
109
+ onFocus: onFocus,
110
+ onBlur: onBlur
111
+ }, rest));
112
+ };
113
+
114
+ var _default = AddressSelect;
115
+ exports.default = _default;
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+ export declare const GooglePlacesAPIContext: React.Context<{
3
+ available: boolean;
4
+ }>;
5
+ export interface ILocation {
6
+ lat: number;
7
+ lng: number;
8
+ }
9
+ interface IPredictionsOptions {
10
+ types?: string[] | undefined;
11
+ location?: ILocation | undefined;
12
+ radius?: number | undefined;
13
+ components: {
14
+ countries: string | string[] | undefined;
15
+ };
16
+ }
17
+ interface IPredictionSubstring {
18
+ length: number;
19
+ offset: number;
20
+ }
21
+ interface IAutocompleteStructuredFormatting {
22
+ main_text: string;
23
+ main_text_matched_substrings: IPredictionSubstring[];
24
+ secondary_text: string;
25
+ }
26
+ interface IPredictionTerm {
27
+ offset: number;
28
+ value: string;
29
+ }
30
+ export interface IPrediction {
31
+ description: string;
32
+ matched_substrings: IPredictionSubstring[];
33
+ place_id: string;
34
+ reference: string;
35
+ structured_formatting: IAutocompleteStructuredFormatting;
36
+ terms: IPredictionTerm[];
37
+ types: string[];
38
+ }
39
+ export interface IGooglePlacesAPIProviderProps {
40
+ children: React.ReactNode;
41
+ apiKey: string;
42
+ language?: string;
43
+ libraries?: string;
44
+ }
45
+ export declare const GooglePlacesAPIProvider: ({ children, apiKey, language, libraries, }: IGooglePlacesAPIProviderProps) => JSX.Element;
46
+ export declare const useGooglePlacesAPI: () => {
47
+ available: boolean;
48
+ };
49
+ export declare const useAddressPredictions: (input: string, { types, location, radius, components }: IPredictionsOptions) => {
50
+ available: boolean;
51
+ predictions: IPrediction[];
52
+ getPlacePredictions: (input: string) => Promise<IPrediction[]>;
53
+ };
54
+ export {};
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useAddressPredictions = exports.useGooglePlacesAPI = exports.GooglePlacesAPIProvider = exports.GooglePlacesAPIContext = void 0;
7
+
8
+ var _react = _interopRequireWildcard(require("react"));
9
+
10
+ var _hooks = require("@dfds-ui/hooks");
11
+
12
+ var _react2 = require("@emotion/react");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
18
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
19
+
20
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
21
+
22
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
23
+
24
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
25
+
26
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
27
+
28
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
29
+
30
+ const GooglePlacesAPIContext = /*#__PURE__*/(0, _react.createContext)({
31
+ available: false
32
+ });
33
+ exports.GooglePlacesAPIContext = GooglePlacesAPIContext;
34
+
35
+ const GooglePlacesAPIProvider = ({
36
+ children,
37
+ apiKey,
38
+ language,
39
+ libraries = 'places'
40
+ }) => {
41
+ var _window, _window$google;
42
+
43
+ // We want to prevent the google maps api to be loaded more than once
44
+ const isGoogleMapsDefined = typeof window !== 'undefined' && Boolean((_window = window) === null || _window === void 0 ? void 0 : (_window$google = _window.google) === null || _window$google === void 0 ? void 0 : _window$google.maps);
45
+ const languageParam = language ? `&language=${language}` : '';
46
+ const scriptUrl = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries}${languageParam}`;
47
+
48
+ const _useScript = (0, _hooks.useScript)(scriptUrl, isGoogleMapsDefined),
49
+ _useScript2 = _slicedToArray(_useScript, 2),
50
+ loaded = _useScript2[0],
51
+ error = _useScript2[1];
52
+
53
+ const _useState = (0, _react.useState)(false),
54
+ _useState2 = _slicedToArray(_useState, 2),
55
+ available = _useState2[0],
56
+ setAvailable = _useState2[1];
57
+
58
+ (0, _react.useEffect)(() => {
59
+ setAvailable(loaded && !error);
60
+
61
+ if (error) {
62
+ throw new Error('Google Places API script failed to load');
63
+ }
64
+ }, [loaded, error]);
65
+ return (0, _react2.jsx)(GooglePlacesAPIContext.Provider, {
66
+ value: {
67
+ available
68
+ }
69
+ }, children);
70
+ };
71
+
72
+ exports.GooglePlacesAPIProvider = GooglePlacesAPIProvider;
73
+
74
+ const useGooglePlacesAPI = () => {
75
+ const context = (0, _react.useContext)(GooglePlacesAPIContext);
76
+
77
+ if (context === undefined) {
78
+ throw new Error('useGooglePlaces must be used inside of GooglePlacesAPIProvider');
79
+ }
80
+
81
+ return context;
82
+ };
83
+
84
+ exports.useGooglePlacesAPI = useGooglePlacesAPI;
85
+
86
+ const useAddressPredictions = (input, {
87
+ types,
88
+ location,
89
+ radius,
90
+ components
91
+ }) => {
92
+ const _useState3 = (0, _react.useState)([]),
93
+ _useState4 = _slicedToArray(_useState3, 2),
94
+ predictions = _useState4[0],
95
+ setPredictions = _useState4[1];
96
+
97
+ const _useGooglePlacesAPI = useGooglePlacesAPI(),
98
+ available = _useGooglePlacesAPI.available;
99
+
100
+ const autocomplete = (0, _react.useRef)();
101
+
102
+ if (!autocomplete.current && available && typeof window !== 'undefined') {
103
+ autocomplete.current = new google.maps.places.AutocompleteService();
104
+ }
105
+
106
+ function getPlacePredictions(input) {
107
+ return new Promise(resolve => {
108
+ autocomplete.current.getPlacePredictions({
109
+ input,
110
+ types,
111
+ radius,
112
+ location: location !== undefined ? {
113
+ lat: () => location !== undefined ? location.lat : undefined,
114
+ lng: () => location !== undefined ? location.lng : undefined
115
+ } : undefined,
116
+ componentRestrictions: {
117
+ country: components.countries
118
+ }
119
+ }, predictions => {
120
+ var _predictions;
121
+
122
+ predictions = (_predictions = predictions) !== null && _predictions !== void 0 ? _predictions : [];
123
+ setPredictions(predictions);
124
+ resolve(predictions);
125
+ });
126
+ });
127
+ }
128
+
129
+ (0, _react.useEffect)(() => {
130
+ if (input !== '') {
131
+ void getPlacePredictions(input);
132
+ } // eslint-disable-next-line react-hooks/exhaustive-deps
133
+
134
+ }, [input]);
135
+ return {
136
+ available,
137
+ predictions,
138
+ getPlacePredictions
139
+ };
140
+ };
141
+
142
+ exports.useAddressPredictions = useAddressPredictions;
package/cjs/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './GooglePlaces';
2
+ export { default as AddressSelect } from './AddressSelect';
3
+ export * from './AddressSelect';
package/cjs/index.js ADDED
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ AddressSelect: true
8
+ };
9
+ Object.defineProperty(exports, "AddressSelect", {
10
+ enumerable: true,
11
+ get: function get() {
12
+ return _AddressSelect.default;
13
+ }
14
+ });
15
+
16
+ var _GooglePlaces = require("./GooglePlaces");
17
+
18
+ Object.keys(_GooglePlaces).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
21
+ if (key in exports && exports[key] === _GooglePlaces[key]) return;
22
+ Object.defineProperty(exports, key, {
23
+ enumerable: true,
24
+ get: function get() {
25
+ return _GooglePlaces[key];
26
+ }
27
+ });
28
+ });
29
+
30
+ var _AddressSelect = _interopRequireWildcard(require("./AddressSelect"));
31
+
32
+ Object.keys(_AddressSelect).forEach(function (key) {
33
+ if (key === "default" || key === "__esModule") return;
34
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
35
+ if (key in exports && exports[key] === _AddressSelect[key]) return;
36
+ Object.defineProperty(exports, key, {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _AddressSelect[key];
40
+ }
41
+ });
42
+ });
43
+
44
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
45
+
46
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -0,0 +1 @@
1
+ "use strict";
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './GooglePlaces';
2
+ export { default as AddressSelect } from './AddressSelect';
3
+ export * from './AddressSelect';
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './GooglePlaces';
2
+ export { default as AddressSelect } from './AddressSelect';
3
+ export * from './AddressSelect';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Components using Google places",
4
4
  "license": "MIT",
5
5
  "private": false,
6
- "version": "1.1.12-alpha.d54268bb",
6
+ "version": "1.1.12-alpha.f3c16129",
7
7
  "sideEffects": false,
8
8
  "main": "./cjs/index.js",
9
9
  "module": "./index.js",
@@ -14,15 +14,15 @@
14
14
  "react-dom": ">= 17.0.2"
15
15
  },
16
16
  "dependencies": {
17
- "@dfds-ui/colors": "1.1.12-alpha.d54268bb",
18
- "@dfds-ui/hooks": "1.1.12-alpha.d54268bb",
19
- "@dfds-ui/icons": "1.1.12-alpha.d54268bb",
20
- "@dfds-ui/react-components": "1.1.12-alpha.d54268bb",
17
+ "@dfds-ui/colors": "1.1.12-alpha.f3c16129",
18
+ "@dfds-ui/hooks": "1.1.12-alpha.f3c16129",
19
+ "@dfds-ui/icons": "1.1.12-alpha.f3c16129",
20
+ "@dfds-ui/react-components": "1.1.12-alpha.f3c16129",
21
21
  "@types/googlemaps": "3.43.3",
22
22
  "@types/lodash.debounce": "^4.0.7",
23
23
  "lodash.debounce": "^4.0.8"
24
24
  },
25
- "gitHead": "d54268bbf4c3b37fd5e62b6c6d4ebf3fdfb70c31",
25
+ "gitHead": "f3c1612943a1eafc63aed49e7db6b4256e834df9",
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  }
package/typings.d.js ADDED
File without changes