// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useId, useState} from 'react'; import {Card} from '@astryxdesign/core/Card'; import {useAnnounce} from '@astryxdesign/core/hooks'; import {Item} from '@astryxdesign/core/Item'; import {VStack} from '@astryxdesign/core/Layout'; import {useCombobox} from '@astryxdesign/core/Selector'; import {Text} from '@astryxdesign/core/Text'; import {TextInput} from '@astryxdesign/core/TextInput'; type Teammate = {value: string; label: string; disabled?: boolean}; const TEAMMATES: Teammate[] = [ {value: 'ada', label: 'Ada Lovelace'}, {value: 'alan', label: 'Alan Turing'}, {value: 'grace', label: 'Grace Hopper (away)', disabled: true}, {value: 'katherine', label: 'Katherine Johnson'}, {value: 'margaret', label: 'Margaret Hamilton'}, ]; function matching(query: string): Teammate[] { const needle = query.trim().toLowerCase(); return TEAMMATES.filter(teammate => teammate.label.toLowerCase().includes(needle), ); } export default function UseComboboxHookUsage() { const listboxId = useId(); const announce = useAnnounce({hasReplace: true}); const [query, setQuery] = useState(''); const [isOpen, setIsOpen] = useState(false); const [assignee, setAssignee] = useState(); const options = matching(query); const combobox = useCombobox({ selectableItems: options, isOpen, onOpen: () => setIsOpen(true), onClose: () => setIsOpen(false), onSelect: value => { setAssignee(TEAMMATES.find(teammate => teammate.value === value)?.label); setQuery(''); }, listboxId, }); return ( { const matches = matching(next); setQuery(next); setIsOpen(true); // A new query makes its first enabled match active, so Enter // picks it. combobox.setHighlightedIndex( matches.findIndex(teammate => !teammate.disabled), ); if (next.trim() !== query.trim()) { announce( matches.length === 0 ? 'No one matches' : matches.length === 1 ? '1 teammate' : `${matches.length} teammates`, ); } }} onBlur={() => { setIsOpen(false); combobox.setHighlightedIndex(-1); }} {...combobox.getFieldProps()} /> {isOpen && options.length > 0 ? ( {options.map((teammate, index) => ( ))} ) : null} {isOpen && options.length === 0 ? ( No one matches ) : null} {assignee == null ? 'Unassigned' : `Assigned to ${assignee}`} ); }