// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useId, useRef, useState} from 'react'; import {CheckIcon} from '@heroicons/react/24/outline'; import {Card} from '@astryxdesign/core/Card'; import {useAnnounce} from '@astryxdesign/core/hooks'; import {Icon} from '@astryxdesign/core/Icon'; import {Item} from '@astryxdesign/core/Item'; import {HStack, VStack} from '@astryxdesign/core/Layout'; import {useMultiCombobox} from '@astryxdesign/core/MultiSelector'; import {TextInput} from '@astryxdesign/core/TextInput'; import {Token} from '@astryxdesign/core/Token'; import {isImeKeyEvent} from '@astryxdesign/core/utils'; const LABELS = [ {value: 'bug', label: 'Bug'}, {value: 'docs', label: 'Documentation'}, {value: 'a11y', label: 'Accessibility'}, {value: 'perf', label: 'Performance'}, {value: 'good-first-issue', label: 'Good first issue'}, ]; function filterLabels(query: string) { const needle = query.trim().toLowerCase(); return LABELS.filter(item => item.label.toLowerCase().includes(needle)); } export default function UseMultiComboboxHookUsage() { const listboxId = useId(); const inputRef = useRef(null); const [query, setQuery] = useState(''); const [isOpen, setIsOpen] = useState(false); const [selected, setSelected] = useState(['bug']); const matches = filterLabels(query); const announce = useAnnounce({hasReplace: true}); const toggle = (value: string) => setSelected(current => current.includes(value) ? current.filter(item => item !== value) : [...current, value], ); const combobox = useMultiCombobox({ selectableItems: matches, isOpen, hasSearch: true, // With no match there is no option to point at, so the list stays shut. onOpen: () => { if (matches.length === 0) { return false; } setIsOpen(true); }, onClose: () => setIsOpen(false), onToggle: toggle, listboxId, }); return ( {LABELS.filter(item => selected.includes(item.value)).map(item => ( { toggle(item.value); inputRef.current?.focus(); }} /> ))} { const nextMatches = filterLabels(nextQuery); setQuery(nextQuery); setIsOpen(true); combobox.setHighlightedIndex(nextMatches.length > 0 ? 0 : -1); if (nextQuery.trim() !== query.trim()) { announce( nextMatches.length === 0 ? 'No labels match' : nextMatches.length === 1 ? '1 label' : `${nextMatches.length} labels`, ); } }} onClick={combobox.onTriggerClick} onKeyDown={event => { // Keys that compose a Korean, Japanese or Chinese syllable belong // to the input method. Home, End, PageUp and PageDown keep their // text-field meaning. if ( isImeKeyEvent(event.nativeEvent) || ['Home', 'End', 'PageUp', 'PageDown'].includes(event.key) ) { return; } // With no active option, Enter submits a surrounding form. if (event.key === 'Enter' && combobox.activeDescendantId == null) { return; } combobox.onKeyDown(event); }} onBlur={() => { setIsOpen(false); combobox.setHighlightedIndex(-1); }} /> {isOpen && ( event.preventDefault()}> {matches.map((item, index) => ( ) } isHighlighted={index === combobox.highlightedIndex} onClick={() => toggle(item.value)} // Highlight follows pointer movement: a list that opens under // a resting pointer keeps the active option the keys set. onMouseMove={() => combobox.onItemMouseEnter(item, index)} /> ))} )} ); }