-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISPN-15284 Refactor Select: FeaturesSelector
- Loading branch information
Showing
3 changed files
with
230 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
Button, | ||
Chip, | ||
ChipGroup, | ||
MenuToggle, | ||
MenuToggleElement, | ||
Select, | ||
SelectList, | ||
SelectOption, | ||
SelectOptionProps, | ||
TextInputGroup, | ||
TextInputGroupMain, | ||
TextInputGroupUtilities | ||
} from '@patternfly/react-core'; | ||
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon'; | ||
|
||
const SelectMultiWithChips = (props: {id: string, placeholder:string, | ||
options: SelectOptionProps[], | ||
onSelect: (selection) => void, | ||
onClear: () => void, | ||
selection: string[] | ||
} | ||
) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [inputValue, setInputValue] = useState<string>(''); | ||
const [selected, setSelected] = useState<string[]>(props.selection); | ||
const [selectOptions, setSelectOptions] = useState<SelectOptionProps[]>(props.options); | ||
const [focusedItemIndex, setFocusedItemIndex] = useState<number | null>(null); | ||
const [activeItem, setActiveItem] = useState<string | null>(null); | ||
const textInputRef = React.useRef<HTMLInputElement>(); | ||
|
||
useEffect(() => { | ||
setSelected(props.selection); | ||
}, [props.selection]) | ||
|
||
useEffect(() => { | ||
let newSelectOptions: SelectOptionProps[] = props.options; | ||
|
||
// Filter menu items based on the text input value when one exists | ||
if (inputValue) { | ||
newSelectOptions = props.options.filter((menuItem) => | ||
String(menuItem.children).toLowerCase().includes(inputValue.toLowerCase()) | ||
); | ||
|
||
// When no options are found after filtering, display 'No results found' | ||
if (!newSelectOptions.length) { | ||
newSelectOptions = [ | ||
{ isDisabled: false, children: `No results found for "${inputValue}"`, value: 'no results' } | ||
]; | ||
} | ||
|
||
// Open the menu when the input value changes and the new value is not empty | ||
if (!isOpen) { | ||
setIsOpen(true); | ||
} | ||
} | ||
|
||
setSelectOptions(newSelectOptions); | ||
setFocusedItemIndex(null); | ||
setActiveItem(null); | ||
}, [inputValue]); | ||
|
||
const handleMenuArrowKeys = (key: string) => { | ||
let indexToFocus; | ||
|
||
if (isOpen) { | ||
if (key === 'ArrowUp') { | ||
// When no index is set or at the first index, focus to the last, otherwise decrement focus index | ||
if (focusedItemIndex === null || focusedItemIndex === 0) { | ||
indexToFocus = selectOptions.length - 1; | ||
} else { | ||
indexToFocus = focusedItemIndex - 1; | ||
} | ||
} | ||
|
||
if (key === 'ArrowDown') { | ||
// When no index is set or at the last index, focus to the first, otherwise increment focus index | ||
if (focusedItemIndex === null || focusedItemIndex === selectOptions.length - 1) { | ||
indexToFocus = 0; | ||
} else { | ||
indexToFocus = focusedItemIndex + 1; | ||
} | ||
} | ||
|
||
setFocusedItemIndex(indexToFocus); | ||
const focusedItem = selectOptions.filter((option) => !option.isDisabled)[indexToFocus]; | ||
setActiveItem(`select-multi-typeahead-${focusedItem.value.replace(' ', '-')}`); | ||
} | ||
}; | ||
|
||
const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
const enabledMenuItems = selectOptions.filter((menuItem) => !menuItem.isDisabled); | ||
const [firstMenuItem] = enabledMenuItems; | ||
const focusedItem = focusedItemIndex ? enabledMenuItems[focusedItemIndex] : firstMenuItem; | ||
|
||
switch (event.key) { | ||
// Select the first available option | ||
case 'Enter': | ||
if (!isOpen) { | ||
setIsOpen((prevIsOpen) => !prevIsOpen); | ||
} else if (isOpen && focusedItem.value !== 'no results') { | ||
onSelect(focusedItem.value as string); | ||
} | ||
break; | ||
case 'Tab': | ||
case 'Escape': | ||
setIsOpen(false); | ||
setActiveItem(null); | ||
break; | ||
case 'ArrowUp': | ||
case 'ArrowDown': | ||
event.preventDefault(); | ||
handleMenuArrowKeys(event.key); | ||
break; | ||
} | ||
}; | ||
|
||
const onToggleClick = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => { | ||
setInputValue(value); | ||
}; | ||
|
||
const onSelect = (value: string) => { | ||
if (value && value !== 'no results') { | ||
setSelected( | ||
selected.includes(value) ? selected.filter((selection) => selection !== value) : [...selected, value] | ||
); | ||
} | ||
|
||
textInputRef.current?.focus(); | ||
props.onSelect(value); | ||
}; | ||
|
||
const toggle = (toggleRef: React.Ref<MenuToggleElement>) => ( | ||
<MenuToggle variant="typeahead" onClick={onToggleClick} innerRef={toggleRef} isExpanded={isOpen} isFullWidth> | ||
<TextInputGroup isPlain> | ||
<TextInputGroupMain | ||
value={inputValue} | ||
onClick={onToggleClick} | ||
onChange={onTextInputChange} | ||
onKeyDown={onInputKeyDown} | ||
id={props.id + 'multi-typeahead-select-input'} | ||
autoComplete="off" | ||
innerRef={textInputRef} | ||
placeholder={props.placeholder} | ||
{...(activeItem && { 'aria-activedescendant': activeItem })} | ||
role="combobox" | ||
isExpanded={isOpen} | ||
aria-controls={props.id + 'select-multi-typeahead'} | ||
> | ||
<ChipGroup aria-label="Current selections"> | ||
{selected.map((selection, index) => ( | ||
<Chip | ||
key={index} | ||
onClick={(ev) => { | ||
ev.stopPropagation(); | ||
onSelect(selection); | ||
}} | ||
> | ||
{selection} | ||
</Chip> | ||
))} | ||
</ChipGroup> | ||
</TextInputGroupMain> | ||
<TextInputGroupUtilities> | ||
{selected.length > 0 && ( | ||
<Button | ||
variant="plain" | ||
onClick={() => { | ||
setInputValue(''); | ||
setSelected([]); | ||
props.onClear() | ||
textInputRef?.current?.focus(); | ||
}} | ||
aria-label="Clear input value" | ||
> | ||
<TimesIcon aria-hidden /> | ||
</Button> | ||
)} | ||
</TextInputGroupUtilities> | ||
</TextInputGroup> | ||
</MenuToggle> | ||
); | ||
|
||
return ( | ||
<Select | ||
id={props.id + 'multi-typeahead-select'} | ||
isOpen={isOpen} | ||
selected={selected} | ||
onSelect={(ev, selection) => onSelect(selection as string)} | ||
onOpenChange={() => setIsOpen(false)} | ||
toggle={toggle} | ||
> | ||
<SelectList isAriaMultiselectable id={props.id + 'select-multi-typeahead-listbox' }> | ||
{selectOptions.map((option, index) => ( | ||
<SelectOption | ||
key={option.value || option.children} | ||
isFocused={focusedItemIndex === index} | ||
className={option.className} | ||
id={`select-multi-typeahead-${option.value.replace(' ', '-')}`} | ||
{...option} | ||
ref={null} | ||
/> | ||
))} | ||
</SelectList> | ||
</Select> | ||
); | ||
}; | ||
|
||
export { SelectMultiWithChips } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters