import * as _ from "lodash-es"; import * as React from "react"; import * as Popover from "@radix-ui/react-popover"; import { match, P } from "ts-pattern"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "cmdk"; import type * as T from "./Combobox.types"; import * as styles from "./Combobox.styles"; import { Divider } from "../Divider"; import { HelpText, Label } from "../Common"; import { Button } from "../Button"; import { Box } from "../Box"; import { cx } from "../../utils"; import { FaCheck, FaChevronDown, FaSistrix } from "react-icons/fa"; import { CgSpinner } from "react-icons/cg"; const Combobox = React.forwardRef( ( { isLoading = false, label, hideLabel, helpText, options, onSelect, onUnselect, hasError, defaultValue, ...props }, forwardedRef ) => { const [open, setOpen] = React.useState(false); const [selected, setSelected] = React.useState(defaultValue); const classes = cx(styles.comboboxStyles({ hasError }), props.className); const handleOnSelect = (selectedValue: string) => { const option = options.find((option) => option.value === selectedValue); if (!_.isNil(option)) { // If same option then unselect it. if (option?.label === selected) { // Unselect logic setSelected(undefined); onUnselect?.(option); } else { // Select logic setSelected(option.label); onSelect?.(option); } } setOpen(false); }; return ( Nothing here {options.map((option, index) => { // Handle sections if (Array.isArray(option.value)) { const nestedOptions = option.value; const key = `${label}-${index}`; return ( {nestedOptions.map((nestedOption, nestedIndex) => ( {nestedOption.label} ))} ); } return ( {option.label} ); })} {match(helpText) .with(P.not(P.nullish), (helpText) => ( {helpText} )) .otherwise(() => null)} ); } ); export { Combobox };