forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableFavoritesDemo.tsx
More file actions
167 lines (155 loc) · 4.57 KB
/
Copy pathTableFavoritesDemo.tsx
File metadata and controls
167 lines (155 loc) · 4.57 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
import * as React from 'react';
import { sortable, ICell, IRow, ISortBy } from '@patternfly/react-table';
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated';
import { Checkbox } from '@patternfly/react-core';
interface TableState {
columns: (ICell | string)[];
rows: IRow[];
sortBy: ISortBy;
canSortFavorites: boolean;
}
export class TableFavoritesDemo extends React.Component<TableProps, TableState> {
static displayName = 'TableFavoritesDemo';
constructor(props: TableProps) {
super(props);
this.state = {
columns: [
{ title: 'Repositories', transforms: [sortable] },
'Branches',
'Pull requests',
'Workspaces',
'Last commit'
],
rows: [
{
favorited: true,
// The favorites button has defaults that can be overriden
favoritesProps: {
'aria-label': 'Favorited', // Defaults to 'Starred' / 'Not starred'
'aria-labelledby': 'favorites-button-a repository-1' // Defaults to `favorites-button-${rowIndex}`
},
cells: ['one', 'two', 'a', 'four', 'five']
},
{
cells: ['a', 'two', 'k', 'four', 'five'],
disableSelection: true
},
{
favorited: true,
cells: ['p', 'two', 'b', 'four', 'five']
}
],
sortBy: {},
canSortFavorites: true
};
this.onSelect = this.onSelect.bind(this);
this.onFavorite = this.onFavorite.bind(this);
this.onSort = this.onSort.bind(this);
this.toggleFavsSort = this.toggleFavsSort.bind(this);
}
onSelect(_event: React.FormEvent, isSelected: boolean, rowId: number) {
let rows;
if (rowId === -1) {
// header row
rows = this.state.rows.map((oneRow) => {
oneRow.selected = isSelected;
return oneRow;
});
} else {
// body row
rows = [...this.state.rows];
rows[rowId] = { ...rows[rowId], selected: isSelected };
}
this.setState({
rows
});
}
onFavorite(_event: React.MouseEvent, isFavorited: boolean, rowId: number) {
this.setState({
rows: this.state.rows.map((row, index) => {
if (index === rowId) {
row.favorited = isFavorited;
row.favoritesProps = {
...row.favoritesProps,
// Example of how to override the default aria-label of Starred / Not starred
'aria-label': isFavorited ? 'Favorited' : 'Not favorited'
};
}
return row;
})
});
}
onSort(_: React.MouseEvent, index: number, direction: 'asc' | 'desc') {
let sortedRows;
if (index === 1) {
// favorites column
sortedRows = [...this.state.rows].sort((a, b) => {
if (a.favorited && !b.favorited) {
return -1;
} else if (!a.favorited && b.favorited) {
return 1;
}
return 0;
});
} else {
const userIndex = index - 2;
sortedRows = [...this.state.rows].sort((a, b) => {
const aValue = a.cells?.[userIndex];
const bValue = b.cells?.[userIndex];
if (typeof aValue !== 'number' || typeof bValue !== 'number') {
return 0;
}
if (aValue < bValue) {
return -1;
}
if (aValue > bValue) {
return 1;
}
return 0;
});
}
this.setState({
sortBy: {
index,
direction
},
rows: direction === 'asc' ? sortedRows : sortedRows.reverse()
});
}
toggleFavsSort(checked: boolean) {
this.setState({
canSortFavorites: checked
});
}
render() {
const { columns, rows, sortBy, canSortFavorites } = this.state;
return (
<div>
<Checkbox
label="Can sort favorites"
isChecked={canSortFavorites}
onChange={(_event, checked) => this.toggleFavsSort(checked)}
aria-label="toggle select all checkbox"
id="toggle-select-all"
name="toggle-select-all"
/>
<Table
// using this prop enables the favorites column
onFavorite={this.onFavorite}
// if the onSort prop is detected, favorites can be sorted
// if you want to exclude favorites from sorting you can use this prop with a value of `false`
canSortFavorites={canSortFavorites}
onSelect={this.onSelect}
onSort={this.onSort}
sortBy={sortBy}
aria-label="Favoritable Table"
cells={columns}
rows={rows}
>
<TableHeader />
<TableBody />
</Table>
</div>
);
}
}