UNPKG

native-forms-react

Version:

Build forms, surveys and polls for React.

2 lines (1 loc) 3.99 kB
"use strict";var e,t=require("./index-8e695942.js"),n=require("react"),r=(e=n)&&"object"==typeof e&&"default"in e?e.default:e,o="undefined"!=typeof window&&window.document;function c(e){var r=e.ref,c=e.onPlaceSelected,u=e.apiKey,i=e.libraries,s=void 0===i?"places":i,a=e.inputAutocompleteValue,l=void 0===a?"new-password":a,p=e.options,d=(p=void 0===p?{}:p).types,f=void 0===d?["(cities)"]:d,m=p.componentRestrictions,g=p.fields,b=void 0===g?["address_components","geometry.location","place_id","formatted_address"]:g,v=p.bounds,w=t._objectWithoutProperties(p,["types","componentRestrictions","fields","bounds"]),y=e.googleMapsScriptBaseUrl,P=void 0===y?"https://maps.googleapis.com/maps/api/js":y,E=e.language,S=n.useRef(null),h=n.useRef(null),j=n.useRef(null),M=n.useRef(null),A=E?"&language=".concat(E):"",R="".concat(P,"?libraries=").concat(s,"&key=").concat(u).concat(A),_=n.useCallback((function(){return function(e,t){if(!o)return Promise.resolve();if(void 0!==window.google&&window.google.maps&&window.google.maps.api)return Promise.resolve();var n=document.querySelectorAll('script[src*="'.concat(e,'"]'));if(n&&n.length)return new Promise((function(e){if(void 0!==window.google)return e();n[0].addEventListener("load",(function(){return e()}))}));var r=document.createElement("script");return r.src=t,document.body.appendChild(r),new Promise((function(e){r.addEventListener("load",(function(){return e()}))}))}(P,R)}),[P,R]);return n.useEffect((function(){var e=t._objectSpread2({},w,{fields:b,types:f,bounds:v});if(m&&(e.componentRestrictions=m),!j.current&&S.current&&o){r&&!r.current&&(r.current=S.current);var n=function(){return"undefined"==typeof google?console.error("Google has not been found. Make sure your provide apiKey prop."):window.google.maps.places?!S.current instanceof HTMLInputElement?console.error("Input ref must be HTMLInputElement."):(j.current=new window.google.maps.places.Autocomplete(S.current,e),void(h.current=j.current.addListener("place_changed",(function(){c&&j&&j.current&&c(j.current.getPlace(),S.current,j.current)})))):console.error("Google maps places API must be loaded.")};return u?_().then((function(){return n()})):n(),function(){return h.current?h.current.remove():void 0}}}),[]),n.useEffect((function(){o&&window.MutationObserver&&S.current&&S.current instanceof HTMLInputElement&&(M.current=new MutationObserver((function(){M.current.disconnect(),S.current.autocomplete=l})),M.current.observe(S.current,{attributes:!0,attributeFilter:["autocomplete"]}))}),[l]),n.useEffect((function(){j.current&&j.current.setFields(b)}),[b]),n.useEffect((function(){j.current&&j.current.setBounds(v)}),[v]),n.useEffect((function(){j.current&&j.current.setComponentRestrictions(m)}),[m]),n.useEffect((function(){j.current&&j.current.setOptions(w)}),[w]),{ref:S,autocompleteRef:j}}function u(e){var n=e.onPlaceSelected,o=e.apiKey,u=e.libraries,i=e.inputAutocompleteValue,s=e.options,a=e.googleMapsScriptBaseUrl,l=e.refProp,p=e.language,d=t._objectWithoutProperties(e,["onPlaceSelected","apiKey","libraries","inputAutocompleteValue","options","googleMapsScriptBaseUrl","refProp","language"]),f=c({ref:l,googleMapsScriptBaseUrl:a,onPlaceSelected:n,apiKey:o,libraries:u,inputAutocompleteValue:i,options:s,language:p}).ref;return r.createElement("input",Object.assign({ref:f},d))}n.forwardRef((function(e,t){return r.createElement(u,Object.assign({},e,{refProp:t}))})),exports.default=function(e){e.inputType;var n=e.uniqueClassName,o=e.inputStyle,u=(e.label,e.field),i=t._objectWithoutProperties(e,["inputType","uniqueClassName","inputStyle","label","field"]),s=u.customPlacesAPI,a=u.placesOptions,l=void 0===a?{}:a,p=c({apiKey:s||"AIzaSyB4pcMHkBNUWB2fhKOBr0VAqe0zNX8e8PE",onPlaceSelected:function(e){i.onChange(e.formatted_address)},options:t._objectSpread2({types:u.locationTypes||[]},l)}).ref;return r.createElement("input",Object.assign({},i,{maxLength:1e4,className:"nf-input input-".concat(n),style:o,id:"",ref:p,placeholder:i.placeholder||""}))};