| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Divider, |
| 5 |
MenuToggle, |
| 6 |
MenuToggleElement, |
| 7 |
MenuSearch, |
| 8 |
MenuSearchInput, |
| 9 |
Dropdown, |
| 10 |
DropdownItem, |
| 11 |
DropdownList, |
| 12 |
SearchInput, |
| 13 |
} from "@patternfly/react-core"; |
| 14 |
import { useGetIDListMutation, GenericPayload } from "src/services/rpc"; |
| 15 |
|
| 16 |
interface DropdownProps { |
| 17 |
dataCy: string; |
| 18 |
id?: string; |
| 19 |
onSelect: (value: string) => void; |
| 20 |
options: string[]; |
| 21 |
ariaLabelledBy?: string; |
| 22 |
searchType: string; |
| 23 |
value: string; |
| 24 |
} |
| 25 |
|
| 26 |
const DropdownSearch = (props: DropdownProps) => { |
| 27 |
const [isOpen, setIsOpen] = React.useState(false); |
| 28 |
const [searchValue, setSearchValue] = React.useState(""); |
| 29 |
const [options, setOptions] = React.useState(props.options); |
| 30 |
|
| 31 |
const [retrieveList] = useGetIDListMutation({}); |
| 32 |
|
| 33 |
const onSelect = ( |
| 34 |
_event: React.MouseEvent<Element, MouseEvent> | undefined, |
| 35 |
selection: string | number | undefined |
| 36 |
) => { |
| 37 |
props.onSelect(selection as string); |
| 38 |
setSearchValue(""); |
| 39 |
setIsOpen(false); |
| 40 |
setOptions(props.options); |
| 41 |
}; |
| 42 |
|
| 43 |
const onToggleClick = () => { |
| 44 |
setIsOpen(!isOpen); |
| 45 |
}; |
| 46 |
|
| 47 |
// Search for specific items |
| 48 |
const submitSearchValue = (value: string) => { |
| 49 |
if (value === "") { |
| 50 |
// Reset options |
| 51 |
setOptions(props.options); |
| 52 |
return; |
| 53 |
} else { |
| 54 |
// Searching |
| 55 |
setOptions(["Searching ..."]); |
| 56 |
} |
| 57 |
retrieveList({ |
| 58 |
searchValue: value, |
| 59 |
sizeLimit: 200, |
| 60 |
startIdx: 0, |
| 61 |
stopIdx: 199, |
| 62 |
entryType: props.searchType, |
| 63 |
} as GenericPayload).then((result) => { |
| 64 |
if (result && "data" in result) { |
| 65 |
setOptions(result.data?.list ?? []); |
| 66 |
} else { |
| 67 |
setOptions([]); |
| 68 |
} |
| 69 |
setIsOpen(true); |
| 70 |
}); |
| 71 |
}; |
| 72 |
|
| 73 |
// Removed selected value from options |
| 74 |
const filteredOptions = options.filter((item) => item !== props.value); |
| 75 |
|
| 76 |
return ( |
| 77 |
<Dropdown |
| 78 |
data-cy={props.dataCy + "-dropdown"} |
| 79 |
id={props.id || "dropdown-search"} |
| 80 |
isOpen={isOpen} |
| 81 |
onSelect={onSelect} |
| 82 |
onOpenChange={(isOpen: boolean) => setIsOpen(isOpen)} |
| 83 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 84 |
<MenuToggle |
| 85 |
data-cy={props.dataCy + "-dropdown-toggle"} |
| 86 |
ref={toggleRef} |
| 87 |
isFullWidth |
| 88 |
onClick={onToggleClick} |
| 89 |
isExpanded={isOpen} |
| 90 |
> |
| 91 |
{props.value} |
| 92 |
</MenuToggle> |
| 93 |
)} |
| 94 |
ouiaId="BasicDropdown" |
| 95 |
shouldFocusToggleOnSelect |
| 96 |
isScrollable |
| 97 |
> |
| 98 |
<MenuSearch> |
| 99 |
<MenuSearchInput> |
| 100 |
<SearchInput |
| 101 |
data-cy={props.dataCy + "-dropdown-search"} |
| 102 |
value={searchValue} |
| 103 |
placeholder="Search" |
| 104 |
onChange={(_event, value) => setSearchValue(value)} |
| 105 |
onSearch={() => submitSearchValue(searchValue)} |
| 106 |
onClear={() => { |
| 107 |
setSearchValue(""); |
| 108 |
}} |
| 109 |
/> |
| 110 |
</MenuSearchInput> |
| 111 |
</MenuSearch> |
| 112 |
<Divider /> |
| 113 |
<DropdownList> |
| 114 |
{filteredOptions.map((option, index) => ( |
| 115 |
<DropdownItem |
| 116 |
data-cy={props.dataCy + "-dropdown-" + option} |
| 117 |
value={option} |
| 118 |
key={index} |
| 119 |
> |
| 120 |
{option} |
| 121 |
</DropdownItem> |
| 122 |
))} |
| 123 |
</DropdownList> |
| 124 |
</Dropdown> |
| 125 |
); |
| 126 |
}; |
| 127 |
|
| 128 |
export default DropdownSearch; |