@patternfly/react-table 4.44.12 → 4.44.16

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 (70) hide show
  1. package/CHANGELOG.md +35 -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/dist/umd/react-table.css +250 -250
  23. package/dist/umd/react-table.js +9 -1
  24. package/dist/umd/react-table.min.js +3 -3
  25. package/package.json +6 -6
  26. package/src/components/Table/Table.tsx +2 -1
  27. package/src/components/Table/TableTypes.tsx +2 -1
  28. package/src/components/Table/examples/LegacyTableActions.tsx +218 -0
  29. package/src/components/Table/examples/LegacyTableBasic.tsx +90 -0
  30. package/src/components/Table/examples/LegacyTableCellWidth.tsx +66 -0
  31. package/src/components/Table/examples/LegacyTableCompoundExpandable.tsx +175 -0
  32. package/src/components/Table/examples/LegacyTableControllingText.tsx +51 -0
  33. package/src/components/Table/examples/LegacyTableEmptyState.tsx +48 -0
  34. package/src/components/Table/examples/LegacyTableExpandable.tsx +172 -0
  35. package/src/components/Table/examples/LegacyTableFavoritable.tsx +157 -0
  36. package/src/components/Table/examples/LegacyTableHoverable.tsx +125 -0
  37. package/src/components/Table/examples/LegacyTableMisc.tsx +56 -0
  38. package/src/components/Table/examples/LegacyTableSelectable.tsx +124 -0
  39. package/src/components/Table/examples/LegacyTableSelectableRadio.tsx +54 -0
  40. package/src/components/Table/examples/LegacyTableSortable.tsx +120 -0
  41. package/src/components/Table/examples/LegacyTableSortableCustom.tsx +189 -0
  42. package/src/components/Table/examples/LegacyTableTextModifiers.tsx +31 -0
  43. package/src/components/Table/examples/LegacyTableTree.tsx +213 -0
  44. package/src/components/Table/examples/Table.md +83 -1848
  45. package/src/components/TableComposable/examples/ComposableTable.md +31 -2805
  46. package/src/components/TableComposable/examples/ComposableTableActions.tsx +163 -0
  47. package/src/components/TableComposable/examples/ComposableTableBasic.tsx +89 -0
  48. package/src/components/TableComposable/examples/ComposableTableCellWidth.tsx +56 -0
  49. package/src/components/TableComposable/examples/ComposableTableCompoundExpandable.tsx +103 -0
  50. package/src/components/TableComposable/examples/ComposableTableControllingText.tsx +51 -0
  51. package/src/components/TableComposable/examples/ComposableTableDraggable.tsx +170 -0
  52. package/src/components/TableComposable/examples/ComposableTableEmptyState.tsx +45 -0
  53. package/src/components/TableComposable/examples/ComposableTableExpandable.tsx +195 -0
  54. package/src/components/TableComposable/examples/ComposableTableFavoritable.tsx +118 -0
  55. package/src/components/TableComposable/examples/ComposableTableHoverable.tsx +67 -0
  56. package/src/components/TableComposable/examples/ComposableTableMisc.tsx +92 -0
  57. package/src/components/TableComposable/examples/ComposableTableMultipleStickyColumns.tsx +153 -0
  58. package/src/components/TableComposable/examples/ComposableTableNestedExpandable.tsx +127 -0
  59. package/src/components/TableComposable/examples/ComposableTableNestedHeaders.tsx +202 -0
  60. package/src/components/TableComposable/examples/ComposableTableSelectable.tsx +125 -0
  61. package/src/components/TableComposable/examples/ComposableTableSelectableRadio.tsx +68 -0
  62. package/src/components/TableComposable/examples/ComposableTableSortable.tsx +107 -0
  63. package/src/components/TableComposable/examples/ComposableTableSortableCustom.tsx +175 -0
  64. package/src/components/TableComposable/examples/ComposableTableStickyColumn.tsx +144 -0
  65. package/src/components/TableComposable/examples/ComposableTableStickyColumnsAndHeader.tsx +167 -0
  66. package/src/components/TableComposable/examples/ComposableTableTextModifiers.tsx +33 -0
  67. package/src/components/TableComposable/examples/ComposableTableTree.tsx +201 -0
  68. package/src/demos/Table.md +7 -6
  69. package/src/components/Table/examples/DemoSortableTable.js +0 -65
  70. package/src/components/TableComposable/examples/DemoSortableTable.js +0 -65
@@ -0,0 +1,48 @@
1
+ import React from 'react';
2
+ import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table';
3
+ import {
4
+ Bullseye,
5
+ EmptyState,
6
+ EmptyStateVariant,
7
+ EmptyStateIcon,
8
+ Title,
9
+ EmptyStateBody,
10
+ Button
11
+ } from '@patternfly/react-core';
12
+ import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
13
+
14
+ // This example has been simplified to focus on the empty state. In real usage,
15
+ // you may want to derive your rows from typed underlying data and minimal state. See other examples.
16
+
17
+ export const LegacyTableEmptyState: React.FunctionComponent = () => {
18
+ const columns: TableProps['cells'] = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit'];
19
+ const rows: TableProps['rows'] = [
20
+ {
21
+ heightAuto: true,
22
+ cells: [
23
+ {
24
+ props: { colSpan: 8 },
25
+ title: (
26
+ <Bullseye>
27
+ <EmptyState variant={EmptyStateVariant.small}>
28
+ <EmptyStateIcon icon={SearchIcon} />
29
+ <Title headingLevel="h2" size="lg">
30
+ No results found
31
+ </Title>
32
+ <EmptyStateBody>Clear all filters and try again.</EmptyStateBody>
33
+ <Button variant="link">Clear all filters</Button>
34
+ </EmptyState>
35
+ </Bullseye>
36
+ )
37
+ }
38
+ ]
39
+ }
40
+ ];
41
+
42
+ return (
43
+ <Table aria-label="Table text with modifiers" cells={columns} rows={rows}>
44
+ <TableHeader />
45
+ <TableBody />
46
+ </Table>
47
+ );
48
+ };
@@ -0,0 +1,172 @@
1
+ import React from 'react';
2
+ import { Table, TableBody, TableHeader, TableVariant, TableProps, expandable, IRowCell } 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
+ details?: {
11
+ detail1?: string;
12
+ detail2?: string;
13
+ detail3?: string;
14
+ detailFormat: 0 | 1 | 2 | 3;
15
+ };
16
+ }
17
+
18
+ export const LegacyTableExpandable: React.FunctionComponent = () => {
19
+ // In real usage, this data would come from some external source like an API via props.
20
+ const repositories: Repository[] = [
21
+ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four' },
22
+ {
23
+ name: 'parent 1',
24
+ branches: 'two',
25
+ prs: 'k',
26
+ workspaces: 'four',
27
+ // This `details` structure is just for this example. You can drive expanded content from any kind of data.
28
+ details: { detailFormat: 0, detail1: 'single cell' }
29
+ },
30
+ {
31
+ name: 'parent 2',
32
+ branches: 'two',
33
+ prs: 'b',
34
+ workspaces: 'four',
35
+ details: { detailFormat: 1, detail1: 'single cell - fullWidth' }
36
+ },
37
+ {
38
+ name: 'parent 3',
39
+ branches: '2',
40
+ prs: 'b',
41
+ workspaces: 'four',
42
+ details: { detailFormat: 2, detail1: 'single cell - noPadding' }
43
+ },
44
+ {
45
+ name: 'parent 4',
46
+ branches: '2',
47
+ prs: 'b',
48
+ workspaces: 'four',
49
+ details: { detailFormat: 3, detail1: 'single cell - fullWidth & noPadding' }
50
+ },
51
+ {
52
+ name: 'parent 5',
53
+ branches: '2',
54
+ prs: 'b',
55
+ workspaces: 'four',
56
+ details: {
57
+ detailFormat: 0,
58
+ detail1: "spans 'Repositories and 'Branches'",
59
+ detail2: "spans 'Pull requests' and 'Workspaces', and 'Last commit'"
60
+ }
61
+ },
62
+ {
63
+ name: 'parent 6',
64
+ branches: '2',
65
+ prs: 'b',
66
+ workspaces: 'four',
67
+ details: {
68
+ detailFormat: 1,
69
+ detail1: "fullWidth, spans the collapsible column and 'Repositories'",
70
+ detail2: "fullWidth, spans 'Branches' and 'Pull requests'",
71
+ detail3: 'fullWidth, spans the empty column'
72
+ }
73
+ }
74
+ ];
75
+
76
+ // In this example, expanded 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
+ // Note that this behavior is very similar to selection state.
79
+ const initialExpandedRepoNames = repositories.filter(repo => !!repo.details).map(repo => repo.name); // Default to all expanded
80
+ const [expandedRepoNames, setExpandedRepoNames] = React.useState<string[]>(initialExpandedRepoNames);
81
+ const setRepoExpanded = (repo: Repository, isExpanding = true) =>
82
+ setExpandedRepoNames(prevExpanded => {
83
+ const otherExpandedRepoNames = prevExpanded.filter(r => r !== repo.name);
84
+ return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames;
85
+ });
86
+ const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name);
87
+
88
+ const [isExampleCompact, setIsExampleCompact] = React.useState(true);
89
+
90
+ // We want to be able to reference the original data object based on row index. But because an expanded
91
+ // row takes up two row indexes, repositories[rowIndex] will not be accurate like it would in a normal table.
92
+ // One solution to this is to create an array of data objects indexed by the displayed row index.
93
+ const reposByRowIndex: (Repository | null)[] = [];
94
+
95
+ const columns: TableProps['cells'] = [
96
+ {
97
+ title: 'Repositories',
98
+ cellFormatters: [expandable]
99
+ },
100
+ 'Branches',
101
+ { title: 'Pull requests' },
102
+ { title: '' /* deliberately empty */, dataLabel: 'Label for mobile view' }
103
+ ];
104
+ const rows: TableProps['rows'] = [];
105
+ repositories.forEach(repo => {
106
+ rows.push({
107
+ ...(repo.details ? { isOpen: isRepoExpanded(repo) } : {}),
108
+ cells: [repo.name, repo.branches, repo.prs, repo.workspaces]
109
+ });
110
+ reposByRowIndex.push(repo);
111
+ if (repo.details) {
112
+ // Some arbitrary examples of how you could customize the child row based on your needs
113
+ const { detail1, detail2, detail3, detailFormat } = repo.details;
114
+ const numColumns = 5;
115
+ const childIsFullWidth = [1, 3].includes(detailFormat);
116
+ const childHasNoPadding = [2, 3].includes(detailFormat);
117
+ let detailColSpans = [1, 1, 1];
118
+ if (detail1 && !detail2 && !detail3) {
119
+ detailColSpans = [childIsFullWidth ? numColumns : numColumns + 1]; // Account for toggle column
120
+ } else if (detail1 && detail2 && !detail3) {
121
+ detailColSpans = [2, childIsFullWidth ? 3 : 4];
122
+ } else if (detail1 && detail2 && detail3) {
123
+ detailColSpans = [2, 2, childIsFullWidth ? 1 : 2];
124
+ }
125
+ const cells: IRowCell[] = [];
126
+ [detail1, detail2, detail3].forEach((detail, index) => {
127
+ if (detail) {
128
+ cells.push({
129
+ title: detail1,
130
+ props: {
131
+ colSpan: detailColSpans[index]
132
+ }
133
+ });
134
+ }
135
+ });
136
+ rows.push({
137
+ parent: rows.length - 1,
138
+ cells,
139
+ fullWidth: childIsFullWidth,
140
+ noPadding: childHasNoPadding
141
+ });
142
+ reposByRowIndex.push(null);
143
+ }
144
+ });
145
+
146
+ return (
147
+ <React.Fragment>
148
+ <Checkbox
149
+ label="Compact"
150
+ isChecked={isExampleCompact}
151
+ onChange={checked => setIsExampleCompact(checked)}
152
+ aria-label="toggle compact variation"
153
+ id="toggle-compact"
154
+ name="toggle-compact"
155
+ />
156
+ <Table
157
+ aria-label="Expandable table"
158
+ variant={isExampleCompact ? TableVariant.compact : undefined}
159
+ onCollapse={(_event, rowIndex, isOpen) => {
160
+ if (reposByRowIndex[rowIndex]) {
161
+ setRepoExpanded(reposByRowIndex[rowIndex], isOpen);
162
+ }
163
+ }}
164
+ rows={rows}
165
+ cells={columns}
166
+ >
167
+ <TableHeader />
168
+ <TableBody />
169
+ </Table>
170
+ </React.Fragment>
171
+ );
172
+ };
@@ -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
+ };