import React, { useRef, useState } from 'react'; import { Trans } from '@lingui/react/macro'; import { PopoverAnchor } from '@radix-ui/react-popover'; import { Popover, PopoverContent } from '@documenso/ui/primitives/popover'; import { Command, CommandGroup, CommandItem } from '../../primitives/command'; import { Input } from '../../primitives/input'; export type RecipientAutoCompleteOption = { email: string; name: string | null; }; type RecipientAutoCompleteInputProps = { type: 'email' | 'text'; value: string; placeholder?: string; disabled?: boolean; loading?: boolean; options: RecipientAutoCompleteOption[]; onSelect: (option: RecipientAutoCompleteOption) => void; onSearchQueryChange: (query: string) => void; }; type CombinedProps = RecipientAutoCompleteInputProps & Omit, keyof RecipientAutoCompleteInputProps>; export const RecipientAutoCompleteInput = ({ value, placeholder, disabled, loading, onSearchQueryChange, onSelect, options = [], onChange: _onChange, ...props }: CombinedProps) => { const [isOpen, setIsOpen] = useState(false); const inputRef = useRef(null); const onValueChange = (value: string) => { setIsOpen(!!value.length); onSearchQueryChange(value); }; const handleSelectItem = (option: RecipientAutoCompleteOption) => { setIsOpen(false); onSelect(option); }; return ( onValueChange(e.target.value)} {...props} /> { e.preventDefault(); }} > {/* Not using here due to some weird behaviour */} {options.length === 0 && (
{loading ? ( Loading suggestions... ) : ( No suggestions found )}
)} {options.length > 0 && ( {options.map((option, index) => ( handleSelectItem(option)} > {option.name} ({option.email}) ))} )}
); };