Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BaseTable): custom empty content support added #31

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 63 additions & 29 deletions src/components/BaseTable/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface BaseTableProps<TData, TScrollElement extends Element | Window =
bodyClassName?: string;
cellClassName?: BaseRowProps<TData>['cellClassName'];
className?: string;
emptyContent?: React.ReactNode | (() => React.ReactNode);
enableNesting?: boolean;
footerCellClassName?: string;
footerClassName?: string;
Expand Down Expand Up @@ -62,6 +63,7 @@ export const BaseTable = React.forwardRef(
bodyClassName,
cellClassName,
className,
emptyContent,
enableNesting,
footerCellClassName,
footerClassName,
Expand Down Expand Up @@ -116,6 +118,66 @@ export const BaseTable = React.forwardRef(
const footerRowCount = footerGroups ? footerGroups.length : 0;
const rowCount = bodyRowCount + headerRowCount + footerRowCount;

const renderBodyRows = React.useCallback(() => {
beliarh marked this conversation as resolved.
Show resolved Hide resolved
const bodyRows = rowVirtualizer?.getVirtualItems() || rows;

if (bodyRows.length === 0) {
return (
<tr>
beliarh marked this conversation as resolved.
Show resolved Hide resolved
<td colSpan={colCount}>
beliarh marked this conversation as resolved.
Show resolved Hide resolved
{typeof emptyContent === 'function' ? emptyContent() : emptyContent}
</td>
</tr>
);
}

return bodyRows.map((virtualItemOrRow) => {
const row = rowVirtualizer
? rows[virtualItemOrRow.index]
: (virtualItemOrRow as RowType<TData>);

const rowProps: BaseRowProps<TData, TScrollElement> = {
cellClassName,
className: rowClassName,
getGroupTitle,
getIsGroupHeaderRow,
attributes: rowAttributes,
cellAttributes,
onClick: onRowClick,
renderGroupHeader,
renderGroupHeaderRowContent,
row,
rowVirtualizer,
virtualItem: rowVirtualizer
? (virtualItemOrRow as VirtualItem<HTMLTableRowElement>)
: undefined,
'aria-rowindex': headerRowCount + row.index + 1,
};

if (draggableContext) {
return <BaseDraggableRow key={row.id} {...rowProps} />;
}

return <BaseRow key={row.id} {...rowProps} />;
});
}, [
cellAttributes,
cellClassName,
colCount,
draggableContext,
emptyContent,
getGroupTitle,
getIsGroupHeaderRow,
headerRowCount,
onRowClick,
renderGroupHeader,
renderGroupHeaderRowContent,
rowAttributes,
rowClassName,
rowVirtualizer,
rows,
]);

return (
<TableContextProvider getRowByIndex={getRowByIndex} enableNesting={enableNesting}>
<table
Expand Down Expand Up @@ -156,35 +218,7 @@ export const BaseTable = React.forwardRef(
}}
{...bodyAttributes}
>
{(rowVirtualizer?.getVirtualItems() || rows).map((virtualItemOrRow) => {
const row = rowVirtualizer
? rows[virtualItemOrRow.index]
: (virtualItemOrRow as RowType<TData>);

const rowProps: BaseRowProps<TData, TScrollElement> = {
cellClassName,
className: rowClassName,
getGroupTitle,
getIsGroupHeaderRow,
attributes: rowAttributes,
cellAttributes,
onClick: onRowClick,
renderGroupHeader,
renderGroupHeaderRowContent,
row,
rowVirtualizer,
virtualItem: rowVirtualizer
? (virtualItemOrRow as VirtualItem<HTMLTableRowElement>)
: undefined,
'aria-rowindex': headerRowCount + row.index + 1,
};

if (draggableContext) {
return <BaseDraggableRow key={row.id} {...rowProps} />;
}

return <BaseRow key={row.id} {...rowProps} />;
})}
{renderBodyRows()}
</tbody>
{footerGroups && (
<tfoot className={b('footer', {sticky: stickyFooter}, footerClassName)}>
Expand Down
4 changes: 4 additions & 0 deletions src/components/__stories__/BaseTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ColumnPinningDemo} from './ColumnPinningDemo';
import {ColumnPinningWithReorderingDemo} from './ColumnPinningWithReorderingDemo';
import {ColumnPinningWithSelectionDemo} from './ColumnPinningWithSelectionDemo';
import {DefaultDemo} from './DefaultDemo';
import {EmptyContentDemo} from './EmptyContent';
import {GroupingDemo} from './GroupingDemo';
import {GroupingDemo2} from './GroupingDemo2';
import {GroupingWithSelectionDemo} from './GroupingWithSelectionDemo';
Expand Down Expand Up @@ -89,3 +90,6 @@ export const ColumnPinningWithSelection: StoryFn = ColumnPinningWithSelectionTem

const StickyHeaderTamplate: StoryFn = () => <StickyHeaderDemo />;
export const StickyHeader: StoryFn = StickyHeaderTamplate.bind({});

const EmptyContentTamplate: StoryFn = () => <EmptyContentDemo />;
export const EmptyContent: StoryFn = EmptyContentTamplate.bind({});
3 changes: 3 additions & 0 deletions src/components/__stories__/EmptyContent.classname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {cn} from '../../utils';

export const cnEmptyContentDemo = cn('empty-content-demo');
11 changes: 11 additions & 0 deletions src/components/__stories__/EmptyContent.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.empty-content-demo {
&__placeholder {
min-height: 20px;

display: flex;
align-items: center;
justify-content: center;

color: #ff0000;
}
}
20 changes: 20 additions & 0 deletions src/components/__stories__/EmptyContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

import {useTable} from '../../hooks';
import {BaseTable} from '../BaseTable';

import {cnEmptyContentDemo} from './EmptyContent.classname';
import {columns} from './constants/columns';

import './EmptyContent.scss';

const emptyContentPlaceholder = <div className={cnEmptyContentDemo('placeholder')}>No data</div>;

export const EmptyContentDemo = () => {
const table = useTable({
columns,
data: [],
});

return <BaseTable table={table} emptyContent={emptyContentPlaceholder} />;
};
Loading