@patternfly/react-table 4.44.11 → 4.44.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/esm/components/Table/Table.d.ts +2 -1
  3. package/dist/esm/components/Table/Table.d.ts.map +1 -1
  4. package/dist/esm/components/Table/Table.js.map +1 -1
  5. package/dist/esm/components/Table/TableTypes.d.ts +2 -1
  6. package/dist/esm/components/Table/TableTypes.d.ts.map +1 -1
  7. package/dist/esm/components/Table/TableTypes.js.map +1 -1
  8. package/dist/esm/components/Table/base/provider.d.ts +2 -2
  9. package/dist/esm/components/Table/base/types.d.ts +2 -2
  10. package/dist/esm/components/TableComposable/TableComposable.d.ts +1 -1
  11. package/dist/esm/components/TableComposable/Td.d.ts +1 -1
  12. package/dist/js/components/Table/Table.d.ts +2 -1
  13. package/dist/js/components/Table/Table.d.ts.map +1 -1
  14. package/dist/js/components/Table/Table.js.map +1 -1
  15. package/dist/js/components/Table/TableTypes.d.ts +2 -1
  16. package/dist/js/components/Table/TableTypes.d.ts.map +1 -1
  17. package/dist/js/components/Table/TableTypes.js.map +1 -1
  18. package/dist/js/components/Table/base/provider.d.ts +2 -2
  19. package/dist/js/components/Table/base/types.d.ts +2 -2
  20. package/dist/js/components/TableComposable/TableComposable.d.ts +1 -1
  21. package/dist/js/components/TableComposable/Td.d.ts +1 -1
  22. package/package.json +6 -6
  23. package/src/components/Table/Table.tsx +2 -1
  24. package/src/components/Table/TableTypes.tsx +2 -1
  25. package/src/components/Table/examples/LegacyTableActions.tsx +218 -0
  26. package/src/components/Table/examples/LegacyTableBasic.tsx +90 -0
  27. package/src/components/Table/examples/LegacyTableCellWidth.tsx +66 -0
  28. package/src/components/Table/examples/LegacyTableCompoundExpandable.tsx +175 -0
  29. package/src/components/Table/examples/LegacyTableControllingText.tsx +51 -0
  30. package/src/components/Table/examples/LegacyTableEmptyState.tsx +48 -0
  31. package/src/components/Table/examples/LegacyTableExpandable.tsx +172 -0
  32. package/src/components/Table/examples/LegacyTableFavoritable.tsx +157 -0
  33. package/src/components/Table/examples/LegacyTableHoverable.tsx +125 -0
  34. package/src/components/Table/examples/LegacyTableMisc.tsx +56 -0
  35. package/src/components/Table/examples/LegacyTableSelectable.tsx +124 -0
  36. package/src/components/Table/examples/LegacyTableSelectableRadio.tsx +54 -0
  37. package/src/components/Table/examples/LegacyTableSortable.tsx +120 -0
  38. package/src/components/Table/examples/LegacyTableSortableCustom.tsx +189 -0
  39. package/src/components/Table/examples/LegacyTableTextModifiers.tsx +31 -0
  40. package/src/components/Table/examples/LegacyTableTree.tsx +213 -0
  41. package/src/components/Table/examples/Table.md +83 -1848
  42. package/src/components/TableComposable/examples/ComposableTable.md +31 -2805
  43. package/src/components/TableComposable/examples/ComposableTableActions.tsx +163 -0
  44. package/src/components/TableComposable/examples/ComposableTableBasic.tsx +89 -0
  45. package/src/components/TableComposable/examples/ComposableTableCellWidth.tsx +56 -0
  46. package/src/components/TableComposable/examples/ComposableTableCompoundExpandable.tsx +103 -0
  47. package/src/components/TableComposable/examples/ComposableTableControllingText.tsx +51 -0
  48. package/src/components/TableComposable/examples/ComposableTableDraggable.tsx +170 -0
  49. package/src/components/TableComposable/examples/ComposableTableEmptyState.tsx +45 -0
  50. package/src/components/TableComposable/examples/ComposableTableExpandable.tsx +195 -0
  51. package/src/components/TableComposable/examples/ComposableTableFavoritable.tsx +118 -0
  52. package/src/components/TableComposable/examples/ComposableTableHoverable.tsx +67 -0
  53. package/src/components/TableComposable/examples/ComposableTableMisc.tsx +92 -0
  54. package/src/components/TableComposable/examples/ComposableTableMultipleStickyColumns.tsx +153 -0
  55. package/src/components/TableComposable/examples/ComposableTableNestedExpandable.tsx +127 -0
  56. package/src/components/TableComposable/examples/ComposableTableNestedHeaders.tsx +202 -0
  57. package/src/components/TableComposable/examples/ComposableTableSelectable.tsx +125 -0
  58. package/src/components/TableComposable/examples/ComposableTableSelectableRadio.tsx +68 -0
  59. package/src/components/TableComposable/examples/ComposableTableSortable.tsx +107 -0
  60. package/src/components/TableComposable/examples/ComposableTableSortableCustom.tsx +175 -0
  61. package/src/components/TableComposable/examples/ComposableTableStickyColumn.tsx +144 -0
  62. package/src/components/TableComposable/examples/ComposableTableStickyColumnsAndHeader.tsx +167 -0
  63. package/src/components/TableComposable/examples/ComposableTableTextModifiers.tsx +33 -0
  64. package/src/components/TableComposable/examples/ComposableTableTree.tsx +201 -0
  65. package/src/demos/Table.md +7 -6
  66. package/src/components/Table/examples/DemoSortableTable.js +0 -65
  67. package/src/components/TableComposable/examples/DemoSortableTable.js +0 -65
@@ -0,0 +1,157 @@
1
+ import React from 'react';
2
+ import { Table, TableHeader, TableBody, sortable, TableProps } from '@patternfly/react-table';
3
+ import { Checkbox } from '@patternfly/react-core';
4
+
5
+ interface Repository {
6
+ name: string;
7
+ branches: string;
8
+ prs: string;
9
+ workspaces: string;
10
+ lastCommit: string;
11
+ }
12
+
13
+ export const LegacyTableFavoritable: React.FunctionComponent = () => {
14
+ // In real usage, this data would come from some external source like an API via props.
15
+ const repositories: Repository[] = [
16
+ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
17
+ { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
18
+ { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
19
+ ];
20
+
21
+ // Index of the currently sorted column
22
+ // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key
23
+ // as the identifier of the sorted column. See the "Compound expandable" example.
24
+ const [activeSortIndex, setActiveSortIndex] = React.useState<number | null>(null);
25
+
26
+ // Sort direction of the currently sorted column
27
+ const [activeSortDirection, setActiveSortDirection] = React.useState<'asc' | 'desc' | null>(null);
28
+
29
+ // Favorite state is similar to selection state. See selectedRepoNames and associated code below.
30
+ const [favoriteRepoNames, setFavoriteRepoNames] = React.useState<string[]>([]);
31
+ const setRepoFavorited = (repo: Repository, isFavoriting = true) =>
32
+ setFavoriteRepoNames(prevFavorites => {
33
+ const otherFavorites = prevFavorites.filter(r => r !== repo.name);
34
+ return isFavoriting ? [...otherFavorites, repo.name] : otherFavorites;
35
+ });
36
+ const isRepoFavorited = (repo: Repository) => favoriteRepoNames.includes(repo.name);
37
+
38
+ // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index.
39
+ // Note: We also memoize the sortable values with useCallback to prevent rows jumping around when you change
40
+ // the favorites while sorting on that column. Only updating the sort state will reorder the rows.
41
+ const getSortableRowValues = React.useCallback(
42
+ (repo: Repository): (boolean | string | number)[] => {
43
+ const { name, branches, prs, workspaces, lastCommit } = repo;
44
+ // Blank value for first column (select checkboxes)
45
+ return ['', isRepoFavorited(repo), name, branches, prs, workspaces, lastCommit];
46
+ },
47
+ [activeSortIndex, activeSortDirection]
48
+ );
49
+
50
+ // Note that we perform the sort as part of the component's render logic and not in onSort.
51
+ // We shouldn't store the list of data in state because we don't want to have to sync that with props.
52
+ let sortedRepositories = repositories;
53
+ if (activeSortIndex !== null) {
54
+ sortedRepositories = repositories.sort((a, b) => {
55
+ const aValue = getSortableRowValues(a)[activeSortIndex];
56
+ const bValue = getSortableRowValues(b)[activeSortIndex];
57
+ if (typeof aValue === 'number' || typeof aValue === 'boolean') {
58
+ // Numeric sort
59
+ if (activeSortDirection === 'asc') {
60
+ return (aValue as number) - (bValue as number);
61
+ }
62
+ return (bValue as number) - (aValue as number);
63
+ } else {
64
+ // String sort
65
+ if (activeSortDirection === 'asc') {
66
+ return (aValue as string).localeCompare(bValue as string);
67
+ }
68
+ return (bValue as string).localeCompare(aValue as string);
69
+ }
70
+ });
71
+ }
72
+
73
+ const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example
74
+ const selectableRepos = repositories.filter(isRepoSelectable);
75
+
76
+ // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
77
+ // This is to prevent state from being based on row order index in case we later add sorting.
78
+ const [selectedRepoNames, setSelectedRepoNames] = React.useState<string[]>([]);
79
+ const setRepoSelected = (repo: Repository, isSelecting = true) =>
80
+ setSelectedRepoNames(prevSelected => {
81
+ const otherSelectedRepoNames = prevSelected.filter(r => r !== repo.name);
82
+ return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
83
+ });
84
+ const selectAllRepos = (isSelecting = true) =>
85
+ setSelectedRepoNames(isSelecting ? selectableRepos.map(r => r.name) : []);
86
+ const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);
87
+
88
+ const columns: TableProps['cells'] = [
89
+ { title: 'Repositories', transforms: [sortable] },
90
+ 'Branches',
91
+ 'Pull requests',
92
+ 'Workspaces',
93
+ 'Last commit'
94
+ ];
95
+ const rows: TableProps['rows'] = sortedRepositories.map(repo => {
96
+ const favorited = isRepoFavorited(repo);
97
+ return {
98
+ favorited,
99
+ // The favorites button has defaults that can be overriden
100
+ favoritesProps: {
101
+ 'aria-label': favorited ? 'Favorited' : 'Not favorited', // Defaults to 'Starred' / 'Not starred'
102
+ id: `favorites-button-${repo.name}`, // Defaults to `favorites-button-${rowIndex}`
103
+ 'aria-labelledby': `favorites-button-${repo.name}` // Defaults to `favorites-button-${rowIndex}`
104
+ },
105
+ selected: isRepoSelected(repo),
106
+ disableSelection: !isRepoSelectable(repo),
107
+ cells: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit]
108
+ };
109
+ });
110
+
111
+ const [canSortFavorites, setCanSortFavorites] = React.useState(true);
112
+
113
+ return (
114
+ <div>
115
+ <Checkbox
116
+ label="Can sort favorites"
117
+ isChecked={canSortFavorites}
118
+ onChange={setCanSortFavorites}
119
+ aria-label="toggle can sort favorites checkbox"
120
+ id="toggle-can-sort-favorites"
121
+ name="toggle-can-sort-favorites"
122
+ />
123
+ <Table
124
+ // using this prop enables the favorites column
125
+ onFavorite={(_event, isFavoriting, rowIndex) => {
126
+ const repo = sortedRepositories[rowIndex];
127
+ setRepoFavorited(repo, isFavoriting);
128
+ }}
129
+ // if the onSort prop is detected, favorites can be sorted
130
+ // if you want to exclude favorites from sorting you can use this prop with a value of `false`
131
+ canSortFavorites={canSortFavorites}
132
+ onSelect={(_event, isSelecting, rowIndex) => {
133
+ if (rowIndex === -1) {
134
+ selectAllRepos(isSelecting);
135
+ } else {
136
+ const repo = sortedRepositories[rowIndex];
137
+ setRepoSelected(repo, isSelecting);
138
+ }
139
+ }}
140
+ onSort={(_event, index, direction) => {
141
+ setActiveSortIndex(index);
142
+ setActiveSortDirection(direction);
143
+ }}
144
+ sortBy={{
145
+ index: activeSortIndex,
146
+ direction: activeSortDirection
147
+ }}
148
+ aria-label="Favoritable Table"
149
+ cells={columns}
150
+ rows={rows}
151
+ >
152
+ <TableHeader />
153
+ <TableBody />
154
+ </Table>
155
+ </div>
156
+ );
157
+ };
@@ -0,0 +1,125 @@
1
+ import React from 'react';
2
+ import { Table, TableHeader, TableBody, info, textCenter, TableProps, ICell } from '@patternfly/react-table';
3
+ import styles from '@patternfly/react-styles/css/components/Table/table';
4
+
5
+ interface Repository {
6
+ name: string;
7
+ branches: string | null;
8
+ prs: string | null;
9
+ workspaces: string;
10
+ lastCommit: string;
11
+ }
12
+
13
+ export const LegacyTableHoverable: React.FunctionComponent = () => {
14
+ // In real usage, this data would come from some external source like an API via props.
15
+ const repositories: Repository[] = [
16
+ {
17
+ name: 'Repository one',
18
+ branches: 'Branch one',
19
+ prs: 'PR one',
20
+ workspaces: 'Workspace one',
21
+ lastCommit: 'Commit one'
22
+ },
23
+ { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
24
+ {
25
+ name: 'one - 3',
26
+ branches: 'two - 3',
27
+ prs: 'three - 3',
28
+ workspaces: 'four - 3',
29
+ lastCommit: 'five - 3 (not centered)'
30
+ }
31
+ ];
32
+
33
+ // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
34
+ // This is to prevent state from being based on row order index in case we later add sorting.
35
+ const [selectedRepoNames, setSelectedRepoNames] = React.useState<string[]>([]);
36
+ const setRepoSelected = (repo: Repository, isSelecting = true) =>
37
+ setSelectedRepoNames(prevSelected => {
38
+ const otherSelectedRepoNames = prevSelected.filter(r => r !== repo.name);
39
+ return isSelecting ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
40
+ });
41
+ const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);
42
+
43
+ const columns: TableProps['cells'] = [
44
+ {
45
+ title: 'Repositories',
46
+ transforms: [
47
+ info({
48
+ tooltip: 'More information about repositories',
49
+ className: 'repositories-info-tip',
50
+ tooltipProps: {
51
+ isContentLeftAligned: true
52
+ }
53
+ })
54
+ ]
55
+ },
56
+ 'Branches',
57
+ {
58
+ title: 'Pull requests',
59
+ transforms: [
60
+ info({
61
+ popover: (
62
+ <div>
63
+ More <strong>information</strong> on pull requests
64
+ </div>
65
+ ),
66
+ ariaLabel: 'More information on pull requests',
67
+ popoverProps: {
68
+ headerContent: 'Pull requests',
69
+ footerContent: <a href="#">Click here for even more info</a>
70
+ }
71
+ })
72
+ ]
73
+ },
74
+ 'Workspaces',
75
+ {
76
+ title: 'Last commit',
77
+ transforms: [textCenter],
78
+ cellTransforms: [textCenter]
79
+ }
80
+ ];
81
+ const rows: TableProps['rows'] = repositories.map(repo => {
82
+ let cells: (string | ICell)[] = [repo.name, repo.branches || '', repo.prs || '', repo.workspaces, repo.lastCommit];
83
+ // These rows have arbitrary differences for this example, but these could be based on some other conditions
84
+ if (repo.name === 'one - 2') {
85
+ cells = [
86
+ {
87
+ title: <div>{repo.name}</div>,
88
+ props: { title: 'hover title', colSpan: 3 }
89
+ },
90
+ repo.workspaces,
91
+ repo.lastCommit
92
+ ];
93
+ } else if (repo.name === 'five - 3 (not centrered)') {
94
+ cells = [
95
+ repo.name,
96
+ repo.branches || '',
97
+ repo.prs || '',
98
+ repo.workspaces,
99
+ {
100
+ title: repo.lastCommit,
101
+ props: { textCenter: false }
102
+ }
103
+ ];
104
+ }
105
+ return {
106
+ cells,
107
+ isHoverable: true,
108
+ isRowSelected: isRepoSelected(repo)
109
+ };
110
+ });
111
+
112
+ return (
113
+ <Table caption="Row click handler table" cells={columns} rows={rows}>
114
+ <TableHeader className={styles.modifiers.nowrap} />
115
+ <TableBody
116
+ onRowClick={(_event, row, rowProps) => {
117
+ if (rowProps) {
118
+ const repo = repositories[rowProps.rowIndex];
119
+ setRepoSelected(repo, !isRepoSelected(repo));
120
+ }
121
+ }}
122
+ />
123
+ </Table>
124
+ );
125
+ };
@@ -0,0 +1,56 @@
1
+ import React from 'react';
2
+ import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table';
3
+ import { css } from '@patternfly/react-styles';
4
+
5
+ interface Repository {
6
+ name: string;
7
+ branches: string | null;
8
+ prs: string | null;
9
+ workspaces: string;
10
+ lastCommit: string;
11
+ }
12
+
13
+ export const LegacyTableMisc: React.FunctionComponent = () => {
14
+ // In real usage, this data would come from some external source like an API via props.
15
+ const repositories: Repository[] = [
16
+ { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
17
+ { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
18
+ { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' }
19
+ ];
20
+
21
+ const columns: TableProps['cells'] = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
22
+ const rows: TableProps['rows'] = repositories.map(repo => [
23
+ repo.name,
24
+ repo.branches || '',
25
+ repo.prs || '',
26
+ repo.workspaces,
27
+ repo.lastCommit
28
+ ]);
29
+
30
+ const customRowWrapper: TableProps['rowWrapper'] = ({ trRef, className, rowProps, row: _row, ...props }) => {
31
+ const isOddRow = rowProps ? !!((rowProps.rowIndex + 1) % 2) : true;
32
+ const customStyle = {
33
+ borderLeft: '3px solid var(--pf-global--primary-color--100)'
34
+ };
35
+ return (
36
+ <tr
37
+ {...props}
38
+ ref={trRef as React.LegacyRef<HTMLTableRowElement>}
39
+ className={css(className, isOddRow ? 'odd-row-class' : 'even-row-class', 'custom-static-class')}
40
+ style={isOddRow ? customStyle : {}}
41
+ />
42
+ );
43
+ };
44
+
45
+ return (
46
+ <Table
47
+ caption="Table with custom row wrapper that styles odd rows"
48
+ cells={columns}
49
+ rows={rows}
50
+ rowWrapper={customRowWrapper}
51
+ >
52
+ <TableHeader />
53
+ <TableBody />
54
+ </Table>
55
+ );
56
+ };
@@ -0,0 +1,124 @@
1
+ import React from 'react';
2
+ import { Table, TableBody, TableHeader, headerCol, TableProps } from '@patternfly/react-table';
3
+ import { Checkbox } from '@patternfly/react-core';
4
+
5
+ interface Repository {
6
+ name: string;
7
+ branches: string;
8
+ prs: string;
9
+ workspaces: string;
10
+ lastCommit: string;
11
+ }
12
+
13
+ export const LegacyTableSelectable: React.FunctionComponent = () => {
14
+ // In real usage, this data would come from some external source like an API via props.
15
+ const repositories: Repository[] = [
16
+ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
17
+ { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
18
+ { name: 'b', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
19
+ { name: 'c', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
20
+ { name: 'd', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
21
+ { name: 'e', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
22
+ ];
23
+
24
+ const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example
25
+ const selectableRepos = repositories.filter(isRepoSelectable);
26
+
27
+ // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
28
+ // This is to prevent state from being based on row order index in case we later add sorting.
29
+ const [selectedRepoNames, setSelectedRepoNames] = React.useState<string[]>([]);
30
+ const setRepoSelected = (repo: Repository, isSelecting = true) =>
31
+ setSelectedRepoNames(prevSelected => {
32
+ const otherSelectedRepoNames = prevSelected.filter(r => r !== repo.name);
33
+ return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
34
+ });
35
+ const selectAllRepos = (isSelecting = true) =>
36
+ setSelectedRepoNames(isSelecting ? selectableRepos.map(r => r.name) : []);
37
+ const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);
38
+
39
+ const [canSelectAll, setCanSelectAll] = React.useState(true);
40
+
41
+ // To allow shift+click to select/deselect multiple rows
42
+ const [recentSelectedRowIndex, setRecentSelectedRowIndex] = React.useState<number | null>(null);
43
+ const [shifting, setShifting] = React.useState(false);
44
+
45
+ const onSelectRepo = (repo: Repository, rowIndex: number, isSelecting: boolean) => {
46
+ // If the user is shift + selecting the checkboxes, then all intermediate checkboxes should be selected
47
+ if (shifting && recentSelectedRowIndex !== null) {
48
+ const numberSelected = rowIndex - recentSelectedRowIndex;
49
+ const intermediateIndexes =
50
+ numberSelected > 0
51
+ ? Array.from(new Array(numberSelected + 1), (_x, i) => i + recentSelectedRowIndex)
52
+ : Array.from(new Array(Math.abs(numberSelected) + 1), (_x, i) => i + rowIndex);
53
+ intermediateIndexes.forEach(index => setRepoSelected(repositories[index], isSelecting));
54
+ } else {
55
+ setRepoSelected(repo, isSelecting);
56
+ }
57
+ setRecentSelectedRowIndex(rowIndex);
58
+ };
59
+
60
+ React.useEffect(() => {
61
+ const onKeyDown = (e: KeyboardEvent) => {
62
+ if (e.key === 'Shift') {
63
+ setShifting(true);
64
+ }
65
+ };
66
+ const onKeyUp = (e: KeyboardEvent) => {
67
+ if (e.key === 'Shift') {
68
+ setShifting(false);
69
+ }
70
+ };
71
+
72
+ document.addEventListener('keydown', onKeyDown);
73
+ document.addEventListener('keyup', onKeyUp);
74
+
75
+ return () => {
76
+ document.removeEventListener('keydown', onKeyDown);
77
+ document.removeEventListener('keyup', onKeyUp);
78
+ };
79
+ }, []);
80
+
81
+ const columns: TableProps['cells'] = [
82
+ { title: 'Repositories', cellTransforms: [headerCol()] },
83
+ 'Branches',
84
+ { title: 'Pull requests' },
85
+ 'Workspaces',
86
+ 'Last commit'
87
+ ];
88
+ const rows: TableProps['rows'] = repositories.map(repo => ({
89
+ cells: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit],
90
+ selected: isRepoSelected(repo),
91
+ disableSelection: !isRepoSelectable(repo)
92
+ }));
93
+
94
+ return (
95
+ <div>
96
+ <Checkbox
97
+ label="Can select all"
98
+ className="pf-u-mb-lg"
99
+ isChecked={canSelectAll}
100
+ onChange={checked => setCanSelectAll(checked)}
101
+ aria-label="toggle select all checkbox"
102
+ id="toggle-select-all"
103
+ name="toggle-select-all"
104
+ />
105
+ <Table
106
+ onSelect={(_event, isSelecting, rowIndex) => {
107
+ if (rowIndex === -1) {
108
+ selectAllRepos(isSelecting);
109
+ } else {
110
+ const repo = repositories[rowIndex];
111
+ onSelectRepo(repo, rowIndex, isSelecting);
112
+ }
113
+ }}
114
+ canSelectAll={canSelectAll}
115
+ aria-label="Selectable Table"
116
+ cells={columns}
117
+ rows={rows}
118
+ >
119
+ <TableHeader />
120
+ <TableBody />
121
+ </Table>
122
+ </div>
123
+ );
124
+ };
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+ import { Table, TableHeader, TableBody, headerCol, TableProps } from '@patternfly/react-table';
3
+
4
+ interface Repository {
5
+ name: string;
6
+ branches: string;
7
+ prs: string;
8
+ workspaces: string;
9
+ lastCommit: string;
10
+ }
11
+
12
+ export const LegacyTableSelectableRadio: React.FunctionComponent = () => {
13
+ // In real usage, this data would come from some external source like an API via props.
14
+ const repositories: Repository[] = [
15
+ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
16
+ { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
17
+ { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
18
+ ];
19
+
20
+ const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example
21
+
22
+ // In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
23
+ // This is to prevent state from being based on row order index in case we later add sorting.
24
+ const [selectedRepoName, setSelectedRepoName] = React.useState<string | null>(null);
25
+
26
+ const columns: TableProps['cells'] = [
27
+ { title: 'Repositories', cellTransforms: [headerCol()] },
28
+ 'Branches',
29
+ { title: 'Pull requests' },
30
+ 'Workspaces',
31
+ 'Last commit'
32
+ ];
33
+ const rows: TableProps['rows'] = repositories.map(repo => ({
34
+ cells: [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit],
35
+ selected: selectedRepoName === repo.name,
36
+ disableSelection: !isRepoSelectable(repo)
37
+ }));
38
+
39
+ return (
40
+ <Table
41
+ onSelect={(_event, _isSelecting, rowIndex) => {
42
+ const repo = repositories[rowIndex];
43
+ setSelectedRepoName(repo.name);
44
+ }}
45
+ selectVariant="radio"
46
+ aria-label="Selectable Table with Radio Buttons"
47
+ cells={columns}
48
+ rows={rows}
49
+ >
50
+ <TableHeader />
51
+ <TableBody />
52
+ </Table>
53
+ );
54
+ };
@@ -0,0 +1,120 @@
1
+ import React from 'react';
2
+ import {
3
+ TableProps,
4
+ sortable,
5
+ info,
6
+ cellWidth,
7
+ wrappable,
8
+ Table,
9
+ TableBody,
10
+ TableHeader
11
+ } from '@patternfly/react-table';
12
+
13
+ interface Repository {
14
+ name: string;
15
+ branches: string;
16
+ prs: string;
17
+ workspaces: string;
18
+ lastCommit: string;
19
+ }
20
+
21
+ export const LegacyTableSortable: React.FunctionComponent = () => {
22
+ // In real usage, this data would come from some external source like an API via props.
23
+ const repositories: Repository[] = [
24
+ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
25
+ { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
26
+ { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
27
+ ];
28
+
29
+ // Index of the currently sorted column
30
+ // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key
31
+ // as the identifier of the sorted column. See the "Compound expandable" example.
32
+ const [activeSortIndex, setActiveSortIndex] = React.useState<number | null>(null);
33
+
34
+ // Sort direction of the currently sorted column
35
+ const [activeSortDirection, setActiveSortDirection] = React.useState<'asc' | 'desc' | null>(null);
36
+
37
+ // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index.
38
+ // This example is trivial since our data objects just contain strings, but if the data was more complex
39
+ // this would be a place to return simplified string or number versions of each column to sort by.
40
+ const getSortableRowValues = (repo: Repository): (string | number)[] => {
41
+ const { name, branches, prs, workspaces, lastCommit } = repo;
42
+ return [name, branches, prs, workspaces, lastCommit];
43
+ };
44
+
45
+ // Note that we perform the sort as part of the component's render logic and not in onSort.
46
+ // We shouldn't store the list of data in state because we don't want to have to sync that with props.
47
+ let sortedRepositories = repositories;
48
+ if (activeSortIndex !== null) {
49
+ sortedRepositories = repositories.sort((a, b) => {
50
+ const aValue = getSortableRowValues(a)[activeSortIndex];
51
+ const bValue = getSortableRowValues(b)[activeSortIndex];
52
+ if (typeof aValue === 'number') {
53
+ // Numeric sort
54
+ if (activeSortDirection === 'asc') {
55
+ return (aValue as number) - (bValue as number);
56
+ }
57
+ return (bValue as number) - (aValue as number);
58
+ } else {
59
+ // String sort
60
+ if (activeSortDirection === 'asc') {
61
+ return (aValue as string).localeCompare(bValue as string);
62
+ }
63
+ return (bValue as string).localeCompare(aValue as string);
64
+ }
65
+ });
66
+ }
67
+
68
+ const columns: TableProps['cells'] = [
69
+ { title: 'Repositories', transforms: [sortable] },
70
+ {
71
+ title: 'Branches',
72
+ transforms: [
73
+ info({
74
+ tooltip: 'More information about branches'
75
+ }),
76
+ sortable
77
+ ]
78
+ },
79
+ { title: 'Pull requests', transforms: [sortable] },
80
+ {
81
+ title: 'Workspaces - Also this long header will wrap since we have applied the wrappable transform',
82
+ transforms: [sortable, wrappable, cellWidth(20)]
83
+ },
84
+ {
85
+ title: 'Last commit',
86
+ transforms: [
87
+ info({
88
+ tooltip: 'More information about commits'
89
+ })
90
+ ]
91
+ }
92
+ ];
93
+ const rows: TableProps['rows'] = sortedRepositories.map(repo => [
94
+ repo.name,
95
+ repo.branches,
96
+ repo.prs,
97
+ repo.workspaces,
98
+ repo.lastCommit
99
+ ]);
100
+
101
+ return (
102
+ <Table
103
+ aria-label="Sortable Table"
104
+ sortBy={{
105
+ index: activeSortIndex,
106
+ direction: activeSortDirection,
107
+ defaultDirection: 'asc' // starting sort direction when first sorting a column. Defaults to 'asc'
108
+ }}
109
+ onSort={(_event, index, direction) => {
110
+ setActiveSortIndex(index);
111
+ setActiveSortDirection(direction);
112
+ }}
113
+ cells={columns}
114
+ rows={rows}
115
+ >
116
+ <TableHeader />
117
+ <TableBody />
118
+ </Table>
119
+ );
120
+ };