import { Combobox } from '@headlessui/react'; import { FieldHookConfig, useField } from 'formik'; import React, { useEffect, useState } from 'react'; import PlacesAutocomplete, { geocodeByAddress, getLatLng, } from 'react-places-autocomplete'; import { Location } from '../../Location/Location'; import { formStyles } from '../../../utils/formClasses'; import { MagnifyingGlassIcon, XMarkIcon } from '@heroicons/react/24/solid'; function classNames(...classes: any) { return classes.filter(Boolean).join(' '); } export interface PickerProps { label?: string; } export const PlacesAutoComplete = ({ label, ...props }: PickerProps & FieldHookConfig) => { const [field, meta, helper] = useField(props); const [address, setAddress] = useState(''); const [coordinates, setCoordinates] = useState({ lat: 45.3875812, lng: -75.69602019999999, }); const defaultLocation = field.value; const handleSelect = async (locationValue: any) => { const results = await geocodeByAddress(locationValue); const latLng = await getLatLng(results[0]); setAddress(locationValue); setCoordinates(latLng); helper.setValue(locationValue); }; useEffect(() => { const DefaultLocationCoordinates = async () => { const results = await geocodeByAddress(defaultLocation); console.log('results', results); const latLng = await getLatLng(results[0]); setCoordinates(latLng); helper.setValue(defaultLocation); }; DefaultLocationCoordinates(); }, [defaultLocation]); return (
{({ getInputProps, suggestions, getSuggestionItemProps }) => (
{suggestions.map(suggestion => { return ( classNames( 'cursor-default select-none px-4 py-2', active && 'bg-cu-red text-white' ) } >
{suggestion.description}
); })}
)}
); };