use-places-autocomplete
Version:
React hook for Google Maps Places Autocomplete.
3 lines (2 loc) • 3.44 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(){return t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},t.apply(this,arguments)}var n=function(t){var n=e.useRef(t);return n.current=t,n},o="💡 use-places-autocomplete: Please provide a place Id when using getDetails() either as a string or as part of an Autocomplete Prediction.";exports.default=function(o){var a,r,s,u=void 0===o?{}:o,c=u.requestOptions,i=u.debounce,l=void 0===i?200:i,d=u.cache,p=void 0===d?86400:d,g=u.cacheKey,f=void 0===g?"upa":g,v=u.googleMaps,m=u.callbackName,w=u.defaultValue,h=void 0===w?"":w,y=u.initOnMount,b=void 0===y||y,O=e.useState(!1),P=O[0],S=O[1],x=e.useState(h),k=x[0],C=x[1],A=e.useState({loading:!1,status:"",data:[]}),I=A[0],_=A[1],j=e.useRef(),D=n(c),K=n(v),G=e.useCallback((function(){var e;if(!j.current){var t=window.google,n=K.current,o=(null==n?void 0:n.places)||(null==t||null==(e=t.maps)?void 0:e.places);o?(j.current=new o.AutocompleteService,S(!0)):console.error("💡 use-places-autocomplete: Google Maps Places API library must be loaded. See: https://github.com/wellyshen/use-places-autocomplete#load-the-library")}}),[K]),M=e.useCallback((function(){_({loading:!1,status:"",data:[]})}),[]),R=e.useCallback((function(e){void 0===e&&(e=f);try{sessionStorage.removeItem(e)}catch(e){}}),[f]),N=e.useCallback((a=function(e){var n;if(e){_((function(e){return t({},e,{loading:!0})}));var o={};try{o=JSON.parse(sessionStorage.getItem(f)||"{}")}catch(e){}p&&(o=Object.keys(o).reduce((function(e,t){return o[t].maxAge-Date.now()>=0&&(e[t]=o[t]),e}),{}))[e]?_({loading:!1,status:"OK",data:o[e].data}):null==(n=j.current)||n.getPlacePredictions(t({},D.current,{input:e}),(function(t,n){if(_({loading:!1,status:n,data:t||[]}),p&&"OK"===n){o[e]={data:t,maxAge:Date.now()+1e3*p};try{sessionStorage.setItem(f,JSON.stringify(o))}catch(e){}}}))}else M()},r=l,function(){for(var e=this,t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];null!==s&&(clearTimeout(s),s=null),s=setTimeout((function(){return a.apply(e,n)}),r)}),[p,f,M,D]),q=e.useCallback((function(e,t){void 0===t&&(t=!0),C(e),j.current&&t&&N(e)}),[N]);return e.useEffect((function(){if(!b)return function(){return null};var e=window.google;return K.current||null!=e&&e.maps||!m?G():window[m]=G,function(){window[m]&&delete window[m]}}),[m,K,G,b]),{ready:P,value:k,suggestions:I,setValue:q,clearSuggestions:M,clearCache:R,init:G}},exports.getDetails=function(e){var t=new window.google.maps.places.PlacesService(document.createElement("div"));return e.placeId?new Promise((function(n,o){t.getDetails(e,(function(e,t){"OK"!==t&&o(t),n(e)}))})):(console.error(o),Promise.reject(o))},exports.getGeocode=function(e){var t=new window.google.maps.Geocoder;return new Promise((function(n,o){t.geocode(e,(function(t,a){"OK"!==a&&o(a),!e.address&&e.componentRestrictions&&(console.error("💡 use-places-autocomplete: Please provide an address when using getGeocode() with the componentRestrictions."),n(t)),n(t)}))}))},exports.getLatLng=function(e){var t=e.geometry.location,n=t.lat,o=t.lng;return{lat:n(),lng:o()}},exports.getZipCode=function(e,t){var n=e.address_components.find((function(e){return e.types.includes("postal_code")}));if(n)return t?n.short_name:n.long_name};
//# sourceMappingURL=index.js.map