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!: replace getRowTrProps with render prop renderRowTr #753

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 10 additions & 14 deletions src/components/logger/FifoLoggerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import type { LogEntry } from 'fifo-logger';
import { CSSProperties, useMemo } from 'react';

import { Button } from '../button';
import {
createTableColumnHelper,
Table,
type TableRowTrPropsGetter,
} from '../table';
import { createTableColumnHelper, Table } from '../table';

import { useFifoLogger } from './useFifoLogger';

Expand Down Expand Up @@ -79,14 +75,6 @@ function useColumns(unseen: number) {
);
}

const getRowTrProps: TableRowTrPropsGetter<LogEntry> = (row) => {
return {
style: {
backgroundColor: rowBackgroundColor[row.original.levelLabel],
},
};
};

export function FifoLoggerDialog(props: FifoLoggerDialogProps) {
const logger = useFifoLogger();

Expand All @@ -113,7 +101,15 @@ export function FifoLoggerDialog(props: FifoLoggerDialogProps) {
compact
bordered
tableProps={{ style: { width: '100%' } }}
getRowTrProps={getRowTrProps}
renderRowTr={({ row, children }) => (
<tr
style={{
backgroundColor: rowBackgroundColor[row.original.levelLabel],
}}
>
{children}
</tr>
)}
/>
</DialogBody>
<DialogFooter
Expand Down
36 changes: 21 additions & 15 deletions src/components/table/table_body.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import type { Row, RowData } from '@tanstack/react-table';
import type { HTMLAttributes } from 'react';
import { Fragment } from 'react';

import { TableRow } from './table_row';
import { TableRowCell } from './table_row_cell';
import type { TableRowTrPropsGetter } from './table_utils';

export type TrPropsGetter = Omit<
HTMLAttributes<HTMLTableRowElement>,
'children'
>;
import type {
TableRowTrRenderer,
TableRowTrRendererProps,
} from './table_utils';

interface TableBodyProps<TData extends RowData> {
rows: Array<Row<TData>>;
getRowTrProps?: TableRowTrPropsGetter<TData>;
renderRowTr: TableRowTrRenderer<TData> | undefined;
}

export function TableBody<TData extends RowData>(props: TableBodyProps<TData>) {
const { rows, getRowTrProps } = props;
const { rows, renderRowTr = defaultRenderRowTr } = props;
return (
<tbody>
{rows.map((row) => (
<TableRow key={row.id} trProps={getRowTrProps?.(row)}>
{row.getVisibleCells().map((cell) => (
<TableRowCell key={cell.id} cell={cell} />
))}
</TableRow>
<Fragment key={row.id}>
{renderRowTr({
row,
children: row
.getVisibleCells()
.map((cell) => <TableRowCell key={cell.id} cell={cell} />),
})}
</Fragment>
))}
</tbody>
);
}

function defaultRenderRowTr<TData extends RowData>(
props: TableRowTrRendererProps<TData>,
) {
return <tr>{props.children}</tr>;
}
5 changes: 2 additions & 3 deletions src/components/table/table_header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Header, RowData } from '@tanstack/react-table';
import { CSSProperties } from 'react';

import { TableHeaderCell } from './table_header_cell';
import { TableRow } from './table_row';

const headerStyle: CSSProperties = {
position: 'sticky',
Expand All @@ -22,11 +21,11 @@ export function TableHeader<TData extends RowData>(
const { headers, sticky } = props;
return (
<thead style={sticky ? headerStyle : undefined}>
<TableRow>
<tr>
{headers.map((header) => (
<TableHeaderCell key={header.id} header={header} />
))}
</TableRow>
</tr>
</thead>
);
}
11 changes: 4 additions & 7 deletions src/components/table/table_root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { TableHTMLAttributes } from 'react';

import { TableBody } from './table_body';
import { TableHeader } from './table_header';
import type { TableColumnDef, TableRowTrPropsGetter } from './table_utils';
import type { TableColumnDef, TableRowTrRenderer } from './table_utils';
import { useTableColumns } from './use_table_columns';

export interface TableProps<TData extends RowData> {
Expand All @@ -31,7 +31,7 @@ export interface TableProps<TData extends RowData> {
'data' | 'columns' | 'getCoreRowModel' | 'getSortedRowModel'
>;
tableProps?: Omit<TableHTMLAttributes<HTMLTableElement>, 'children'>;
getRowTrProps?: TableRowTrPropsGetter<TData>;
renderRowTr?: TableRowTrRenderer<TData>;
}

export function Table<TData extends RowData>(props: TableProps<TData>) {
Expand All @@ -47,7 +47,7 @@ export function Table<TData extends RowData>(props: TableProps<TData>) {

reactTable,
tableProps,
getRowTrProps,
renderRowTr,
} = props;

const columnDefs = useTableColumns(columns);
Expand All @@ -68,10 +68,7 @@ export function Table<TData extends RowData>(props: TableProps<TData>) {
{...tableProps}
>
<TableHeader sticky={stickyHeader} headers={table.getFlatHeaders()} />
<TableBody
rows={table.getRowModel().rows}
getRowTrProps={getRowTrProps}
/>
<TableBody rows={table.getRowModel().rows} renderRowTr={renderRowTr} />
</HTMLTable>
);
}
14 changes: 0 additions & 14 deletions src/components/table/table_row.tsx

This file was deleted.

13 changes: 8 additions & 5 deletions src/components/table/table_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
type Row,
type RowData,
} from '@tanstack/react-table';

import type { TrProps } from './table_row';
import type { ReactNode } from 'react';

export type TableColumnDef<TData extends RowData, TValue = unknown> = ColumnDef<
TData,
Expand All @@ -16,6 +15,10 @@ export function createTableColumnHelper<TData extends RowData>() {
return createColumnHelper<TData>();
}

export type TableRowTrPropsGetter<TData extends RowData> = (
row: Row<TData>,
) => TrProps;
export interface TableRowTrRendererProps<TData extends RowData> {
row: Row<TData>;
children: ReactNode;
}
export type TableRowTrRenderer<TData extends RowData> = (
props: TableRowTrRendererProps<TData>,
) => ReactNode;
Loading