Debian Codegraph @

node/@patternfly/react-core/src/components/TreeView/examples/TreeViewWithSearch.tsx

raw
1 import { useState } from 'react';
2 import {
3 Toolbar,
4 ToolbarContent,
5 ToolbarItem,
6 TreeView,
7 TreeViewDataItem,
8 TreeViewSearch
9 } from '@patternfly/react-core';
10 export const TreeViewWithSearch: React.FunctionComponent = () => {
11 const options = [
12 {
13 name: 'Application launcher',
14 id: 'example2-AppLaunch',
15 children: [
16 {
17 name: 'Application 1',
18 id: 'example2-App1',
19 children: [
20 { name: 'Settings', id: 'example2-App1Settings' },
21 { name: 'Current', id: 'example2-App1Current' }
22 ]
23 },
24 {
25 name: 'Application 2',
26 id: 'example2-App2',
27 children: [
28 { name: 'Settings', id: 'example2-App2Settings' },
29 {
30 name: 'Loader',
31 id: 'example2-App2Loader',
32 children: [
33 { name: 'Loading App 1', id: 'example2-LoadApp1' },
34 { name: 'Loading App 2', id: 'example2-LoadApp2' },
35 { name: 'Loading App 3', id: 'example2-LoadApp3' }
36 ]
37 }
38 ]
39 }
40 ],
41 defaultExpanded: true
42 },
43 {
44 name: 'Cost management',
45 id: 'example2-Cost',
46 children: [
47 {
48 name: 'Application 3',
49 id: 'example2-App3',
50 children: [
51 { name: 'Settings', id: 'example2-App3Settings' },
52 { name: 'Current', id: 'example2-App3Current' }
53 ]
54 }
55 ]
56 },
57 {
58 name: 'Sources',
59 id: 'example2-Sources',
60 children: [
61 {
62 name: 'Application 4',
63 id: 'example2-App4',
64 children: [{ name: 'Settingexample2-s', id: 'example2-App4Settings' }]
65 }
66 ]
67 },
68 {
69 name: 'Really really really long folder name that overflows the container it is in',
70 id: 'example2-Long',
71 children: [{ name: 'Application 5', id: 'example2-App5' }]
72 }
73 ];
74
75 const [activeItems, setActiveItems] = useState<TreeViewDataItem[]>();
76 const [filteredItems, setFilteredItems] = useState(options);
77 const [isFiltered, setIsFiltered] = useState(false);
78
79 const onSelect = (_event: React.MouseEvent, treeViewItem: TreeViewDataItem) => {
80 // Ignore folders for selection
81 if (treeViewItem && !treeViewItem.children) {
82 setActiveItems([treeViewItem]);
83 }
84 };
85
86 const onSearch = (event: React.ChangeEvent<HTMLInputElement>) => {
87 const input = event.target.value;
88 if (input === '') {
89 setFilteredItems(options);
90 setIsFiltered(false);
91 } else {
92 const filtered = options.map((opt) => Object.assign({}, opt)).filter((item) => filterItems(item, input));
93 setFilteredItems(filtered);
94 setIsFiltered(true);
95 }
96 };
97 const filterItems = (item, input) => {
98 if (item.name.toLowerCase().includes(input.toLowerCase())) {
99 return true;
100 }
101 if (item.children) {
102 return (
103 (item.children = item.children
104 .map((opt) => Object.assign({}, opt))
105 .filter((child) => filterItems(child, input))).length > 0
106 );
107 }
108 };
109 const toolbar = (
110 <Toolbar style={{ padding: 0 }}>
111 <ToolbarContent style={{ padding: 0 }}>
112 <ToolbarItem>
113 <TreeViewSearch onSearch={onSearch} id="input-search" name="search-input" aria-label="Search input example" />
114 </ToolbarItem>
115 </ToolbarContent>
116 </Toolbar>
117 );
118
119 return (
120 <TreeView
121 hasAnimations
122 aria-label="Tree View with search example"
123 data={filteredItems}
124 activeItems={activeItems}
125 onSelect={onSelect}
126 allExpanded={isFiltered}
127 toolbar={toolbar}
128 />
129 );
130 };