Debian Codegraph @

node/@patternfly/react-core/src/demos/CustomMenus/examples/ContextSelectorDemo.tsx

raw
1 import { useRef, useState } from 'react';
2 import {
3 MenuToggle,
4 MenuFooter,
5 MenuSearch,
6 MenuSearchInput,
7 Divider,
8 InputGroup,
9 InputGroupItem,
10 Button,
11 ButtonVariant,
12 SearchInput,
13 Dropdown,
14 DropdownList,
15 DropdownItem
16 } from '@patternfly/react-core';
17 import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
18
19 interface ItemData {
20 text: string;
21 href?: string;
22 isDisabled?: boolean | undefined;
23 }
24
25 type ItemArrayType = (ItemData | string)[];
26
27 export const ContextSelectorDemo: React.FunctionComponent = () => {
28 const items: ItemArrayType = [
29 {
30 text: 'Action'
31 },
32 {
33 text: 'Link',
34 href: '#'
35 },
36 {
37 text: 'Disabled action',
38 isDisabled: true
39 },
40 {
41 text: 'Disabled link',
42 href: '#',
43 isDisabled: true
44 },
45 'My project',
46 'OpenShift cluster',
47 'Production Ansible',
48 'AWS',
49 'Azure',
50 'My project 2',
51 'OpenShift cluster ',
52 'Production Ansible 2 ',
53 'AWS 2',
54 'Azure 2'
55 ];
56 const [isOpen, setIsOpen] = useState<boolean>(false);
57 const [selected, setSelected] = useState(typeof items[0] === 'string' ? items[0] : items[0].text);
58 const [filteredItems, setFilteredItems] = useState<ItemArrayType>(items);
59 const [searchInputValue, setSearchInputValue] = useState<string>('');
60 const menuRef = useRef<HTMLDivElement>(null);
61 const menuFooterBtnRef = useRef<HTMLButtonElement>(null);
62
63 const onToggleClick = () => {
64 setIsOpen(!isOpen);
65 };
66
67 const onSelect = (ev: React.MouseEvent<Element, MouseEvent> | undefined, itemId: string | number | undefined) => {
68 if (typeof itemId === 'number' || typeof itemId === 'undefined') {
69 return;
70 }
71 setSelected(itemId.toString());
72 setIsOpen(!isOpen);
73 };
74
75 const onSearchInputChange = (value: string) => {
76 setSearchInputValue(value);
77 };
78
79 const onSearchButtonClick = () => {
80 const filtered =
81 searchInputValue === ''
82 ? items
83 : items.filter((item) => {
84 const str = typeof item === 'string' ? item : item.text;
85 return str.toLowerCase().indexOf(searchInputValue.toLowerCase()) !== -1;
86 });
87
88 setFilteredItems(filtered || []);
89 setIsOpen(true); // Keep menu open after search executed
90 };
91
92 const onEnterPressed = (event: React.KeyboardEvent) => {
93 if (event.key === 'Enter') {
94 onSearchButtonClick();
95 }
96 };
97
98 return (
99 <Dropdown
100 isOpen={isOpen}
101 onOpenChange={(isOpen) => setIsOpen(isOpen)}
102 onOpenChangeKeys={['Escape']}
103 toggle={(toggleRef) => (
104 <MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
105 {selected}
106 </MenuToggle>
107 )}
108 ref={menuRef}
109 id="context-selector"
110 onSelect={onSelect}
111 isScrollable
112 >
113 <MenuSearch>
114 <MenuSearchInput>
115 <InputGroup>
116 <InputGroupItem isFill>
117 <SearchInput
118 value={searchInputValue}
119 placeholder="Search"
120 onChange={(_event, value) => onSearchInputChange(value)}
121 onKeyPress={onEnterPressed}
122 aria-labelledby="pf-v6-context-selector-search-button-id-1"
123 />
124 </InputGroupItem>
125 <InputGroupItem>
126 <Button
127 variant={ButtonVariant.control}
128 aria-label="Search menu items"
129 id="pf-v6-context-selector-search-button-id-1"
130 onClick={onSearchButtonClick}
131 icon={<SearchIcon />}
132 />
133 </InputGroupItem>
134 </InputGroup>
135 </MenuSearchInput>
136 </MenuSearch>
137 <Divider />
138 <DropdownList>
139 {filteredItems.map((item, index) => {
140 const [itemText, isDisabled, href] =
141 typeof item === 'string' ? [item, null, null] : [item.text, item.isDisabled || null, item.href || null];
142 return (
143 <DropdownItem
144 itemId={itemText}
145 key={index}
146 isDisabled={isDisabled as boolean | undefined}
147 to={href as string | undefined}
148 >
149 {itemText}
150 </DropdownItem>
151 );
152 })}
153 </DropdownList>
154 <MenuFooter>
155 <Button ref={menuFooterBtnRef} variant="link" isInline>
156 Action
157 </Button>
158 </MenuFooter>
159 </Dropdown>
160 );
161 };