|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { |
| 3 | + MenuItem, |
| 4 | + AutocompleteProps as MuiAutocompleteProps, |
| 5 | +} from "@mui/material"; |
| 6 | + |
| 7 | +import { Autocomplete, AutocompleteProps } from "./subFormComponents/Autocomplete"; |
| 8 | +import { useGeocoder } from "../../hooks/useGeocoder"; |
| 9 | +import { useMemoizedArray } from "../../hooks/useMemoizedArray"; |
| 10 | +import { getDefaultRequiredVal } from "../../helpers/util"; |
| 11 | +import { Nullable } from "../../types/common"; |
| 12 | +import { DEBOUNCE_DURATION, MIN_SEARCH_LENGTH } from "../../constants/geocoder"; |
| 13 | + |
| 14 | +export interface GeocoderInputProps< |
| 15 | + DisableClearable extends boolean | undefined, |
| 16 | + ChipComponent extends React.ElementType, |
| 17 | +> extends Omit< |
| 18 | + AutocompleteProps< |
| 19 | + string, |
| 20 | + false, |
| 21 | + DisableClearable, |
| 22 | + true, |
| 23 | + ChipComponent |
| 24 | + >, |
| 25 | + "autocompleteProps" |
| 26 | +> { |
| 27 | + autocompleteProps?: Omit< |
| 28 | + MuiAutocompleteProps<string, false, DisableClearable, true, ChipComponent>, |
| 29 | + "renderInput" |
| 30 | + | "options" |
| 31 | + | "renderOption" |
| 32 | + | "isOptionEqualToValue" |
| 33 | + | "value" |
| 34 | + | "onChange" |
| 35 | + | "onInputChange" |
| 36 | + >; |
| 37 | + selectedAddress?: Nullable<string>; |
| 38 | + onSelectAddress?: (address: string) => void; |
| 39 | + onAddressSearchChange?: (searchString: string) => void; |
| 40 | +} |
| 41 | + |
| 42 | +export const GeocoderInput = < |
| 43 | + DisableClearable extends boolean | undefined = false, |
| 44 | + ChipComponent extends React.ElementType = "div", |
| 45 | +>(props: GeocoderInputProps< |
| 46 | + DisableClearable, |
| 47 | + ChipComponent |
| 48 | +>) => { |
| 49 | + const { |
| 50 | + autocompleteProps, |
| 51 | + label, |
| 52 | + classes, |
| 53 | + helperText, |
| 54 | + onSelectAddress, |
| 55 | + onAddressSearchChange, |
| 56 | + } = props; |
| 57 | + |
| 58 | + // Already previously selected address to search for, or empty if it doesn't exist |
| 59 | + const selectedAddress = getDefaultRequiredVal("", props.selectedAddress); |
| 60 | + |
| 61 | + // This is the search string that appears in the input textfield |
| 62 | + const [searchString, setSearchString] = useState<string>(selectedAddress); |
| 63 | + |
| 64 | + // This is the same as the searchString, except it's only set after a debounce period |
| 65 | + const [debouncedSearchString, setDebouncedSearchString] = useState<string>(selectedAddress); |
| 66 | + const [isOpen, setIsOpen] = useState<boolean>(false); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + setSearchString(selectedAddress); |
| 70 | + }, [selectedAddress]); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + // We need to debounce the search string to throttle the Geocoder API calls |
| 74 | + const debounceTimeout = setTimeout(() => { |
| 75 | + setDebouncedSearchString(searchString); |
| 76 | + }, DEBOUNCE_DURATION); |
| 77 | + |
| 78 | + return () => clearTimeout(debounceTimeout); |
| 79 | + }, [searchString]); |
| 80 | + |
| 81 | + // Search for addresses based on the debounced search string |
| 82 | + const { data: geocoderResults, isLoading } = useGeocoder({ |
| 83 | + address: debouncedSearchString, |
| 84 | + enableSearch: isOpen && (debouncedSearchString.trim().length >= MIN_SEARCH_LENGTH), |
| 85 | + }); |
| 86 | + |
| 87 | + const addressSuggestions = useMemoizedArray( |
| 88 | + getDefaultRequiredVal( |
| 89 | + [], |
| 90 | + geocoderResults?.features?.map(({ properties }) => properties.fullAddress), |
| 91 | + ), |
| 92 | + (result) => result, |
| 93 | + (result1, result2) => result1 === result2, |
| 94 | + ); |
| 95 | + |
| 96 | + const handleOpen = () => { |
| 97 | + setIsOpen(true); |
| 98 | + }; |
| 99 | + |
| 100 | + const handleClose = () => { |
| 101 | + setIsOpen(false); |
| 102 | + if (!searchString) { |
| 103 | + // If inputted search string is empty when exiting geocoder input, |
| 104 | + // set selected address to be empty |
| 105 | + onSelectAddress?.(""); |
| 106 | + } else if (searchString !== selectedAddress) { |
| 107 | + // If upon exiting geocoder, the inputted search string is partial, |
| 108 | + // and none of the address options where selected, |
| 109 | + // or if no available options to select due to search string being invalid |
| 110 | + // then set selected address to be previously selected address |
| 111 | + setSearchString(selectedAddress); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + return ( |
| 116 | + <Autocomplete |
| 117 | + label={label} |
| 118 | + classes={classes} |
| 119 | + helperText={helperText} |
| 120 | + autocompleteProps={{ |
| 121 | + ...autocompleteProps, |
| 122 | + freeSolo: true, |
| 123 | + options: addressSuggestions, |
| 124 | + filterOptions: (options) => options, |
| 125 | + renderOption: (props, option) => ( |
| 126 | + <MenuItem {...props} key={option} value={option}> |
| 127 | + {option} |
| 128 | + </MenuItem> |
| 129 | + ), |
| 130 | + isOptionEqualToValue: (option, value) => |
| 131 | + option === value, |
| 132 | + open: isOpen, |
| 133 | + onOpen: handleOpen, |
| 134 | + onClose: handleClose, |
| 135 | + value: selectedAddress, |
| 136 | + inputValue: searchString, |
| 137 | + onChange: (_, value) => { |
| 138 | + if (!value) { |
| 139 | + onSelectAddress?.(""); |
| 140 | + } else { |
| 141 | + onSelectAddress?.(value); |
| 142 | + } |
| 143 | + }, |
| 144 | + onInputChange: (_, value) => { |
| 145 | + setSearchString(value); |
| 146 | + onAddressSearchChange?.(value); |
| 147 | + }, |
| 148 | + loading: isLoading, |
| 149 | + }} |
| 150 | + /> |
| 151 | + ); |
| 152 | +}; |
0 commit comments