Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type P = {
  2.   label: string
  3.   setPlace: (place: Place) => void
  4. }
  5.  
  6. const GooglePlacesAutocomplete = ({ label, setPlace }: P) => {
  7.   const [query, setQuery] = useState('')
  8.  
  9.   let textInput = React.createRef<HTMLInputElement>()
  10.  
  11.   const handleChange = ({
  12.     currentTarget: { value },
  13.   }: React.ChangeEvent<HTMLInputElement>) => {
  14.     setQuery(value)
  15.   }
  16.  
  17.   const handlePlaceSelect = (autocomplete: google.maps.places.Autocomplete) => {
  18.     let addressObject = autocomplete.getPlace()
  19.  
  20.     if (addressObject.geometry && addressObject.address_components) {
  21.       let place: Place = {}
  22.       place.name = addressObject.address_components[0].short_name
  23.       place.lat = addressObject.geometry.location.lat()
  24.       place.lng = addressObject.geometry.location.lng()
  25.       if (place) {
  26.         setQuery(place.name)
  27.         setPlace(place)
  28.       }
  29.     }
  30.   }
  31.  
  32.   const handleLoad = () => {
  33.     // Options for Autocomplete
  34.     const options = { types: [`(cities)`] }
  35.  
  36.     // Initialise Google Autocomplete
  37.     if (textInput.current) {
  38.       /*global google*/
  39.       const autocomplete = new google.maps.places.Autocomplete(
  40.         textInput.current,
  41.         options,
  42.       )
  43.  
  44.       // Set initial restrict to the greater list of countries.
  45.       // Currently restricted to the UK
  46.       autocomplete.setComponentRestrictions({
  47.         country: ['gb', 'fr', 'pt', 'es'],
  48.       })
  49.  
  50.       // Avoid paying for data that you don't need by restricting the
  51.       // set of place fields that are returned to just the address
  52.       // components and formatted address
  53.       autocomplete.setFields([
  54.         'address_components',
  55.         'formatted_address',
  56.         'geometry',
  57.       ])
  58.  
  59.       // Fire Event when a suggested name is selected
  60.       autocomplete.addListener('place_changed', () => {
  61.         handlePlaceSelect(autocomplete)
  62.       })
  63.     } else {
  64.       throw new Error(
  65.         `Google maps failed to initialise. Is the ref target correct?`,
  66.       )
  67.     }
  68.   }
  69.  
  70.   return (
  71.     <>
  72.       <Script
  73.         url={`...google api url`}
  74.         onLoad={handleLoad}
  75.       />
  76.       <TextInputLabel
  77.         htmlFor="gpac"
  78.         css={{
  79.           marginTop: '20px',
  80.         }}
  81.       >
  82.         {label}
  83.       </TextInputLabel>
  84.       <Input
  85.         id="gpac"
  86.         type="text"
  87.         value={query}
  88.         className={query ? 'suggestions-active' : ''}
  89.         onChange={handleChange}
  90.         placeholder="eg London"
  91.         ref={textInput}
  92.         spellCheck={false}
  93.         autoComplete="none"
  94.       />
  95.     </>
  96.   )
  97. }
  98.  
  99. export default GooglePlacesAutocomplete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement