Skip to content

Commit

Permalink
feat: expose noDataIcon props to display more icon while table is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
anishkumar-bhut committed Feb 21, 2024
1 parent a1c0955 commit 1d7c69f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 48 deletions.
108 changes: 60 additions & 48 deletions packages/react/src/components/Table/EmptyTable/EmptyTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ const propTypes = {
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
testId: PropTypes.string,
/** icon to display while no data in table */
noDataIcon: PropTypes.oneOfType([
PropTypes.func,
PropTypes.oneOf(['error', 'error404', 'empty', 'not-authorized', 'no-result', 'success', '']),
]),
};

const defaultProps = {
id: 'EmptyTable',
onEmptyStateAction: null,
testId: '',
noDataIcon: '',
};

const EmptyTable = ({
Expand All @@ -49,55 +55,61 @@ const EmptyTable = ({
// TODO: remove deprecated 'testID' in v3
testID,
testId,
}) => (
<TableBody
id={`${id}-empty-table`}
// TODO: remove deprecated 'testID' in v3
data-testid={testID || testId}
>
<TableRow className={`${iotPrefix}--empty-table--table-row`}>
<TableCell colSpan={totalColumns}>
{React.isValidElement(emptyState) ? (
emptyState
) : (
<div className="empty-table-cell--default">
{isFiltered ? (
<EmptyState
icon="no-result"
title={messageWithFilters}
body={messageWithFiltersBody || ''}
action={
onEmptyStateAction
? {
label: buttonLabelWithFilters,
onClick: onEmptyStateAction,
kind: 'secondary',
}
: null
}
/>
) : (
<EmptyState
icon="empty"
title={message}
body={messageBody || ''}
action={
onEmptyStateAction
? {
label: buttonLabel,
onClick: onEmptyStateAction,
}
: null
}
/>
)}
</div>
)}
</TableCell>
</TableRow>
</TableBody>
);
noDataIcon,
}) => {
const isFilterEmptyTable = (
<EmptyState
icon="no-result"
title={messageWithFilters}
body={messageWithFiltersBody || ''}
action={
onEmptyStateAction
? {
label: buttonLabelWithFilters,
onClick: onEmptyStateAction,
kind: 'secondary',
}
: null
}
/>
);

const defaultEmptyTable = (
<EmptyState
icon={noDataIcon !== '' ? noDataIcon : 'empty'}
title={message}
body={messageBody || ''}
action={
onEmptyStateAction
? {
label: buttonLabel,
onClick: onEmptyStateAction,
}
: null
}
/>
);

return (
<TableBody
id={`${id}-empty-table`}
// TODO: remove deprecated 'testID' in v3
data-testid={testID || testId}
>
<TableRow className={`${iotPrefix}--empty-table--table-row`}>
<TableCell colSpan={totalColumns}>
{React.isValidElement(emptyState) ? (
emptyState
) : (
<div className="empty-table-cell--default">
{isFiltered ? isFilterEmptyTable : defaultEmptyTable}
</div>
)}
</TableCell>
</TableRow>
</TableBody>
);
};
EmptyTable.propTypes = propTypes;
EmptyTable.defaultProps = defaultProps;

Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ const propTypes = {
hasBreadcrumbDrop: PropTypes.bool,
/** Freezes table header and footer */
pinHeaderAndFooter: PropTypes.bool,
/** display icon while Table data is empty */
noDataIcon: PropTypes.oneOfType([
PropTypes.func,
PropTypes.oneOf(['error', 'error404', 'empty', 'not-authorized', 'no-result', 'success', '']),
]),
}),

/** Size prop from Carbon to shrink row height (and header height in some instances) */
Expand Down Expand Up @@ -613,6 +618,7 @@ export const defaultProps = (baseProps) => ({
// previously used as testId.
testId: null,
enablePercentageColumnWidth: false,
noDataIcon: '',
});

const Table = (props) => {
Expand All @@ -639,6 +645,7 @@ const Table = (props) => {
testId,
size,
zIndex,
noDataIcon,
...others
} = merge({}, defaultProps(props), props);

Expand Down Expand Up @@ -1239,6 +1246,7 @@ const Table = (props) => {
id={id}
totalColumns={totalColumns}
isFiltered={isFiltered}
noDataIcon={noDataIcon}
emptyState={
// only show emptyState if no filters or search is applied
view.table.emptyState && !isFiltered
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/Table/mdx/Table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ import TableViewDropdownContent from '../TableViewDropdown/TableViewDropdownCont
| locale | string | | what locale should we use to format table values if left empty no locale formatting happens |
| i18n | object | | (see [i18n prop](#i18n-prop)) |
| error | string | | Specify the error message that need to be displayed by default. Incase we use `view.table.errorState` property then the error state element will be rendered instead of error message |
| noDataIcon | string | | While Table is empty then we have choice while icon should be display. one of (error', 'error404', 'empty', 'not-authorized', 'no-result', 'success') option so display. |
### Column Prop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ Map {
"EmptyTable" => Object {
"defaultProps": Object {
"id": "EmptyTable",
"noDataIcon": "",
"onEmptyStateAction": null,
"testId": "",
},
Expand Down Expand Up @@ -672,6 +673,30 @@ Map {
"isRequired": true,
"type": "bool",
},
"noDataIcon": Object {
"args": Array [
Array [
Object {
"type": "func",
},
Object {
"args": Array [
Array [
"error",
"error404",
"empty",
"not-authorized",
"no-result",
"success",
"",
],
],
"type": "oneOf",
},
],
],
"type": "oneOfType",
},
"onEmptyStateAction": Object {
"type": "func",
},
Expand Down Expand Up @@ -797,6 +822,7 @@ Map {
"id": null,
"lightweight": undefined,
"locale": null,
"noDataIcon": "",
"options": Object {
"hasAdvancedFilter": false,
"hasAggregations": false,
Expand Down Expand Up @@ -1725,6 +1751,30 @@ Map {
"hasUserViewManagement": Object {
"type": "bool",
},
"noDataIcon": Object {
"args": Array [
Array [
Object {
"type": "func",
},
Object {
"args": Array [
Array [
"error",
"error404",
"empty",
"not-authorized",
"no-result",
"success",
"",
],
],
"type": "oneOf",
},
],
],
"type": "oneOfType",
},
"pinColumn": Object {
"args": Array [
Array [
Expand Down

0 comments on commit 1d7c69f

Please sign in to comment.