|
| 1 | +import { Popover, PopoverAnchor } from '@radix-ui/react-popover'; |
| 2 | +import React, { useRef, useState } from 'react'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import type { TextFieldProps } from './TextField'; |
| 5 | +import { TextField } from './TextField'; |
| 6 | +import { PopoverContent } from '../popover/Popover'; |
| 7 | +import { Typography } from '../typography/Typography'; |
| 8 | +import { GenericLoaderSpinner } from '../utilities/loaders'; |
| 9 | +import { IconSize } from '../Icon'; |
| 10 | + |
| 11 | +interface AutocompleteProps |
| 12 | + extends Omit<TextFieldProps, 'inputId' | 'onChange' | 'onSelect'> { |
| 13 | + name: string; |
| 14 | + onChange: (value: string) => void; |
| 15 | + onSelect: (value: string) => void; |
| 16 | + selectedValue?: string; |
| 17 | + options: Array<{ value: string; label: string }>; |
| 18 | + isLoading?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +const Autocomplete = ({ |
| 22 | + name, |
| 23 | + isLoading, |
| 24 | + options, |
| 25 | + onChange, |
| 26 | + onSelect, |
| 27 | + selectedValue, |
| 28 | + defaultValue, |
| 29 | + ...restProps |
| 30 | +}: AutocompleteProps) => { |
| 31 | + const [input, setInput] = useState(defaultValue || ''); |
| 32 | + const [isOpen, setIsOpen] = useState(false); |
| 33 | + const inputRef = useRef<HTMLInputElement | null>(null); |
| 34 | + const handleChange = (val: string) => { |
| 35 | + setInput(val); |
| 36 | + onChange(val); |
| 37 | + }; |
| 38 | + const handleSelect = (opt: { value: string; label: string }) => { |
| 39 | + setInput(opt.label); |
| 40 | + onSelect(opt.value); |
| 41 | + setIsOpen(false); |
| 42 | + inputRef.current?.focus(); |
| 43 | + }; |
| 44 | + const handleBlur = () => { |
| 45 | + setIsOpen(false); |
| 46 | + setInput(options.find((opt) => opt.value === selectedValue)?.label || ''); |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Popover open={isOpen}> |
| 51 | + <PopoverAnchor asChild> |
| 52 | + <TextField |
| 53 | + inputRef={(ref) => { |
| 54 | + inputRef.current = ref; |
| 55 | + }} |
| 56 | + inputId={name} |
| 57 | + {...restProps} |
| 58 | + onChange={(e) => { |
| 59 | + handleChange(e.target.value); |
| 60 | + setIsOpen(true); |
| 61 | + }} |
| 62 | + onFocus={() => setIsOpen(true)} |
| 63 | + onBlur={handleBlur} |
| 64 | + value={input} |
| 65 | + autoComplete="off" |
| 66 | + /> |
| 67 | + </PopoverAnchor> |
| 68 | + <PopoverContent |
| 69 | + className="rounded-16 border border-border-subtlest-tertiary bg-background-popover p-4 data-[side=bottom]:mt-1 data-[side=top]:mb-1" |
| 70 | + side="bottom" |
| 71 | + align="start" |
| 72 | + avoidCollisions |
| 73 | + sameWidthAsAnchor |
| 74 | + onOpenAutoFocus={(e) => e.preventDefault()} // keep focus in input |
| 75 | + onCloseAutoFocus={(e) => e.preventDefault()} // avoid refocus jumps |
| 76 | + > |
| 77 | + {!isLoading ? ( |
| 78 | + <div className="flex w-full flex-col"> |
| 79 | + {options?.length > 0 ? ( |
| 80 | + options.map((opt) => ( |
| 81 | + <button |
| 82 | + type="button" |
| 83 | + className={classNames( |
| 84 | + 'text-left', |
| 85 | + selectedValue === opt.value && 'font-bold', |
| 86 | + )} |
| 87 | + key={opt.value} |
| 88 | + onMouseDown={(e) => { |
| 89 | + e.preventDefault(); |
| 90 | + handleSelect(opt); |
| 91 | + }} |
| 92 | + > |
| 93 | + {opt.label} |
| 94 | + </button> |
| 95 | + )) |
| 96 | + ) : ( |
| 97 | + <Typography>No results</Typography> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + ) : ( |
| 101 | + <GenericLoaderSpinner className="mx-auto" size={IconSize.Small} /> |
| 102 | + )} |
| 103 | + </PopoverContent> |
| 104 | + </Popover> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +export default Autocomplete; |
0 commit comments