@@ -14,6 +14,7 @@ var _material = require("@mui/material");
1414var _parse = _interopRequireDefault ( require ( "autosuggest-highlight/parse" ) ) ;
1515var _lodash = _interopRequireDefault ( require ( "lodash.throttle" ) ) ;
1616var _react = _interopRequireWildcard ( require ( "react" ) ) ;
17+ var _jsApiLoader = require ( "@googlemaps/js-api-loader" ) ;
1718const _excluded = [ "apiKey" , "fields" , "label" , "onChange" , "value" , "requestOptions" ] ;
1819function _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 ) ; }
1920function _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 ; }
@@ -44,10 +45,10 @@ const AddressAutocomplete = _ref => {
4445 requestOptions = { }
4546 } = _ref ,
4647 rest = _objectWithoutProperties ( _ref , _excluded ) ;
47- const loaded = ( 0 , _react . useRef ) ( false ) ;
4848 const [ addressOptions , setAddressOptions ] = ( 0 , _react . useState ) ( [ ] ) ;
4949 const [ addressValue , setAddressValue ] = ( 0 , _react . useState ) ( value ) ;
5050 const [ addressInputValue , setAddressInputValue ] = ( 0 , _react . useState ) ( '' ) ;
51+ const [ google , setGoogle ] = ( 0 , _react . useState ) ( null ) ;
5152
5253 // Update inner value when props value change
5354 ( 0 , _react . useEffect ) ( ( ) => {
@@ -140,6 +141,7 @@ const AddressAutocomplete = _ref => {
140141 main_text_matched_substrings : matches
141142 }
142143 } = option ;
144+ if ( ! matches ) return null ;
143145 const parts = ( 0 , _parse . default ) ( option . structured_formatting . main_text , matches . map ( match => [ match . offset , match . offset + match . length ] ) ) ;
144146 return /*#__PURE__*/ _react . default . createElement ( _material . Box , _extends ( {
145147 component : "li"
@@ -167,23 +169,23 @@ const AddressAutocomplete = _ref => {
167169 } , option . structured_formatting . secondary_text ) ) ) ) ;
168170 } ;
169171
170- // Load Google Maps API script if not already loaded
172+ // Load Google Maps API if not already loaded
171173 ( 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 ;
174+ if ( google ) return ;
175+ if ( ! apiKey ) {
176+ console . error ( 'You need to provide an API key to use this component' ) ;
177+ return ;
185178 }
186- } , [ apiKey , loaded ] ) ;
179+ const loader = new _jsApiLoader . Loader ( {
180+ apiKey,
181+ version : 'weekly'
182+ } ) ;
183+ loader . importLibrary ( 'places' ) . then ( google => {
184+ setGoogle ( google ) ;
185+ } ) . catch ( err => {
186+ console . error ( err ) ;
187+ } ) ;
188+ } , [ apiKey ] ) ;
187189
188190 // Autocomplete predictions fetcher
189191 const fetch = ( 0 , _react . useMemo ) ( ( ) => ( 0 , _lodash . default ) ( ( request , callback ) => {
@@ -196,14 +198,15 @@ const AddressAutocomplete = _ref => {
196198 ( 0 , _react . useEffect ) ( ( ) => {
197199 // Lock worker
198200 let active = true ;
201+ if ( ! google ) return ;
199202
200203 // Initialize Google Maps Autocomplete Service
201- if ( ! autocompleteService . current && window . google ) {
202- autocompleteService . current = new window . google . maps . places . AutocompleteService ( ) ;
204+ if ( ! autocompleteService . current ) {
205+ autocompleteService . current = new google . AutocompleteService ( ) ;
203206 }
204207 // Initialize Google Maps Places Service
205- if ( ! placesService . current && window . google ) {
206- placesService . current = new window . google . maps . places . PlacesService ( document . createElement ( 'div' ) ) ;
208+ if ( ! placesService . current ) {
209+ placesService . current = new google . PlacesService ( document . createElement ( 'div' ) ) ;
207210 }
208211 // Stop execution if the service is not available
209212 if ( ! autocompleteService . current || ! placesService . current ) {
0 commit comments