|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + Select, |
| 4 | + SelectOption, |
| 5 | + SelectList, |
| 6 | + MenuToggle, |
| 7 | + MenuToggleElement, |
| 8 | + ChipGroup, |
| 9 | + Chip, |
| 10 | + TextInputGroup, |
| 11 | + TextInputGroupMain, |
| 12 | + TextInputGroupUtilities, |
| 13 | + Button, |
| 14 | +} from '@patternfly/react-core'; |
| 15 | +import { TimesIcon } from '@patternfly/react-icons/dist/esm/icons/times-icon'; |
| 16 | +import { ContextOption } from './utils'; |
| 17 | + |
| 18 | +type ContextSelectListProps = { |
| 19 | + allContexts: ContextOption[]; |
| 20 | + filteredContexts: ContextOption[]; |
| 21 | + onSelect: (contextName: string) => void; |
| 22 | + inputValue: string; |
| 23 | + onInputValueChange: (value: string) => void; |
| 24 | + onRemoveAll: () => void; |
| 25 | + editing: boolean; |
| 26 | +}; |
| 27 | + |
| 28 | +export const ContextSelectList: React.FC<ContextSelectListProps> = ({ |
| 29 | + allContexts, |
| 30 | + filteredContexts, |
| 31 | + onSelect, |
| 32 | + onRemoveAll, |
| 33 | + inputValue, |
| 34 | + onInputValueChange, |
| 35 | + editing, |
| 36 | +}) => { |
| 37 | + const [isOpen, setIsOpen] = useState(false); |
| 38 | + const [focusedItemIndex, setFocusedItemIndex] = useState<number | null>(null); |
| 39 | + const [activeItemId, setActiveItemId] = React.useState<string | null>(null); |
| 40 | + const textInputRef = React.useRef<HTMLInputElement>(); |
| 41 | + |
| 42 | + const NO_RESULTS = 'No results found'; |
| 43 | + |
| 44 | + // Open the dropdown if the input value changes |
| 45 | + React.useEffect(() => { |
| 46 | + if (inputValue) { |
| 47 | + setIsOpen(true); |
| 48 | + } |
| 49 | + }, [inputValue]); |
| 50 | + |
| 51 | + // Utility function to create a unique item ID based on the context value |
| 52 | + const createItemId = (value: string) => `select-multi-typeahead-${value.replace(' ', '-')}`; |
| 53 | + |
| 54 | + // Set both the focused and active item for keyboard navigation |
| 55 | + const setActiveAndFocusedItem = (itemIndex: number) => { |
| 56 | + setFocusedItemIndex(itemIndex); |
| 57 | + const focusedItem = filteredContexts[itemIndex]; |
| 58 | + setActiveItemId(createItemId(focusedItem.name)); |
| 59 | + }; |
| 60 | + |
| 61 | + // Reset focused and active items when the dropdown is closed or input is cleared |
| 62 | + const resetActiveAndFocusedItem = () => { |
| 63 | + setFocusedItemIndex(null); |
| 64 | + setActiveItemId(null); |
| 65 | + }; |
| 66 | + |
| 67 | + // Close the dropdown menu and reset focus states |
| 68 | + const closeMenu = () => { |
| 69 | + setIsOpen(false); |
| 70 | + resetActiveAndFocusedItem(); |
| 71 | + }; |
| 72 | + |
| 73 | + // Handle the input field click event to toggle the dropdown |
| 74 | + const onInputClick = () => { |
| 75 | + if (!isOpen) { |
| 76 | + setIsOpen(true); |
| 77 | + } else if (!inputValue) { |
| 78 | + closeMenu(); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + // Gets the index of the next element we want to focus on, based on the length of |
| 83 | + // the filtered contexts and the arrow key direction. |
| 84 | + const getNextFocusedIndex = ( |
| 85 | + currentIndex: number | null, |
| 86 | + length: number, |
| 87 | + direction: 'up' | 'down', |
| 88 | + ) => { |
| 89 | + if (direction === 'up') { |
| 90 | + return currentIndex === null || currentIndex === 0 ? length - 1 : currentIndex - 1; |
| 91 | + } |
| 92 | + return currentIndex === null || currentIndex === length - 1 ? 0 : currentIndex + 1; |
| 93 | + }; |
| 94 | + |
| 95 | + // Handle up/down arrow key navigation for the dropdown |
| 96 | + const handleMenuArrowKeys = (key: string) => { |
| 97 | + // If we're pressing the arrow keys, make sure the list is open. |
| 98 | + if (!isOpen) { |
| 99 | + setIsOpen(true); |
| 100 | + } |
| 101 | + const direction = key === 'ArrowUp' ? 'up' : 'down'; |
| 102 | + const indexToFocus = getNextFocusedIndex(focusedItemIndex, filteredContexts.length, direction); |
| 103 | + setActiveAndFocusedItem(indexToFocus); |
| 104 | + }; |
| 105 | + |
| 106 | + // Handle keydown events in the input field (e.g., Enter, Arrow keys) |
| 107 | + const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { |
| 108 | + const focusedItem = focusedItemIndex !== null ? filteredContexts[focusedItemIndex] : null; |
| 109 | + |
| 110 | + if (event.key === 'Enter' && focusedItem && focusedItem.name !== NO_RESULTS) { |
| 111 | + onSelect(focusedItem.name); |
| 112 | + } |
| 113 | + |
| 114 | + if (['ArrowUp', 'ArrowDown'].includes(event.key)) { |
| 115 | + handleMenuArrowKeys(event.key); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + // Handle selection of a context from the dropdown |
| 120 | + const handleSelect = (value: string) => { |
| 121 | + onSelect(value); |
| 122 | + textInputRef.current?.focus(); |
| 123 | + }; |
| 124 | + |
| 125 | + // Toggle the dropdown open/closed |
| 126 | + const onToggleClick = () => { |
| 127 | + setIsOpen(!isOpen); |
| 128 | + textInputRef?.current?.focus(); |
| 129 | + }; |
| 130 | + |
| 131 | + // Handle changes to the input field value |
| 132 | + const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => { |
| 133 | + // Update input value |
| 134 | + onInputValueChange(value); |
| 135 | + resetActiveAndFocusedItem(); |
| 136 | + }; |
| 137 | + |
| 138 | + const renderToggle = (toggleRef: React.Ref<MenuToggleElement>) => ( |
| 139 | + <MenuToggle |
| 140 | + variant="typeahead" |
| 141 | + aria-label="Multi typeahead menu toggle" |
| 142 | + onClick={onToggleClick} |
| 143 | + innerRef={toggleRef} |
| 144 | + isExpanded={isOpen} |
| 145 | + style={{ minWidth: '750px' } as React.CSSProperties} |
| 146 | + data-test="context-dropdown-toggle" |
| 147 | + > |
| 148 | + <TextInputGroup isPlain> |
| 149 | + <TextInputGroupMain |
| 150 | + value={inputValue} |
| 151 | + onChange={onTextInputChange} |
| 152 | + onClick={onInputClick} |
| 153 | + onKeyDown={onInputKeyDown} |
| 154 | + data-test="multi-typeahead-select-input" |
| 155 | + id="multi-typeahead-select-input" |
| 156 | + autoComplete="off" |
| 157 | + innerRef={textInputRef} |
| 158 | + placeholder="Select a context" |
| 159 | + {...(activeItemId && { 'aria-activedescendant': activeItemId })} |
| 160 | + role="combobox" |
| 161 | + isExpanded={isOpen} |
| 162 | + aria-controls="select-multi-typeahead-listbox" |
| 163 | + > |
| 164 | + <ChipGroup> |
| 165 | + {allContexts |
| 166 | + .filter((ctx) => ctx.selected) |
| 167 | + .map((ctx) => ( |
| 168 | + <Chip |
| 169 | + key={ctx.name} |
| 170 | + onClick={() => handleSelect(ctx.name)} |
| 171 | + data-test={`context-chip-${ctx.name}`} |
| 172 | + > |
| 173 | + {ctx.name} |
| 174 | + </Chip> |
| 175 | + ))} |
| 176 | + </ChipGroup> |
| 177 | + </TextInputGroupMain> |
| 178 | + {filteredContexts.some((ctx) => ctx.selected) && ( |
| 179 | + <TextInputGroupUtilities> |
| 180 | + <Button variant="plain" onClick={onRemoveAll} data-test={'clear-button'}> |
| 181 | + <TimesIcon aria-hidden /> |
| 182 | + </Button> |
| 183 | + </TextInputGroupUtilities> |
| 184 | + )} |
| 185 | + </TextInputGroup> |
| 186 | + </MenuToggle> |
| 187 | + ); |
| 188 | + |
| 189 | + return ( |
| 190 | + <Select |
| 191 | + isOpen={isOpen} |
| 192 | + onSelect={(_event, value) => handleSelect(value as string)} |
| 193 | + onOpenChange={closeMenu} |
| 194 | + style={{ maxWidth: '750px' } as React.CSSProperties} |
| 195 | + toggle={renderToggle} |
| 196 | + > |
| 197 | + <SelectList id="select-multi-typeahead-listbox" data-test={'context-option-select-list'}> |
| 198 | + {filteredContexts.map((ctx, idx) => ( |
| 199 | + <SelectOption |
| 200 | + id={ctx.name} |
| 201 | + key={ctx.name} |
| 202 | + isFocused={focusedItemIndex === idx} |
| 203 | + value={ctx.name} |
| 204 | + isSelected={ctx.selected} |
| 205 | + description={ctx.description} |
| 206 | + ref={null} |
| 207 | + isDisabled={!editing && ctx.name === 'application'} |
| 208 | + data-test={`context-option-${ctx.name}`} |
| 209 | + > |
| 210 | + {ctx.name} |
| 211 | + </SelectOption> |
| 212 | + ))} |
| 213 | + </SelectList> |
| 214 | + </Select> |
| 215 | + ); |
| 216 | +}; |
0 commit comments