11"use strict" ;
22
33require ( "core-js/modules/es.object.assign.js" ) ;
4-
54Object . defineProperty ( exports , "__esModule" , {
65 value : true
76} ) ;
87exports . default = void 0 ;
9-
108require ( "core-js/modules/web.dom-collections.iterator.js" ) ;
11-
129require ( "core-js/modules/es.string.includes.js" ) ;
13-
1410require ( "core-js/modules/es.symbol.description.js" ) ;
15-
1611require ( "core-js/modules/es.array.reduce.js" ) ;
17-
18- var _iconsMaterial = require ( "@mui/icons-material" ) ;
19-
12+ var _LocationOn = _interopRequireDefault ( require ( "@mui/icons-material/LocationOn" ) ) ;
2013var _material = require ( "@mui/material" ) ;
21-
2214var _parse = _interopRequireDefault ( require ( "autosuggest-highlight/parse" ) ) ;
23-
2415var _lodash = _interopRequireDefault ( require ( "lodash.throttle" ) ) ;
25-
2616var _react = _interopRequireWildcard ( require ( "react" ) ) ;
27-
2817const _excluded = [ "apiKey" , "fields" , "label" , "onChange" , "value" , "requestOptions" ] ;
29-
3018function _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 ) ; }
31-
3219function _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 ; }
33-
3420function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
35-
3621function _extends ( ) { _extends = Object . assign || 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 ) ; }
37-
3822function ownKeys ( object , enumerableOnly ) { var keys = Object . keys ( object ) ; if ( Object . getOwnPropertySymbols ) { var symbols = Object . getOwnPropertySymbols ( object ) ; enumerableOnly && ( symbols = symbols . filter ( function ( sym ) { return Object . getOwnPropertyDescriptor ( object , sym ) . enumerable ; } ) ) , keys . push . apply ( keys , symbols ) ; } return keys ; }
39-
4023function _objectSpread ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = null != arguments [ i ] ? arguments [ i ] : { } ; i % 2 ? ownKeys ( Object ( source ) , ! 0 ) . forEach ( function ( key ) { _defineProperty ( target , key , source [ key ] ) ; } ) : Object . getOwnPropertyDescriptors ? Object . defineProperties ( target , Object . getOwnPropertyDescriptors ( source ) ) : ownKeys ( Object ( source ) ) . forEach ( function ( key ) { Object . defineProperty ( target , key , Object . getOwnPropertyDescriptor ( source , key ) ) ; } ) ; } return target ; }
41-
4224function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
43-
4425function _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-
4626function _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-
4827const autocompleteService = {
4928 current : null
5029} ;
5130const placesService = {
5231 current : null
5332} ;
5433
34+ /**
35+ * AddressAutocomplete Component
36+ */
5537const AddressAutocomplete = _ref => {
5638 let {
57- apiKey,
58- fields = [ 'address_components' , 'formatted_address' ] ,
59- label,
60- onChange,
61- value,
62- requestOptions = { }
63- } = _ref ,
64- rest = _objectWithoutProperties ( _ref , _excluded ) ;
65-
66- const loaded = _react . default . useRef ( false ) ;
67-
68- const [ addressOptions , setAddressOptions ] = _react . default . useState ( [ ] ) ;
69-
70- const [ addressValue , setAddressValue ] = _react . default . useState ( value ) ;
71-
72- const [ addressInputValue , setAddressInputValue ] = _react . default . useState ( '' ) ; // Update inner value when props value change
73-
74-
39+ apiKey,
40+ fields = [ 'address_components' , 'formatted_address' ] ,
41+ label,
42+ onChange,
43+ value,
44+ requestOptions = { }
45+ } = _ref ,
46+ rest = _objectWithoutProperties ( _ref , _excluded ) ;
47+ const loaded = ( 0 , _react . useRef ) ( false ) ;
48+ const [ addressOptions , setAddressOptions ] = ( 0 , _react . useState ) ( [ ] ) ;
49+ const [ addressValue , setAddressValue ] = ( 0 , _react . useState ) ( value ) ;
50+ const [ addressInputValue , setAddressInputValue ] = ( 0 , _react . useState ) ( '' ) ;
51+
52+ // Update inner value when props value change
7553 ( 0 , _react . useEffect ) ( ( ) => {
7654 setAddressValue ( value ) ;
77- } , [ value ] ) ; // Prefill fields if needed
55+ } , [ value ] ) ;
7856
57+ // Prefill fields if needed
7958 const actualFields = ( 0 , _react . useMemo ) ( ( ) => {
8059 if ( ! fields . includes ( 'address_components' ) ) {
8160 fields . push ( 'address_components' ) ;
8261 }
83-
8462 if ( ! fields . includes ( 'formatted_address' ) ) {
8563 fields . push ( 'formatted_address' ) ;
8664 }
87-
8865 return fields ;
89- } , [ fields ] ) ; // Options label
66+ } , [ fields ] ) ;
9067
91- const getOptionLabel = ( 0 , _react . useCallback ) ( option => typeof option === 'string' ? option : option . description , [ ] ) ; // Autocomplete equals
68+ // Options label
69+ const getOptionLabel = option => typeof option === 'string' ? option : option . description ;
9270
93- const isOptionEqualToValue = ( 0 , _react . useCallback ) ( ( option , val ) => option . place_id === val . place_id , [ ] ) ; // Empty filter
71+ // Autocomplete equals
72+ const isOptionEqualToValue = ( option , val ) => option . place_id === val . place_id ;
9473
95- const filterOptions = ( 0 , _react . useCallback ) ( x => x , [ ] ) ; // Address selection
74+ // Empty filter
75+ const filterOptions = x => x ;
9676
97- const selectAddress = ( 0 , _react . useCallback ) ( ( event , newValue , reason ) => {
77+ // Address selection
78+ const selectAddress = ( event , newValue , reason ) => {
9879 if ( ! placesService . current ) {
9980 return ;
10081 }
101-
10282 setAddressOptions ( previous => newValue ? [ newValue , ...previous ] : previous ) ;
103-
10483 if ( newValue && newValue . place_id ) {
10584 placesService . current . getDetails ( {
10685 placeId : newValue . place_id ,
10786 fields : actualFields
10887 } , place => {
10988 var _place$formatted_addr ;
110-
11189 if ( ! place ) {
11290 return ;
11391 }
114-
11592 const placeWithComponents = _objectSpread ( _objectSpread ( { } , place ) , { } , {
11693 structured_formatting : {
11794 main_text : place . formatted_address || '' ,
@@ -126,7 +103,6 @@ const AddressAutocomplete = _ref => {
126103 if ( ! acc [ type ] ) {
127104 acc [ type ] = [ ] ;
128105 }
129-
130106 acc [ type ] . push ( {
131107 long_name : item . long_name ,
132108 short_name : item . short_name
@@ -137,39 +113,43 @@ const AddressAutocomplete = _ref => {
137113 description : newValue . description ,
138114 place_id : newValue . place_id
139115 } ) ;
140-
141116 setAddressValue ( placeWithComponents ) ;
142117 onChange ( event , placeWithComponents , reason ) ;
143118 } ) ;
144119 } else {
145120 setAddressValue ( null ) ;
146121 onChange ( event , null , reason ) ;
147122 }
148- } , [ actualFields , onChange ] ) ; // Address input change
123+ } ;
149124
150- const searchAddress = ( 0 , _react . useCallback ) ( ( _ , newInputValue ) => {
125+ // Address input change
126+ const searchAddress = ( _ , newInputValue ) => {
151127 setAddressInputValue ( newInputValue ) ;
152- } , [ ] ) ; // Address input renderer
128+ } ;
153129
154- const renderAddressInput = ( 0 , _react . useCallback ) ( params => /*#__PURE__*/ _react . default . createElement ( _material . TextField , _extends ( { } , params , {
130+ // Address input renderer
131+ const renderAddressInput = params => /*#__PURE__*/ _react . default . createElement ( _material . TextField , _extends ( { } , params , {
155132 fullWidth : true ,
156133 label : label
157- } ) ) , [ label ] ) ; // Options renderer
134+ } ) ) ;
158135
159- const renderAddressOption = ( 0 , _react . useCallback ) ( ( props , option ) => {
136+ // Options renderer
137+ const renderAddressOption = ( props , option ) => {
160138 const {
161139 structured_formatting : {
162140 main_text_matched_substrings : matches
163141 }
164142 } = option ;
165143 const parts = ( 0 , _parse . default ) ( option . structured_formatting . main_text , matches . map ( match => [ match . offset , match . offset + match . length ] ) ) ;
166- return /*#__PURE__*/ _react . default . createElement ( "li" , props , /*#__PURE__*/ _react . default . createElement ( _material . Grid , {
144+ return /*#__PURE__*/ _react . default . createElement ( _material . Box , _extends ( {
145+ component : "li"
146+ } , props ) , /*#__PURE__*/ _react . default . createElement ( _material . Grid , {
167147 alignItems : "center" ,
168148 container : true
169149 } , /*#__PURE__*/ _react . default . createElement ( _material . Grid , {
170150 item : true
171151 } , /*#__PURE__*/ _react . default . createElement ( _material . Box , {
172- component : _iconsMaterial . LocationOn ,
152+ component : _LocationOn . default ,
173153 sx : {
174154 mr : 2
175155 }
@@ -185,81 +165,83 @@ const AddressAutocomplete = _ref => {
185165 } , part . text ) ) , /*#__PURE__*/ _react . default . createElement ( _material . Typography , {
186166 variant : "body2"
187167 } , option . structured_formatting . secondary_text ) ) ) ) ;
188- } , [ ] ) ; // Load Google Maps API script if not already loaded
168+ } ;
189169
190- if ( typeof window !== 'undefined' && ! loaded . current ) {
191- if ( ! document . querySelector ( '#google-maps' ) ) {
192- var _document$querySelect ;
193-
194- const script = document . createElement ( 'script' ) ;
195- script . setAttribute ( 'async' , '' ) ;
196- script . setAttribute ( 'id' , 'google-maps' ) ;
197- script . src = "https://maps.googleapis.com/maps/api/js?key=" . concat ( apiKey , "&libraries=places" ) ;
198- ( _document$querySelect = document . querySelector ( 'head' ) ) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect . appendChild ( script ) ;
170+ // Load Google Maps API script if not already loaded
171+ ( 0 , _react . useEffect ) ( ( ) => {
172+ if ( typeof window !== 'undefined' && ! loaded . current ) {
173+ if ( ! document . querySelector ( '#google-maps' ) ) {
174+ var _document$querySelect ;
175+ const script = document . createElement ( 'script' ) ;
176+ if ( ! apiKey ) {
177+ console . error ( 'You need to provide an API key to use this component' ) ;
178+ }
179+ script . setAttribute ( 'async' , '' ) ;
180+ script . setAttribute ( 'id' , 'google-maps' ) ;
181+ script . src = "https://maps.googleapis.com/maps/api/js?key=" . concat ( apiKey , "&libraries=places" ) ;
182+ ( _document$querySelect = document . querySelector ( 'head' ) ) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect . appendChild ( script ) ;
183+ }
184+ loaded . current = true ;
199185 }
186+ } , [ apiKey , loaded ] ) ;
200187
201- loaded . current = true ;
202- } // Autocomplete predictions fetcher
203-
204-
188+ // Autocomplete predictions fetcher
205189 const fetch = ( 0 , _react . useMemo ) ( ( ) => ( 0 , _lodash . default ) ( ( request , callback ) => {
206190 if ( autocompleteService . current ) {
207191 autocompleteService . current . getPlacePredictions ( request , callback ) ;
208192 }
209- } , 200 ) , [ ] ) ; // Runs on input change
193+ } , 200 ) , [ ] ) ;
210194
195+ // Runs on input change
211196 ( 0 , _react . useEffect ) ( ( ) => {
212197 // Lock worker
213- let active = true ; // Initialize Google Maps Autocomplete Service
198+ let active = true ;
214199
200+ // Initialize Google Maps Autocomplete Service
215201 if ( ! autocompleteService . current && window . google ) {
216202 autocompleteService . current = new window . google . maps . places . AutocompleteService ( ) ;
217- } // Initialize Google Maps Places Service
218-
219-
203+ }
204+ // Initialize Google Maps Places Service
220205 if ( ! placesService . current && window . google ) {
221206 placesService . current = new window . google . maps . places . PlacesService ( document . createElement ( 'div' ) ) ;
222- } // Stop execution if the service is not available
223-
224-
207+ }
208+ // Stop execution if the service is not available
225209 if ( ! autocompleteService . current || ! placesService . current ) {
226210 return undefined ;
227- } // Hide options when input is empty
228-
211+ }
229212
213+ // Hide options when input is empty
230214 if ( addressInputValue === '' ) {
231215 setAddressOptions ( prev => {
232216 if ( addressValue && ! prev . find ( o => o . place_id === addressValue . place_id ) ) {
233217 return [ addressValue ] ;
234218 }
235-
236219 return [ ] ;
237220 } ) ;
238221 return undefined ;
239- } // Fetch autocomplete predictions
240-
222+ }
241223
224+ // Fetch autocomplete predictions
242225 fetch ( _objectSpread ( _objectSpread ( { } , requestOptions ) , { } , {
243226 input : addressInputValue
244227 } ) , results => {
245228 if ( active ) {
246- let newOptions = [ ] ; // Include fetched predictions
229+ let newOptions = [ ] ;
247230
231+ // Include fetched predictions
248232 if ( results ) {
249233 newOptions = results ;
250234 }
251-
252235 if ( addressValue ) {
253- const fetchedAddressValueIndex = newOptions . findIndex ( o => o . place_id === addressValue . place_id ) ; // Include selected address if it is not in the predictions
254-
236+ const fetchedAddressValueIndex = newOptions . findIndex ( o => o . place_id === addressValue . place_id ) ;
237+ // Include selected address if it is not in the predictions
255238 if ( fetchedAddressValueIndex === - 1 ) {
256239 newOptions = [ addressValue , ...newOptions ] ;
257240 } else {
258241 // Place selected addres at the top if it is in the predictions
259242 newOptions = [ newOptions [ fetchedAddressValueIndex ] , ...newOptions . slice ( 0 , fetchedAddressValueIndex ) , ...newOptions . slice ( fetchedAddressValueIndex + 1 ) ] ;
260243 }
261244 }
262-
263245 setAddressOptions ( newOptions ) ;
264246 }
265247 } ) ;
@@ -284,6 +266,5 @@ const AddressAutocomplete = _ref => {
284266 value : addressValue
285267 } , rest ) ) ;
286268} ;
287-
288269var _default = AddressAutocomplete ;
289270exports . default = _default ;
0 commit comments