Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/Table.md

raw
1 ---
2 id: Table
3 cssPrefix: pf-v6-c-table
4 section: components
5 propComponents:
6 [
7 'Table',
8 'Thead',
9 'Tbody',
10 'Tr',
11 'Th',
12 'Td',
13 'Caption',
14 'TableText',
15 'TdActionsType',
16 'TdSelectType',
17 'ThSelectType',
18 'TdTreeRowType',
19 'ActionsColumn',
20 'TdCompoundExpandType',
21 'TdFavoritesType',
22 'TdDraggableType',
23 'ThInfoType',
24 'TdExpandType',
25 'ThExpandType',
26 'EditableSelectInputCell',
27 'EditableTextCell',
28 'EditableSelectInputProps',
29 'EditableTextCellProps',
30 'ThSortType',
31 'ISortBy',
32 'InnerScrollContainer',
33 'OuterScrollContainer'
34 ]
35 ouia: true
36 ---
37
38 Note: Table lives in its own package at [@patternfly/react-table](https://www.npmjs.com/package/@patternfly/react-table)!
39
40 The `Table` component takes an explicit and declarative approach, and its implementation closely mirrors that of an HTML table.
41
42 The documentation for the deprecated table implementation can be found under the [React deprecated](/components/table/react-deprecated) tab. It is configuration based and takes a less declarative and more implicit approach to laying out the table structure, such as the rows and cells within it.
43
44 import { Fragment, isValidElement, useCallback, useEffect, useRef, useState, useLayoutEffect } from 'react';
45 import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
46 import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon';
47 import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon';
48 import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
49 import LeafIcon from '@patternfly/react-icons/dist/esm/icons/leaf-icon';
50 import FolderIcon from '@patternfly/react-icons/dist/esm/icons/folder-icon';
51 import FolderOpenIcon from '@patternfly/react-icons/dist/esm/icons/folder-open-icon';
52 import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon';
53 import RhUiBlueprintIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-blueprint-icon';
54 import RhUiEditFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-edit-fill-icon';
55 import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon';
56 import CheckIcon from '@patternfly/react-icons/dist/esm/icons/check-icon';
57 import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
58
59 import { css } from '@patternfly/react-styles';
60 import styles from '@patternfly/react-styles/css/components/Table/table';
61 import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing';
62 import textStyles from '@patternfly/react-styles/css/utilities/Text/text';
63 import inlineEditStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit';
64 import t_global_background_color_secondary_default from '@patternfly/react-tokens/dist/esm/t_global_background_color_secondary_default';
65
66 ## Table examples
67
68 This `Table` component differs from the deprecated `Table` component, in that it allows you to compose the table by nesting the relevant `Thead`, `Tbody`, `Tr`, `Th` and `Td` components within it. For a less declarative and more implicit approach, use the `Table` component instead.
69
70 Some general notes:
71
72 - Provide `dataLabel` prop to the `Td` components so that in mobile view the cell has a label to provide context.
73 - If you want a table caption, simply place a `<Caption>My caption</Caption>` as the first child within a `Table`.
74 - You can set the `Table` variant to `compact`
75
76 ### Basic
77
78 ```ts file="TableBasic.tsx"
79
80 ```
81
82 ### Plain
83
84 ```ts file="TablePlain.tsx"
85
86 ```
87
88 ### Custom row wrapper, header tooltips & popovers
89
90 - If you add the `noWrap` prop to `Thead`, it won't wrap it if there is no space
91 - You can add the `textCenter` prop to `Th` or `Td` to center the contents
92 - You can pass `className`, `style` and more to `Tr`
93
94 To add a header tooltip or popover to `Th`, pass a `ThInfoType` object via the `info` prop.
95
96 ```ts file="TableMisc.tsx"
97
98 ```
99
100 ### Sortable & wrapping headers
101
102 To make a column sortable, pass a `ThSortType` object via the `sort` prop on a column's `Th`.
103
104 `ThSortType` includes an `OnSort` event handler which has the following signature:
105
106 ```
107 type OnSort = (
108 event: React.MouseEvent,
109 columnIndex: number,
110 sortByDirection: SortByDirection,
111 extraData: IExtraColumnData
112 ) => void;
113
114 ```
115
116 The built in display for sorting is not fully responsive, as the column headers will be displayed per row when the screen size is small. To see a full page demo of a responsive sortable table, utilizing a toolbar item to control sorting for small screens, view the `Sortable - responsive` demo in the `React demos` tab.
117
118 ```ts file="TableSortable.tsx"
119
120 ```
121
122 ### Sortable - custom control
123
124 Sorting a table may also be controlled manually with a toolbar control. To see a full page demo of a responsive table, view the `Sortable - responsive` demo in the `React demos` tab.
125
126 ```ts file="TableSortableCustom.tsx"
127
128 ```
129
130 ### Selectable with checkbox
131
132 To make a row selectable, the table needs a selection column.
133 The selection column is just another column, but with selection specific props added.
134 We add it as the first header cell and also as the first body cell for each row.
135
136 To make a column sortable, pass a `ThSelectType` object via the `select` prop on a column's `Th`.
137
138 To make a row sortable, pass a `TdSelectType` object via the `select` prop on each rows's first `Td`.
139
140 Both the `TdSelectType` and the `ThSelectType` expect an `OnSelect` event handler with the following signature:
141
142 `OnSelect:`
143
144 ```
145 type OnSelect = (
146 event: React.FormEvent<HTMLInputElement>,
147 isSelected: boolean,
148 rowIndex: number,
149 rowData: IRowData,
150 extraData: IExtraData
151 ) => void;
152 ```
153
154 **Note:** This example has a `shift + select` feature where holding shift while
155 checking checkboxes will check intermediate rows' checkboxes.
156
157 ```ts file="TableSelectable.tsx"
158
159 ```
160
161 ### Selectable with indeterminate state
162
163 To indicate partial selection, use `isIndeterminate` on the header's `select` prop. When some (but not all) selectable rows are selected, the header checkbox will convey this information to assistive technologies and also display a dash instead of a checkmark.
164
165 ```ts file="TableSelectableIndeterminate.tsx"
166
167 ```
168
169 ### Selectable radio input
170
171 Similarly to the selectable example above, the radio buttons use the first column. The first header cell is empty, and each body row's first cell has radio button props.
172
173 ```ts file="TableSelectableRadio.tsx"
174
175 ```
176
177 ### Row click handler, clickable rows
178
179 This selectable rows feature is intended for use when a table is used to present a list of objects in a Primary-detail view.
180
181 ```ts file="TableClickable.tsx"
182
183 ```
184
185 ### Editable rows
186
187 This example shows a table with editable rows. Cells in a row can be edited after clicking on the edit icon.
188
189 ```ts file="TableEditable.tsx"
190
191 ```
192
193 ### Actions
194
195 This example demonstrates adding actions as the last column. The header's last cell is an empty cell, and each body row's last cell is an action cell.
196
197 To make a cell an action cell, render an `ActionsColumn` component inside a row's last `Td` and pass an array of `IAction` objects via the `items` prop of `ActionsColumn`.
198
199 If actions menus are getting clipped by other items on the page, such as sticky columns or rows, the `ActionsColumn` can be passed a `menuAppendTo` prop to adjust where the actions menu is appended.
200
201 When a table row contains mixed content of text and interactive elements, the `hasAction` property may be passed to a `Td` which contains interactive content like the below example's start `Button`. This will align buttons and other elements with other cells' text. Note that `hasAction` should not be used with `Td`s in an `ActionsColumn` because that comes with it's own spacing.
202
203 ```ts file="TableActions.tsx"
204
205 ```
206
207 ### Actions Overflow
208
209 Using an `OverflowMenu` in the actions column, allowing the actions to condense into a dropdown if necessary for space.
210
211 ```ts file="TableActionsOverflow.tsx"
212
213 ```
214
215 ### Expandable
216
217 To make a parent/child row pair expandable:
218
219 1. Pass `isExpandable` to `Table`.
220 1. Make the first cell in every row an expandable cell by passing `TdExpandType` object to the `expand` prop on the `Td`.
221 1. Wrap the content of each child row cell in `ExpandableRowContent`.
222 1. Pass `isExpanded` to `Tbody` and the `Tr` containing expandable content, and pass `isContentExpanded` to the `Tr` that acts as the "control row".
223
224 The `TdExpandType` expects an `OnCollapse` event handler that has the following signature:
225
226 ```
227 type OnCollapse = (
228 event: React.MouseEvent,
229 rowIndex: number,
230 isOpen: boolean,
231 rowData: IRowData,
232 extraData: IExtraData
233 ) => void;
234 ```
235
236 Note: Table column widths will respond automatically when toggling expanded rows. To retain column widths between expanded and collapsed states, column header and/or data cell widths must be set.
237
238 ```ts file="TableExpandable.tsx"
239
240 ```
241
242 ### Compound expandable
243
244 To make a parent/child row pair compound expandable:
245
246 1. Pass `isExpandable` to `Table`.
247 1. Pass a `TdCompoundExpandType` object to the `compoundExpand` prop on any `Td` that has an expandable child row.
248 1. Wrap the content of each child row cell in `ExpandableRowContent`.
249 1. Pass `isExpanded` to `Tbody` and the `Tr` containing expandable content, and pass `isContentExpanded` to the `Tr` that acts as the "control row".
250
251 The `TdCompoundExpandType` expects an `OnExpand` event handler with the following signature
252
253 ```
254 export type OnExpand = (
255 event: React.MouseEvent,
256 rowIndex: number,
257 colIndex: number,
258 isOpen: boolean,
259 rowData: IRowData,
260 extraData: IExtraData
261 ) => void;
262 ```
263
264 For a more complex compound expandable implementation with nested tables, see our [table compound expansion demo](/components/table/react-demos/compound-expansion).
265
266 ```ts file="TableCompoundExpandable.tsx"
267
268 ```
269
270 ### Cell width, breakpoint modifiers
271
272 ```ts file="TableCellWidth.tsx"
273
274 ```
275
276 ### Controlling text
277
278 ```ts file="TableControllingText.tsx"
279
280 ```
281
282 ### Modifiers with table text
283
284 If the "wrapModifier" property is set to "truncate", it's needed to ensure that the corresponding tooltip can be opened using both keyboard and screen reader. Since this particular Td element is generic and doesn't have any predefined decorators, the focus management required to trigger the tooltip needs to be handled manually by defining and manipulating the requisite props.
285
286 ```ts file="TableTextModifiers.tsx"
287
288 ```
289
290 ### Empty state
291
292 ```ts file="TableEmptyState.tsx"
293
294 ```
295
296 ### Favoritable (implemented with sortable)
297
298 To make a row favoritable, the table needs a favoritable column.
299 Pass a `TdFavoritesType` object via the `favorites` prop on each rows's first `Td` in the favoritable column.
300
301 The `TdFavoritesType` expects an `OnFavorite` event handler with the following signature:
302
303 ```
304 type OnFavorite = (
305 event: React.MouseEvent,
306 isFavorited: boolean,
307 rowIndex: number,
308 rowData: IRowData,
309 extraData: IExtraData
310 ) => void;
311 ```
312
313 To make a favoritable column sortable, pass a `ThSortType` object to the favoritable column's `Th` with `isFavorites` set to true.
314
315 ```ts file="TableFavoritable.tsx"
316
317 ```
318
319 ### Tree table
320
321 To enable a tree table:
322
323 1. Pass the `isTreeTable` prop to the `Table` component
324 2. Use a `TreeRowWrapper` rather than `Tr`
325 3. Pass the following `props` to each row (both the `TreeRowWrapper` and the `treeRow` in the first column):
326 - `isExpanded` - Flag indicating the node is expanded and its children are visible
327 - `isDetailsExpanded` - (optional) Flag indicating the row's details are visible in responsive view
328 - `isHidden` - Flag indicating the node's parent is expanded and this node is visible
329 - `aria-level` - number representing how many levels deep this node is nested
330 - `aria-posinset` - number representing where in the order this node sits amongst its siblings
331 - `aria-setsize` - number representing the number of children this node has
332 - `isChecked` - (optional) if this row uses checkboxes, flag indicating the checkbox checked
333 - `icon` - (optional) ReactNode icon to display before the row title
334 - `toggleAriaLabel` - (optional) accessible label for the expand/collapse children rows toggle arrow
335 - `checkAriaLabel` - (optional) accessible label for the checkbox
336 - `showDetailsAriaLabel` - (optional) accessible label for the show row details button in the responsive view
337 4. The first `Td` in each row will pass the following to the `treeRow` prop:
338 - `onCollapse` - Callback when user expands/collapses a row to reveal/hide the row's children.
339 - `onCheckChange` - (optional) Callback when user changes the checkbox on a row.
340 - `onToggleRowDetails` - (optional) Callback when user shows/hides the row details in responsive view.
341 - `props` - (as defined above)
342 - `rowIndex` - number representing the index of the row
343
344 Note: If this table is going to be tested using axe-core, the tests will flag the use of aria-level,
345 aria-posinset, and aria-setsize as violations. This is an intentional choice at this time so that
346 the voice over technologies will recognize the flat table structure as a tree.
347
348 ```ts file="TableTree.tsx"
349
350 ```
351
352 ### Flat tree table with no inset
353
354 To remove the inset used to leave space for the expand/collapse toggle in a flat tree table, use the `hasNoInset` prop on the `Table` component.
355
356 ```ts file="TableTreeNoInset.tsx"
357
358 ```
359
360 ### Draggable row table
361
362 To make a row draggable:
363
364 1. The table needs a draggable column.
365 2. Each draggable `Tr` needs to be passed `draggable`, `onDrop`, `onDragEnd`, and `onDragStart` props.
366 3. The `Tbody` needs `onDragOver`, `onDrop`, and `onDragLeave` props.
367 4. While the user is dragging a row, the `pf-m-drag-over` class needs to be applied to `Table`.
368 5. The draggable `Td` in each row needs a `TdDraggableType` object passed to its `draggable` prop.
369
370 ```ts isBeta file="TableDraggable.tsx"
371
372 ```
373
374 ### Sticky table modifiers
375
376 To make certain columns and the header sticky, the table must be wrapped in a combination of `OuterScrollContainer` and `InnerScrollContainer`. For sticky columns, only `InnerScrollContainer` is required. For sticky headers, and sticky headers with sticky columns, both containers are required to ensure the sticky behavior behaves correctly.
377
378 **Note:** Sticky table headers and columns have a higher `z-index` than the `z-index` used for menus (dropdown, select, etc). The intent is that the contents of a scrollable table will scroll under the sticky header/column, including any expanded menus. However, there may be use cases where a menu needs to appear on top of a sticky header/column, such as an expanded menu in a toolbar above a table with a sticky header.
379
380 There are a few ways this can be handled:
381
382 - Manipulate the `z-index` of the menu and/or table headers/columns manually.
383 - Use the `menuAppendTo` prop in non-composable react components with menus to append the menu to an element outside of the table (e.g., the table's parent element) so that the menu has a higher stacking context than - and can appear on top of - sticky headers/columns as well as appear outside of any scrollable content in the table.
384 - In the case where the menu is outside of the table (e.g., above the table in a toolbar, or below the table and the menu expands up), assign the entire table a lower `z-index` than the `z-index` of the menu. This creates a lower stacking context for the entire table compared to the menu, while preserving the stacking context of the elements inside of the table.
385
386 ### Sticky column
387
388 To make a column sticky, wrap `Table` with `InnerScrollContainer` and add the following properties to the `Th` or `Td` that should be sticky:
389
390 - `isStickyColumn`
391 - `hasRightBorder` for a left-aligned sticky column, or `hasLeftBorder` for a right-aligned sticky column.
392
393 To prevent the default text wrapping behavior and allow horizontal scrolling, all `Th` or `Td` cells should also have the `modifier="nowrap"` property. To set the minimum width of the sticky column, use the `stickyMinWidth` property. For multiple sticky columns, use the `stickyLeftOffset` and `stickyRightOffset` properties for additional left or right sticky columns.
394
395 ```ts file="TableStickyColumn.tsx"
396
397 ```
398
399 ### Multiple left-aligned sticky columns
400
401 To make multiple left-aligned columns sticky:
402
403 - wrap `Table` with `InnerScrollContainer`
404 - add `isStickyColumn` to all columns that should be sticky
405 - add `hasRightBorder` to the rightmost sticky column
406 - add `stickyLeftOffset` to each sticky column with a value that equals the combined width - set by `stickyMindWidth` - of the previous sticky columns. The leftmost sticky column should have a value of `0`, which is the default of this property.
407
408 To prevent the default text wrapping behavior and allow horizontal scrolling, all `Th` or `Td` cells should also have the `modifier="nowrap"` property.
409
410 ```ts file="TableMultipleStickyColumns.tsx"
411
412 ```
413
414 ### Multiple right-aligned sticky columns
415
416 To make multiple right-aligned columns sticky:
417
418 - wrap `Table` with `InnerScrollContainer`
419 - add `isStickyColumn` to all columns that should be sticky
420 - add `hasLeftBorder` to the leftmost sticky column
421 - add `stickyRightOffset` to each sticky column with a value that equals the combined width - set by `stickyMindWidth` - of the next sticky columns. The rightmost sticky column should have a value of `0`, which is the default of this property.
422
423 To prevent the default text wrapping behavior and allow horizontal scrolling, all `Th` or `Td` cells should also have the `modifier="nowrap"` property.
424
425 ```ts file="TableRightStickyColumn.tsx"
426
427 ```
428
429 ### Dynamic sticky header
430
431 A sticky header may alternatively be implemented with two properties: `isStickyHeaderBase` and `isStickyHeaderStuck` - which allows separate control of the sticky position and sticky styling. `isStickyHeaderBase` should always be applied to make the header position sticky, and `isStickyHeaderStuck` may be applied dynamically to enable the sticky styling, such as when the sticky header is not at the top of the scroll parent as shown in the example.
432
433 `isStickyHeader` acts as if both properties are present and true when applied, and is useful when dynamic sticky styling is not necessary.
434
435 ```ts file="TableStickyHeaderDynamic.tsx"
436
437 ```
438
439 ### Sticky columns and header
440
441 To maintain proper sticky behavior across sticky columns and header, `Table` must be wrapped with `OuterScrollContainer` and `InnerScrollContainer`.
442
443 ```ts file="TableStickyColumnsAndHeader.tsx"
444
445 ```
446
447 ### Nested column headers
448
449 To make a nested column header:
450
451 1. Wrap `Table` with `InnerScrollContainer`.
452 2. Pass `nestedHeaderColumnSpans` to `Table`. `nestedHeaderColumnSpans` is an array of numbers representing the column spans of the top level columns to `Table`, where each number is equal to the number of sub columns for a column, or `1` if a column contains no sub columns.
453 3. Pass `hasNestedHeader` to `Thead`.
454 4. Pass two `Tr` as children of `Thead`.
455 5. If the parent `Table` has a sticky header, pass an additional `Tr isBorderRow` in `Thead` to create the bottom border in the `Thead`.
456
457 The first `Tr` represents the top level of columns, and each must pass either `rowSpan` if the column does not contain sub columns or `colSpan` if the column contains sub columns. The value of `rowSpan` is equal to the number of rows the nested header will span, typically `2`, and the value of `colSpan` is equal to the number of sub columns in a column. Each `Th` except the last should also pass `hasRightBorder`.
458
459 The second `Tr` represents the second level of sub columns. The `Th` in this row each should pass `isSubHeader`, and the last sub column of a column should also pass `hasRightBorder`.
460
461 ```ts file="TableNestedHeaders.tsx"
462
463 ```
464
465 ### Nested column headers and expandable rows
466
467 ```ts file="TableNestedExpandable.tsx"
468
469 ```
470
471 ### Expandable with nested table
472
473 ```ts file="TableNestedTableExpandable.tsx"
474
475 ```
476
477 ### Nested sticky header
478
479 ```ts file="TableNestedStickyHeader.tsx"
480
481 ```
482
483 ### Striped
484
485 To apply striping to a basic table, add the `isStriped` property to `Table`.
486
487 ```ts file="TableStriped.tsx"
488
489 ```
490
491 ### Striped expandable
492
493 To apply striping to an expandable table, add the `isStriped` and `isExpandable` properties to `Table`.
494
495 ```ts file="TableStripedExpandable.tsx"
496
497 ```
498
499 ### Striped multiple tobdy
500
501 When there are multiple `Tbody` components within a table, a more granular application of striping may be controlled by adding either the `isEvenStriped` or `isOddStriped` properties to `Tbody`. These properties will stripe even or odd rows within that `Tbody` respectively.
502
503 ```ts file="TableStripedMultipleTbody.tsx"
504
505 ```
506
507 ### Striped tr
508
509 To manually control striping, add the `isStriped` property to each desired `Tr`. This replaces adding the `isStriped` property to `Table`.
510
511 ```ts file="TableStripedTr.tsx"
512
513 ```