-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #445 from EdwardEB/v2
Add MRT_TableBodyEmptyRow component to render when table has no rows
- Loading branch information
Showing
9 changed files
with
295 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/mantine-react-table/src/components/body/MRT_TableBodyEmptyRow.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import clsx from 'clsx'; | ||
import classes from './MRT_TableBody.module.css'; | ||
import { useMemo } from 'react'; | ||
import { type TableProps, Text, TableTd, TableTrProps } from '@mantine/core'; | ||
import { MRT_TableBodyRow } from './MRT_TableBodyRow'; | ||
import { | ||
type MRT_Row, | ||
type MRT_RowData, | ||
type MRT_TableInstance, | ||
} from '../../types'; | ||
import { createRow } from '@tanstack/react-table'; | ||
import { MRT_ExpandButton } from '../buttons/MRT_ExpandButton'; | ||
|
||
interface Props<TData extends MRT_RowData> extends TableTrProps { | ||
table: MRT_TableInstance<TData>; | ||
tableProps: Partial<TableProps>; | ||
} | ||
|
||
export const MRT_TableBodyEmptyRow = <TData extends MRT_RowData>({ | ||
table, | ||
tableProps, | ||
...commonRowProps | ||
}: Props<TData>) => { | ||
const { | ||
getState, | ||
options: { | ||
layoutMode, | ||
localization, | ||
renderDetailPanel, | ||
renderEmptyRowsFallback, | ||
}, | ||
refs: { tablePaperRef }, | ||
} = table; | ||
const { columnFilters, globalFilter } = getState(); | ||
|
||
const emptyRow = useMemo( | ||
() => | ||
createRow( | ||
table as any, | ||
'mrt-row-empty', | ||
{} as TData, | ||
0, | ||
0, | ||
) as MRT_Row<TData>, | ||
[], | ||
); | ||
|
||
const emptyRowProps = { | ||
...commonRowProps, | ||
renderedRowIndex: 0, | ||
row: emptyRow, | ||
virtualRow: undefined, | ||
}; | ||
|
||
return ( | ||
<MRT_TableBodyRow | ||
table={table} | ||
tableProps={tableProps} | ||
className={clsx( | ||
'mrt-table-body-row', | ||
layoutMode?.startsWith('grid') && classes['empty-row-tr-grid'], | ||
)} | ||
{...emptyRowProps} | ||
> | ||
{renderDetailPanel && ( | ||
<TableTd | ||
className={clsx( | ||
'mrt-table-body-cell', | ||
layoutMode?.startsWith('grid') && classes['empty-row-td-grid'], | ||
)} | ||
colSpan={1} | ||
> | ||
<MRT_ExpandButton row={emptyRow} table={table} /> | ||
</TableTd> | ||
)} | ||
<td | ||
className={clsx( | ||
'mrt-table-body-cell', | ||
layoutMode?.startsWith('grid') && classes['empty-row-td-grid'], | ||
)} | ||
colSpan={table.getVisibleLeafColumns().length} | ||
> | ||
{renderEmptyRowsFallback?.({ table }) ?? ( | ||
<Text | ||
__vars={{ | ||
'--mrt-paper-width': `${tablePaperRef.current?.clientWidth}`, | ||
}} | ||
className={clsx(classes['empty-row-td-content'])} | ||
> | ||
{globalFilter || columnFilters.length | ||
? localization.noResultsFound | ||
: localization.noRecordsToDisplay} | ||
</Text> | ||
)} | ||
</td> | ||
</MRT_TableBodyRow> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.