Skip to content

Commit 8784729

Browse files
authored
Merge pull request #25 from fhlavac/empty
Add empty state support
2 parents 35b4322 + 358cfc8 commit 8784729

File tree

15 files changed

+343
-32
lines changed

15 files changed

+343
-32
lines changed

cypress.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default defineConfig({
1313
},
1414

1515
component: {
16+
viewportHeight: 1024,
17+
viewportWidth: 1400,
1618
devServer: {
1719
framework: "react",
1820
bundler: "webpack",

cypress/component/DataViewTable.cy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ describe('DataViewTable', () => {
117117
cy.get('[data-ouia-component-id="tree-td-4-3"]').contains('Workspace five');
118118
cy.get('[data-ouia-component-id="tree-td-4-3"]').should('not.be.visible');
119119
cy.get('[data-ouia-component-id="tree-td-5-4"]').contains('Timestamp six');
120-
cy.get('[data-ouia-component-id="tree-td-5-4"]').should('not.be.visible');
120+
cy.get('[data-ouia-component-id="tree-td-5-4"]').should('be.visible');
121121
});
122122
});

cypress/component/DataViewTableBasic.cy.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,21 @@ describe('DataViewTableBasic', () => {
4343
cy.get('[data-ouia-component-id="data-td-4-3"]').contains('Workspace five');
4444
cy.get('[data-ouia-component-id="data-td-5-4"]').contains('Timestamp six');
4545
});
46+
47+
it('renders a basic data view table with an empty state', () => {
48+
const ouiaId = 'data';
49+
50+
cy.mount(
51+
<DataViewTableBasic aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={[]} emptyState="No data found" />
52+
);
53+
54+
cy.get('[data-ouia-component-id="data-th-0"]').contains('Repositories');
55+
cy.get('[data-ouia-component-id="data-th-1"]').contains('Branches');
56+
cy.get('[data-ouia-component-id="data-th-2"]').contains('Pull requests');
57+
cy.get('[data-ouia-component-id="data-th-3"]').contains('Workspaces');
58+
cy.get('[data-ouia-component-id="data-th-4"]').contains('Last commit');
59+
60+
cy.get('[data-ouia-component-id="data-tr-empty"]').should('be.visible');
61+
cy.get('[data-ouia-component-id="data-tr-empty"]').contains('No data found');
62+
});
4663
});

cypress/component/DataViewTableTree.cy.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,21 @@ describe('DataViewTableTree', () => {
125125

126126
cy.get('[data-ouia-component-id="tree-td-4-0"]').should('not.be.visible');
127127
});
128+
129+
it('renders a tree data view table with an empty state', () => {
130+
const ouiaId = 'tree';
131+
132+
cy.mount(
133+
<DataViewTable isTreeTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={[]} emptyState="No data found" />
134+
);
135+
136+
cy.get('[data-ouia-component-id="tree-th-0"]').contains('Repositories');
137+
cy.get('[data-ouia-component-id="tree-th-1"]').contains('Branches');
138+
cy.get('[data-ouia-component-id="tree-th-2"]').contains('Pull requests');
139+
cy.get('[data-ouia-component-id="tree-th-3"]').contains('Workspaces');
140+
cy.get('[data-ouia-component-id="tree-th-4"]').contains('Last commit');
141+
142+
cy.get('[data-ouia-component-id="tree-tr-empty"]').should('be.visible');
143+
cy.get('[data-ouia-component-id="tree-tr-empty"]').contains('No data found');
144+
});
128145
});

packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ sortValue: 4
1414
propComponents: ['DataViewToolbar', 'DataViewTableBasic', 'DataViewTableTree']
1515
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md
1616
---
17-
import { FolderIcon, FolderOpenIcon, LeafIcon, ExclamationCircleIcon } from '@patternfly/react-icons';
17+
import { Button, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon } from '@patternfly/react-core';
18+
import { CubesIcon, FolderIcon, FolderOpenIcon, LeafIcon, ExclamationCircleIcon } from '@patternfly/react-icons';
1819
import { BulkSelect } from '@patternfly/react-component-groups';
1920
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
2021
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
@@ -71,3 +72,10 @@ It is also possible to disable row selection using the `isSelectDisabled` functi
7172
```js file="./DataViewTableTreeExample.tsx"
7273

7374
```
75+
76+
### Empty state example
77+
The data view table also supports displaying a custom empty state. You can pass it using the `emptyState` property and it will be displayed in case there are no rows to be rendered.
78+
79+
```js file="./DataViewTableEmptyExample.tsx"
80+
81+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
3+
import { CubesIcon } from '@patternfly/react-icons';
4+
import { Button, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon } from '@patternfly/react-core';
5+
6+
interface Repository {
7+
id: number;
8+
name: string;
9+
branches: string | null;
10+
prs: string | null;
11+
workspaces: string;
12+
lastCommit: string;
13+
}
14+
15+
const repositories: Repository[] = [];
16+
17+
// you can also pass props to Tr by returning { row: DataViewTd[], props: TrProps } }
18+
const rows: DataViewTr[] = repositories.map((repository) => Object.values(repository));
19+
20+
const columns: DataViewTh[] = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
21+
22+
const ouiaId = 'TableExample';
23+
24+
const emptyState = (
25+
<EmptyState>
26+
<EmptyStateHeader titleText="No data found" headingLevel="h4" icon={<EmptyStateIcon icon={CubesIcon} />} />
27+
<EmptyStateBody>There are no matching data to be displayed.</EmptyStateBody>
28+
<EmptyStateFooter>
29+
<EmptyStateActions>
30+
<Button variant="primary">Primary action</Button>
31+
</EmptyStateActions>
32+
<EmptyStateActions>
33+
<Button variant="link">Multiple</Button>
34+
<Button variant="link">Action Buttons</Button>
35+
</EmptyStateActions>
36+
</EmptyStateFooter>
37+
</EmptyState>
38+
);
39+
40+
export const BasicExample: React.FunctionComponent = () => (
41+
<DataViewTable
42+
aria-label='Repositories table'
43+
ouiaId={ouiaId}
44+
columns={columns}
45+
rows={rows}
46+
emptyState={emptyState}
47+
/>
48+
);

packages/module/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableTreeExample.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,13 @@ const buildRows = (repositories: Repository[]): DataViewTrTree[] => repositories
4242
...(repo.children
4343
? {
4444
children: buildRows(repo.children) // build rows for children
45-
}
45+
}
4646
: {})
4747
}));
4848

4949
const rows: DataViewTrTree[] = buildRows(repositories);
5050

51-
const columns: DataViewTh[] = [
52-
'Repositories',
53-
'Branches',
54-
'Pull requests',
55-
'Workspaces',
56-
'Last commit'
57-
];
51+
const columns: DataViewTh[] = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
5852

5953
const ouiaId = 'TreeTableExample';
6054

@@ -63,7 +57,15 @@ export const BasicExample: React.FunctionComponent = () => {
6357

6458
return (
6559
<DataView selection={selection}>
66-
<DataViewTable isTreeTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} leafIcon={<LeafIcon/>} expandedIcon={<FolderOpenIcon aria-hidden />} collapsedIcon={<FolderIcon aria-hidden />} />
60+
<DataViewTable
61+
isTreeTable
62+
ouiaId={ouiaId}
63+
columns={columns}
64+
rows={rows}
65+
leafIcon={<LeafIcon/>}
66+
expandedIcon={<FolderOpenIcon aria-hidden />}
67+
collapsedIcon={<FolderIcon aria-hidden />}
68+
/>
6769
</DataView>
6870
);
6971
}

packages/module/patternfly-docs/generated/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,23 @@ module.exports = {
3434
sortValue: 3,
3535
Component: () => import(/* webpackChunkName: "extensions/data-view/functionality/react/index" */ './extensions/data-view/functionality/react')
3636
},
37+
'/extensions/data-view/events-context/react': {
38+
id: "Events context",
39+
title: "Events context",
40+
toc: [[{"text":"Row click subscription example"}]],
41+
examples: ["Row click subscription example"],
42+
section: "extensions",
43+
subsection: "Data view",
44+
source: "react",
45+
tabName: null,
46+
sortValue: 3,
47+
Component: () => import(/* webpackChunkName: "extensions/data-view/events-context/react/index" */ './extensions/data-view/events-context/react')
48+
},
3749
'/extensions/data-view/components/react': {
3850
id: "Components",
3951
title: "Components",
40-
toc: [{"text":"Data view toolbar"},[{"text":"Basic example"}]],
41-
examples: ["Basic example"],
52+
toc: [{"text":"Data view toolbar"},[{"text":"Basic toolbar example"}],{"text":"Data view table"},[{"text":"Rows and columns customization"},{"text":"Tree table example"},{"text":"Empty state example"}]],
53+
examples: ["Basic toolbar example","Rows and columns customization","Tree table example","Empty state example"],
4254
section: "extensions",
4355
subsection: "Data view",
4456
source: "react",

packages/module/src/DataViewTable/__snapshots__/DataViewTable.test.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
395395
aria-posinset="1"
396396
aria-setsize="2"
397397
class="pf-v5-c-table__tr"
398-
data-ouia-component-id="OUIA-Generated-TableRow-9"
398+
data-ouia-component-id="TableExample-tr-0"
399399
data-ouia-component-type="PF5/TableRow"
400400
data-ouia-safe="true"
401401
>
@@ -519,7 +519,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
519519
aria-posinset="1"
520520
aria-setsize="0"
521521
class="pf-v5-c-table__tr"
522-
data-ouia-component-id="OUIA-Generated-TableRow-10"
522+
data-ouia-component-id="TableExample-tr-1"
523523
data-ouia-component-type="PF5/TableRow"
524524
data-ouia-safe="true"
525525
hidden=""
@@ -611,7 +611,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
611611
aria-posinset="2"
612612
aria-setsize="0"
613613
class="pf-v5-c-table__tr"
614-
data-ouia-component-id="OUIA-Generated-TableRow-11"
614+
data-ouia-component-id="TableExample-tr-2"
615615
data-ouia-component-type="PF5/TableRow"
616616
data-ouia-safe="true"
617617
hidden=""
@@ -703,7 +703,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
703703
aria-posinset="2"
704704
aria-setsize="1"
705705
class="pf-v5-c-table__tr"
706-
data-ouia-component-id="OUIA-Generated-TableRow-12"
706+
data-ouia-component-id="TableExample-tr-3"
707707
data-ouia-component-type="PF5/TableRow"
708708
data-ouia-safe="true"
709709
>
@@ -827,7 +827,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
827827
aria-posinset="1"
828828
aria-setsize="0"
829829
class="pf-v5-c-table__tr"
830-
data-ouia-component-id="OUIA-Generated-TableRow-13"
830+
data-ouia-component-id="TableExample-tr-4"
831831
data-ouia-component-type="PF5/TableRow"
832832
data-ouia-safe="true"
833833
hidden=""
@@ -919,7 +919,7 @@ exports[`DataViewTable component should render a tree table correctly 1`] = `
919919
aria-posinset="3"
920920
aria-setsize="0"
921921
class="pf-v5-c-table__tr"
922-
data-ouia-component-id="OUIA-Generated-TableRow-14"
922+
data-ouia-component-id="TableExample-tr-5"
923923
data-ouia-component-type="PF5/TableRow"
924924
data-ouia-safe="true"
925925
>

packages/module/src/DataViewTableBasic/DataViewTableBasic.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ describe('DataViewTable component', () => {
3434
);
3535
expect(container).toMatchSnapshot();
3636
});
37+
38+
test('should render with an empty state', () => {
39+
const { container } = render(
40+
<DataViewTableBasic aria-label='Repositories table' ouiaId={ouiaId} columns={columns} emptyState="No data found" rows={[]} />
41+
);
42+
expect(container).toMatchSnapshot();
43+
});
3744
});

0 commit comments

Comments
 (0)