forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableSortableResponsive.tsx
More file actions
332 lines (317 loc) · 12.4 KB
/
TableSortableResponsive.tsx
File metadata and controls
332 lines (317 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { Fragment, useEffect, useState } from 'react';
import {
Button,
Card,
Content,
Dropdown,
DropdownList,
Flex,
FlexItem,
MenuToggle,
MenuToggleElement,
PageSection,
Pagination,
SelectOption,
SelectList,
SelectGroup,
Toolbar,
ToolbarContent,
ToolbarGroup,
ToolbarItem,
OverflowMenuDropdownItem,
PaginationVariant,
Label,
Select,
OverflowMenu,
OverflowMenuContent,
OverflowMenuControl,
OverflowMenuGroup,
OverflowMenuItem,
PageSectionVariants
} from '@patternfly/react-core';
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table';
import CloneIcon from '@patternfly/react-icons/dist/esm/icons/clone-icon';
import EditIcon from '@patternfly/react-icons/dist/esm/icons/edit-icon';
import SyncIcon from '@patternfly/react-icons/dist/esm/icons/sync-icon';
import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';
import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon';
import SortAmountDownIcon from '@patternfly/react-icons/dist/esm/icons/sort-amount-down-icon';
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper';
import EllipsisVIcon from '@patternfly/react-icons/dist/esm/icons/ellipsis-v-icon';
import { rows, columns, SampleDataRow } from '@patternfly/react-table/dist/esm/demos/sampleData';
type Direction = 'asc' | 'desc' | undefined;
export const TableSortableResponsive: React.FunctionComponent = () => {
const [isKebabDropdownOpen, setIsKebabDropdownOpen] = useState(false);
const sortRows = (rows: SampleDataRow[], sortIndex: number, sortDirection: Direction) =>
[...rows].sort((a, b) => {
let returnValue = 0;
if (typeof Object.values(a)[sortIndex] === 'number') {
// numeric sort
returnValue = Object.values(a)[sortIndex] - Object.values(b)[sortIndex];
} else {
// string sort using natural sort
returnValue = Object.values(a)[sortIndex].localeCompare(Object.values(b)[sortIndex], undefined, {
numeric: true
});
}
if (sortDirection === 'desc') {
return returnValue * -1;
}
return returnValue;
});
const [sortedData, setSortedData] = useState([...sortRows(rows, 0, 'asc')]);
const [sortedRows, setSortedRows] = useState([...sortedData]);
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(10);
// index of the currently active column
const [activeSortIndex, setActiveSortIndex] = useState(0);
// sort direction of the currently active column
const [activeSortDirection, setActiveSortDirection] = useState<Direction>('asc');
// sort dropdown expansion
const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false);
const onSort = (_event: any, index: number, direction: Direction) => {
setActiveSortIndex(index);
setActiveSortDirection(direction);
setSortedData(sortRows(rows, index, direction));
};
const kebabDropdownItems = [<OverflowMenuDropdownItem key="kebab-1">Some action</OverflowMenuDropdownItem>];
useEffect(() => {
setSortedRows(sortedData.slice((page - 1) * perPage, page * perPage));
}, [sortedData, page, perPage]);
const handleSetPage = (_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPage: number) => {
setPage(newPage);
};
const handlePerPageSelect = (_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPerPage: number) => {
setPerPage(newPerPage);
};
const renderPagination = (variant: 'top' | 'bottom' | PaginationVariant, isCompact: boolean) => (
<Pagination
isCompact={isCompact}
itemCount={rows.length}
page={page}
perPage={perPage}
onSetPage={handleSetPage}
onPerPageSelect={handlePerPageSelect}
perPageOptions={[
{ title: '10', value: 10 },
{ title: '20', value: 20 },
{ title: '50', value: 50 },
{ title: '100', value: 100 }
]}
variant={variant}
titles={{
paginationAriaLabel: `${variant} pagination`
}}
/>
);
const renderLabel = (labelText: string) => {
switch (labelText) {
case 'Running':
return <Label color="green">{labelText}</Label>;
case 'Stopped':
return <Label color="orange">{labelText}</Label>;
case 'Needs Maintenance':
return <Label color="blue">{labelText}</Label>;
case 'Down':
return <Label color="red">{labelText}</Label>;
}
};
const tableToolbar = (
<Toolbar id="sortable-toolbar">
<ToolbarContent>
<ToolbarItem visibility={{ md: 'hidden' }}>
<Select
isOpen={isSortDropdownOpen}
selected={[activeSortDirection, activeSortIndex]}
onOpenChange={(isOpen: boolean) => setIsSortDropdownOpen(isOpen)}
onSelect={(event: React.MouseEvent<Element, MouseEvent>, value: string | number) => {
if (value === 'asc' || value === 'desc') {
onSort(event, activeSortIndex, value);
} else {
onSort(event, value as number, activeSortDirection ?? 'asc');
}
}}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle
ref={toggleRef}
onClick={() => setIsSortDropdownOpen(!isSortDropdownOpen)}
isExpanded={isSortDropdownOpen}
variant="plain"
icon={<SortAmountDownIcon />}
/>
)}
>
<SelectGroup label="Sort column">
<SelectList>
{columns.map((column, columnIndex) => (
<SelectOption key={column} value={columnIndex} isSelected={activeSortIndex === columnIndex}>
{column}
</SelectOption>
))}
</SelectList>
</SelectGroup>
<SelectGroup label="Sort direction">
<SelectList>
<SelectOption isSelected={activeSortDirection === 'asc'} value="asc" key="ascending">
Ascending
</SelectOption>
<SelectOption isSelected={activeSortDirection === 'desc'} value="desc" key="descending">
Descending
</SelectOption>
</SelectList>
</SelectGroup>
</Select>
</ToolbarItem>
<ToolbarItem>
<OverflowMenu breakpoint="lg">
<OverflowMenuContent isPersistent>
<OverflowMenuGroup isPersistent groupType="button">
<OverflowMenuItem>
<Button variant="primary">Create instance</Button>
</OverflowMenuItem>
<OverflowMenuItem>
<Button variant="secondary">Action</Button>
</OverflowMenuItem>
</OverflowMenuGroup>
</OverflowMenuContent>
<OverflowMenuControl hasAdditionalOptions>
<Dropdown
onSelect={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)}
onOpenChange={(isKebabDropdownOpen: boolean) => setIsKebabDropdownOpen(isKebabDropdownOpen)}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle
ref={toggleRef}
aria-label="overflow menu"
variant="plain"
onClick={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)}
isExpanded={false}
icon={<EllipsisVIcon />}
/>
)}
isOpen={isKebabDropdownOpen}
>
<DropdownList>{kebabDropdownItems}</DropdownList>
</Dropdown>
</OverflowMenuControl>
</OverflowMenu>
</ToolbarItem>
<ToolbarGroup variant="action-group-plain">
<ToolbarItem>
<Button aria-label="Edit" variant="plain" icon={<EditIcon />} />
</ToolbarItem>
<ToolbarItem>
<Button aria-label="Clone" variant="plain" icon={<CloneIcon />} />
</ToolbarItem>
<ToolbarItem>
<Button aria-label="Sync" variant="plain" icon={<SyncIcon />} />
</ToolbarItem>
</ToolbarGroup>
<ToolbarItem variant="pagination">{renderPagination('top', true)}</ToolbarItem>
</ToolbarContent>
</Toolbar>
);
return (
<Fragment>
<DashboardWrapper>
<PageSection isWidthLimited variant={PageSectionVariants.light} aria-labelledby="table-title">
<Content>
<h1 id="table-title">Table demos</h1>
<p>
Below is an example of a responsive sortable table. When the screen size shrinks the table into a compact
form, the toolbar will display a dropdown menu containing sorting options.
</p>
</Content>
</PageSection>
<PageSection
padding={{
default: 'noPadding',
xl: 'padding'
}}
aria-label="Sortable table data"
>
<Card component="div">
{tableToolbar}
<Table aria-label="Sortable Table">
<Thead>
<Tr>
{columns.map((column, columnIndex) => {
const sortParams = {
sort: {
sortBy: {
index: activeSortIndex,
direction: activeSortDirection
},
onSort,
columnIndex
}
};
return (
<Th modifier={columnIndex !== 6 ? 'wrap' : undefined} key={columnIndex} {...sortParams}>
{column}
</Th>
);
})}
</Tr>
</Thead>
<Tbody>
{sortedRows.map((row, rowIndex) => (
<Tr key={rowIndex}>
<>
<Td dataLabel={columns[0]} width={15}>
<div>{row.name}</div>
</Td>
<Td dataLabel={columns[1]} width={10}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>
<CodeBranchIcon key="icon" />
</FlexItem>
<FlexItem>{row.threads}</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[2]} width={10}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>
<CodeIcon key="icon" />
</FlexItem>
<FlexItem>{row.applications}</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[3]} width={10}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>
<CubeIcon key="icon" />
</FlexItem>
<FlexItem>{row.workspaces}</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[4]} width={15}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>{renderLabel(row.status)}</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[5]} width={10}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>{row.location}</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[6]} width={10}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>{row.lastModified[0]} days ago</FlexItem>
</Flex>
</Td>
<Td dataLabel={columns[7]} modifier="truncate">
<a href="#">{row.url}</a>
</Td>
</>
</Tr>
))}
</Tbody>
</Table>
{renderPagination('bottom', false)}
</Card>
</PageSection>
</DashboardWrapper>
</Fragment>
);
};