Skip to content

Commit a4d3470

Browse files
committed
fix(empty): Add empty state support
1 parent 35b4322 commit a4d3470

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

packages/module/src/DataViewTableBasic/DataViewTableBasic.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import {
33
Table,
44
TableProps,
@@ -15,6 +15,8 @@ export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | '
1515
columns: DataViewTh[];
1616
/** Current page rows */
1717
rows: DataViewTr[];
18+
/** Empty state to be displayed */
19+
emptyState?: React.ReactNode;
1820
/** Custom OUIA ID */
1921
ouiaId?: string;
2022
}
@@ -23,20 +25,22 @@ export const DataViewTableBasic: React.FC<DataViewTableBasicProps> = ({
2325
columns,
2426
rows,
2527
ouiaId = 'DataViewTableBasic',
28+
emptyState = null,
2629
...props
2730
}: DataViewTableBasicProps) => {
2831
const { selection } = useInternalContext();
2932
const { onSelect, isSelected, isSelectDisabled } = selection ?? {};
33+
const isSelectable = useMemo(() => Boolean(onSelect && isSelected), [ onSelect, isSelected ])
3034

3135
return (
3236
<Table aria-label="Data table" ouiaId={ouiaId} {...props}>
3337
<DataViewTableHeader columns={columns} ouiaId={ouiaId} />
3438
<Tbody>
35-
{rows.map((row, rowIndex) => {
39+
{rows?.length > 0 ? rows.map((row, rowIndex) => {
3640
const rowIsObject = isDataViewTrObject(row);
3741
return (
3842
<Tr key={rowIndex} ouiaId={`${ouiaId}-tr-${rowIndex}`} {...(rowIsObject && (row?.props ?? {}))}>
39-
{onSelect && isSelected && (
43+
{isSelectable && (
4044
<Td
4145
key={`select-${rowIndex}`}
4246
select={{
@@ -62,7 +66,13 @@ export const DataViewTableBasic: React.FC<DataViewTableBasicProps> = ({
6266
)
6367
})}
6468
</Tr>
65-
)})}
69+
)}) : (
70+
<Tr key="empty" ouiaId={`${ouiaId}-tr-empty`}>
71+
<Td colSpan={columns.length + Number(isSelectable)}>
72+
{emptyState}
73+
</Td>
74+
</Tr>
75+
)}
6676
</Tbody>
6777
</Table>
6878
);

packages/module/src/DataViewTableTree/DataViewTableTree.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Tbody,
66
Td,
77
TdProps,
8+
Tr,
89
TreeRowWrapper,
910
} from '@patternfly/react-table';
1011
import { useInternalContext } from '../InternalContext';
@@ -34,6 +35,8 @@ export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'r
3435
columns: DataViewTh[];
3536
/** Current page rows */
3637
rows: DataViewTrTree[];
38+
/** Empty state to be displayed */
39+
emptyState?: React.ReactNode;
3740
/** Optional icon for the leaf rows */
3841
leafIcon?: React.ReactNode;
3942
/** Optional icon for the expanded parent rows */
@@ -47,6 +50,7 @@ export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'r
4750
export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
4851
columns,
4952
rows,
53+
emptyState = null,
5054
leafIcon = null,
5155
expandedIcon = null,
5256
collapsedIcon = null,
@@ -99,7 +103,6 @@ export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
99103
'aria-posinset': posinset,
100104
'aria-setsize': node.children?.length ?? 0,
101105
isChecked,
102-
ouiaId: `${ouiaId}-tree-toggle-${node.id}`,
103106
checkboxId: `checkbox_id_${node.id?.toLowerCase().replace(/\s+/g, '_')}`,
104107
icon,
105108
},
@@ -110,7 +113,7 @@ export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
110113
: [];
111114

112115
return [
113-
<TreeRowWrapper key={node.id} row={{ props: treeRow.props }}>
116+
<TreeRowWrapper key={node.id} row={{ props: treeRow.props }} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
114117
{node.row.map((cell, colIndex) => {
115118
const cellIsObject = isDataViewTdObject(cell);
116119
return (
@@ -136,7 +139,15 @@ export const DataViewTableTree: React.FC<DataViewTableTreeProps> = ({
136139
return (
137140
<Table isTreeTable aria-label="Data table" ouiaId={ouiaId} {...props}>
138141
<DataViewTableHeader isTreeTable columns={columns} ouiaId={ouiaId} />
139-
<Tbody>{nodes}</Tbody>
142+
<Tbody>
143+
{nodes.length > 0 ? nodes : (
144+
<Tr key="empty" ouiaId={`${ouiaId}-tr-empty`}>
145+
<Td colSpan={columns.length}>
146+
{emptyState}
147+
</Td>
148+
</Tr>
149+
)}
150+
</Tbody>
140151
</Table>
141152
);
142153
};

0 commit comments

Comments
 (0)