forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.tsx
More file actions
301 lines (289 loc) · 9.89 KB
/
Copy pathTable.tsx
File metadata and controls
301 lines (289 loc) · 9.89 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
import * as React from 'react';
import { OUIAProps, getDefaultOUIAId } from '@patternfly/react-core/dist/esm/helpers/OUIA/ouia';
import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit';
import { css } from '@patternfly/react-styles';
import { Provider } from '../../../components/Table/base/provider';
import { BodyCell } from './BodyCell';
import { HeaderCell } from './HeaderCell';
import { RowWrapper, RowWrapperProps } from '../../../components';
import { BodyWrapper } from './BodyWrapper';
import { calculateColumns, RowSelectVariant } from '../../../components';
import { TableContext } from './TableContext';
import {
ISortBy,
OnCollapse,
OnExpand,
OnSelect,
OnRowEdit,
OnSort,
IActions,
IActionsResolver,
IAreActionsDisabled,
IRow,
ICell,
TableVariant,
TableGridBreakpoint,
IHeaderRow,
OnFavorite,
TreeRowWrapper,
CustomActionsToggleProps
} from '../../../components';
export interface TableProps extends OUIAProps {
/** Adds an accessible name for the Table */
'aria-label'?: string;
/** Content rendered inside the Table */
children?: React.ReactNode;
/** Additional classes added to the Table */
className?: string;
/** Style variant for the Table */
variant?: 'compact';
/**
* Render borders
* Borders can only currently be disabled if the variant is set to 'compact'
* https://github.com/patternfly/patternfly/issues/3650
*/
borders?: boolean;
/** Specifies the grid breakpoints */
gridBreakPoint?: '' | 'grid' | 'grid-md' | 'grid-lg' | 'grid-xl' | 'grid-2xl';
/** Specifies the initial sorting pattern for the table - asc/desc and the index of the column to sort by */
sortBy?: ISortBy;
/** Function triggered when an expandable content is collapsed. When this is used, one expandable toggle button will be positioned in the first cell of a non-expandable row, preceding an expandable row */
onCollapse?: OnCollapse;
/** Function triggered when a compound expandable item is clicked */
onExpand?: OnExpand;
/** Function triggered when a row's checkbox is selected. When this is used, one checkbox/radio button will be positioned in the first or second cell of a non-expandable row */
onSelect?: OnSelect;
/** Enables or disables the ability to select all - this is mutually exclusive with radio button select variant */
canSelectAll?: boolean;
/** Enables or disables the ability to expand all */
canCollapseAll?: boolean;
/** Flag indicating the select all checkbox is disabled */
isHeaderSelectDisabled?: boolean;
/** Specifies the type of the select element variant - can be one of checkbox or radio button */
selectVariant?: 'checkbox' | 'radio';
/** An optional alternative aria label for the expand collapse all table header */
collapseAllAriaLabel?: string;
/** Function triggered when a row's inline edit is activated. Adds a column for inline edit when present. */
onRowEdit?: OnRowEdit;
/** Function triggered when sort icon is clicked */
onSort?: OnSort;
/** Actions to add to the Table */
actions?: IActions;
/** Resolver for the given action */
actionResolver?: IActionsResolver;
/** Specifies if the Kebab for actions is disabled */
areActionsDisabled?: IAreActionsDisabled;
/** Component to place in the header */
header?: React.ReactNode;
/** Component used for caption*/
caption?: React.ReactNode;
/** label for row */
rowLabeledBy?: string;
/** ID for expand */
expandId?: string;
/** ID for content */
contentId?: string;
/** The desired position to show the dropdown when clicking on the actions Kebab. Can only be used together with `actions` property */
dropdownPosition?: 'right' | 'left';
/** The desired direction to show the dropdown when clicking on the actions Kebab. Can only be used together with `actions` property */
dropdownDirection?: 'up' | 'down';
/** The container to append the dropdown menu to. Defaults to 'inline'.
* If your menu is being cut off you can append it to an element higher up the DOM tree.
* Some examples:
* actionsMenuAppendTo="parent"
* actionsMenuAppendTo={() => document.body}
* actionsMenuAppendTo={document.getElementById('target')}
*/
actionsMenuAppendTo?: HTMLElement | (() => HTMLElement) | 'inline' | 'parent';
/** The toggle of the actions menu dropdown. A KebabToggle or DropdownToggle component */
actionsToggle?: (props: CustomActionsToggleProps) => React.ReactNode;
/** Row data */
rows: (IRow | string[])[];
/** Cell/column data */
cells: (ICell | string)[];
/** Wrapper for the body */
bodyWrapper?: Function;
/** Wrapper for the row */
rowWrapper?: (props: RowWrapperProps) => JSX.Element;
/** A valid WAI-ARIA role to be applied to the table element */
role?: string;
/** If set to true, the table header sticks to the top of its container */
isStickyHeader?: boolean;
/**
* Enables favorites column
* Callback triggered when a row is favorited/unfavorited
*/
onFavorite?: OnFavorite;
/** Along with the onSort prop, enables favorites sorting, defaults to true */
canSortFavorites?: boolean;
/** Flag indicating table is a tree table */
isTreeTable?: boolean;
/** Flag indicating this table is nested within another table */
isNested?: boolean;
/** Flag indicating this table is striped */
isStriped?: boolean;
/** Flag indicating this table contains expandable rows to maintain proper striping */
isExpandable?: boolean;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
}
class Table extends React.Component<TableProps, {}> {
static displayName = 'Table';
static hasWarnBeta = false;
static defaultProps: Partial<TableProps> = {
children: null as React.ReactNode,
className: '',
variant: null as TableVariant,
borders: true,
rowLabeledBy: 'simple-node',
expandId: 'expandable-toggle',
contentId: 'expanded-content',
dropdownPosition: 'right',
dropdownDirection: 'down',
actionsMenuAppendTo: 'inline',
header: undefined as React.ReactNode,
caption: undefined as React.ReactNode,
'aria-label': undefined as string,
gridBreakPoint: TableGridBreakpoint.gridMd,
role: 'grid',
canSelectAll: true,
canCollapseAll: false,
isHeaderSelectDisabled: false,
selectVariant: 'checkbox',
collapseAllAriaLabel: '',
ouiaSafe: true,
isStickyHeader: false,
canSortFavorites: true,
isTreeTable: false,
isNested: false
};
state = {
ouiaStateId: getDefaultOUIAId(Table.displayName)
};
isSelected = (row: IRow) => row.selected === true;
areAllRowsSelected = (rows: IRow[]) => {
if (rows === undefined || rows.length === 0) {
return false;
}
return rows.every(
(row) => this.isSelected(row) || row.disableSelection || (row.hasOwnProperty('parent') && !row.showSelect)
);
};
areAllExpanded = (rows: IRow[]) => {
if (rows === undefined || rows.length === 0) {
return false;
}
return rows.every((row) => row.isOpen === undefined || row.isOpen);
};
render() {
const {
'aria-label': ariaLabel,
caption,
header,
onSort,
onSelect,
canSelectAll,
canCollapseAll,
isHeaderSelectDisabled,
selectVariant,
collapseAllAriaLabel,
sortBy,
children,
actions,
actionResolver,
areActionsDisabled,
onCollapse,
onExpand,
onRowEdit,
rowLabeledBy,
dropdownPosition,
dropdownDirection,
actionsMenuAppendTo: menuAppendTo,
actionsToggle,
contentId,
expandId,
variant,
rows,
cells,
bodyWrapper,
rowWrapper,
role,
borders,
onFavorite,
canSortFavorites,
...props
} = this.props;
if (!ariaLabel && !caption && !header && role !== 'presentation') {
// eslint-disable-next-line no-console
console.error('Table: Specify at least one of: header, caption, aria-label');
}
const headerData = calculateColumns(cells, {
sortBy,
onSort,
onSelect,
canSelectAll: selectVariant === RowSelectVariant.radio ? false : canSelectAll,
canCollapseAll,
isHeaderSelectDisabled,
selectVariant,
collapseAllAriaLabel,
allRowsSelected: onSelect ? this.areAllRowsSelected(rows as IRow[]) : false,
allRowsExpanded: onCollapse ? this.areAllExpanded(rows as IRow[]) : false,
actions,
actionResolver,
areActionsDisabled,
onCollapse,
onRowEdit,
onExpand,
rowLabeledBy,
expandId,
contentId,
dropdownPosition,
dropdownDirection,
menuAppendTo,
actionsToggle,
onFavorite,
canSortFavorites,
// order of columns: Collapsible | Selectable | Favoritable
firstUserColumnIndex: [onCollapse, onSelect, onFavorite].filter((callback) => callback).length
});
const table = (
<TableContext.Provider
value={{
headerData,
headerRows: null as IHeaderRow[],
rows
}}
>
{header}
<Provider
{...props}
aria-label={ariaLabel}
renderers={{
body: {
wrapper: bodyWrapper || BodyWrapper,
row: rowWrapper || (this.props.isTreeTable ? TreeRowWrapper : RowWrapper),
cell: BodyCell
},
header: {
cell: HeaderCell
}
}}
columns={headerData}
role={role}
variant={variant}
borders={borders}
>
{caption && <caption>{caption}</caption>}
{children}
</Provider>
</TableContext.Provider>
);
if (onRowEdit) {
return <form className={css(inlineStyles.inlineEdit)}>{table}</form>;
}
return table;
}
}
export { Table };