import type {MenuTriggerAction} from "@heroui/react"; import React from "react"; import {Autocomplete, AutocompleteItem} from "@heroui/react"; import {useFilter} from "@react-aria/i18n"; export type FieldState = { selectedKey: React.Key | null; inputValue: string; items: typeof animals; }; export const animals = [ {label: "Cat", key: "cat", description: "The second most popular pet in the world"}, {label: "Dog", key: "dog", description: "The most popular pet in the world"}, {label: "Elephant", key: "elephant", description: "The largest land animal"}, {label: "Lion", key: "lion", description: "The king of the jungle"}, {label: "Tiger", key: "tiger", description: "The largest cat species"}, {label: "Giraffe", key: "giraffe", description: "The tallest land animal"}, { label: "Dolphin", key: "dolphin", description: "A widely distributed and diverse group of aquatic mammals", }, {label: "Penguin", key: "penguin", description: "A group of aquatic flightless birds"}, {label: "Zebra", key: "zebra", description: "A several species of African equids"}, { label: "Shark", key: "shark", description: "A group of elasmobranch fish characterized by a cartilaginous skeleton", }, { label: "Whale", key: "whale", description: "Diverse group of fully aquatic placental marine mammals", }, {label: "Otter", key: "otter", description: "A carnivorous mammal in the subfamily Lutrinae"}, {label: "Crocodile", key: "crocodile", description: "A large semiaquatic reptile"}, ]; export default function App() { // Store Autocomplete input value, selected option, open state, and items // in a state tracker const [fieldState, setFieldState] = React.useState({ selectedKey: "", inputValue: "", items: animals, }); // Implement custom filtering logic and control what items are // available to the Autocomplete. const {startsWith} = useFilter({sensitivity: "base"}); // Specify how each of the Autocomplete values should change when an // option is selected from the list box const onSelectionChange = (key: React.Key | null) => { setFieldState((prevState) => { let selectedItem = prevState.items.find((option) => option.key === key); return { inputValue: selectedItem?.label || "", selectedKey: key, items: animals.filter((item) => startsWith(item.label, selectedItem?.label || "")), }; }); }; // Specify how each of the Autocomplete values should change when the input // field is altered by the user const onInputChange = (value: string) => { setFieldState((prevState) => ({ inputValue: value, selectedKey: value === "" ? null : prevState.selectedKey, items: animals.filter((item) => startsWith(item.label, value)), })); }; // Show entire list if user opens the menu manually const onOpenChange = (isOpen: boolean, menuTrigger: MenuTriggerAction) => { if (menuTrigger === "manual" && isOpen) { setFieldState((prevState) => ({ inputValue: prevState.inputValue, selectedKey: prevState.selectedKey, items: animals, })); } }; return ( {(item) => {item.label}} ); }