setQuery("")}
- onClose={() => setMenuOpen(false)}
- onKeyDown={(event) => {
- if (!menuOpen) {
- return
- }
- if (event.key === "Escape") {
- setQuery("")
- }
- if (!inputHasFocus) {
- // Allow for editing the query without the input having focus
- if (event.key === "Backspace") {
- setQuery(query.slice(0, query.length - 1))
- } else if (event.key.length === 1) {
- setQuery(query + event.key)
+
+ setQuery("")}
+ onClose={() => setMenuOpen(false)}
+ onKeyDown={(event) => {
+ if (!menuOpen) {
+ return
}
- }
- if (options.length === 0) {
- return
- }
- if (event.key === "ArrowUp" || event.key === "ArrowDown") {
- let newIndex
- if (event.key === "ArrowUp") {
- newIndex = Math.max(selectedItem - 1, 0)
- } else {
- newIndex = Math.min(selectedItem + 1, options.length - 1)
+ if (event.key === "Escape") {
+ setQuery("")
+ }
+ if (!inputHasFocus) {
+ // Allow for editing the query without the input having focus
+ if (event.key === "Backspace") {
+ setQuery(query.slice(0, query.length - 1))
+ } else if (event.key.length === 1) {
+ setQuery(query + event.key)
+ }
}
- setSelectedItem(newIndex)
- event.target
- .querySelectorAll("[role='option']")
+ if (options.length === 0) {
+ return
+ }
+ if (event.key === "ArrowUp" || event.key === "ArrowDown") {
+ let newIndex
+ if (event.key === "ArrowUp") {
+ newIndex = Math.max(selectedItem - 1, 0)
+ } else {
+ newIndex = Math.min(selectedItem + 1, options.length - 1)
+ }
+ setSelectedItem(newIndex)
+ event.target
+ .querySelectorAll("[role='option']")
[newIndex]?.scrollIntoView({ block: "nearest" })
+ }
+ if (event.key === "Enter") {
+ onClick(options[selectedItem].value)
+ }
+ }}
+ onOpen={() => setMenuOpen(true)}
+ selectOnBlur={false}
+ selectOnNavigation={false}
+ trigger={
+ <>
+ {`Add ${itemType} `}
+ >
}
- if (event.key === "Enter") {
- onClick(options[selectedItem].value)
- }
- }}
- onOpen={() => setMenuOpen(true)}
- selectOnBlur={false}
- selectOnNavigation={false}
- trigger={
- <>
- {`Add ${itemType} `}
- >
- }
- value={null} // Without this, a selected item becomes active (shown bold in the menu) and can't be selected again
- >
-
- {`Available ${itemType} types`}
-
- {
- setInputHasFocus(false)
- if (allItemSubtypes) {
- event.stopPropagation()
- } // Prevent tabbing to the checkbox from clearing the input
- }}
- onChange={(_event, { value }) => setQuery(value)}
- onClick={stopEventPropagation}
- onFocus={() => {
- setInputHasFocus(true)
- }}
- onKeyDown={stopEventPropagationOnSpace}
- placeholder={`Filter ${itemType} types`}
- value={query}
- />
- {allItemSubtypes?.length > 0 && (
-
+
+ {`Available ${itemType} types`}
+
+ {
+ setInputHasFocus(false)
+ if (allItemSubtypes) {
+ event.stopPropagation()
+ } // Prevent tabbing to the checkbox from clearing the input
+ }}
+ onChange={(_event, { value }) => setQuery(value)}
+ onClick={stopEventPropagation}
+ onFocus={() => {
+ setInputHasFocus(true)
+ }}
+ onKeyDown={stopEventPropagationOnSpace}
+ placeholder={`Filter ${itemType} types`}
+ value={query}
/>
- )}
- {usedItemSubtypeKeys?.length > 0 && (
-
- )}
-
- {options.map((option, index) => (
- onClick(value)}
- selected={selectedItem === index}
- text={option.text}
- value={option.value}
+ {allItemSubtypes?.length > 0 && (
+
+ )}
+ {usedItemSubtypeKeys?.length > 0 && (
+
- ))}
+ )}
+
+ {options.map((option, index) => (
+ onClick(value)}
+ selected={selectedItem === index}
+ text={option.text}
+ value={option.value}
+ />
+ ))}
+
-
-
+
+
}
/>
)