2023-05-02 06:42:16 +00:00
|
|
|
import { ReactNode } from 'react';
|
|
|
|
import { Row } from '@tanstack/react-table';
|
2022-11-22 12:16:34 +00:00
|
|
|
|
|
|
|
import { TableRow } from './TableRow';
|
2023-09-04 09:33:07 +00:00
|
|
|
import { DefaultType } from './types';
|
2022-11-22 12:16:34 +00:00
|
|
|
|
2023-09-04 09:33:07 +00:00
|
|
|
interface Props<D extends DefaultType> {
|
2022-11-22 12:16:34 +00:00
|
|
|
row: Row<D>;
|
|
|
|
disableSelect?: boolean;
|
|
|
|
renderSubRow(row: Row<D>): ReactNode;
|
2023-05-02 06:42:16 +00:00
|
|
|
expandOnClick?: boolean;
|
2022-11-22 12:16:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:33:07 +00:00
|
|
|
export function ExpandableDatatableTableRow<D extends DefaultType>({
|
2022-11-22 12:16:34 +00:00
|
|
|
row,
|
|
|
|
disableSelect,
|
|
|
|
renderSubRow,
|
2023-05-02 06:42:16 +00:00
|
|
|
expandOnClick,
|
2022-11-22 12:16:34 +00:00
|
|
|
}: Props<D>) {
|
2023-05-02 06:42:16 +00:00
|
|
|
const cells = row.getVisibleCells();
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TableRow<D>
|
2023-05-02 06:42:16 +00:00
|
|
|
cells={cells}
|
|
|
|
onClick={expandOnClick ? () => row.toggleExpanded() : undefined}
|
2022-11-22 12:16:34 +00:00
|
|
|
/>
|
2023-08-13 17:09:40 +00:00
|
|
|
{row.getIsExpanded() && row.getCanExpand() && (
|
2022-11-22 12:16:34 +00:00
|
|
|
<tr>
|
|
|
|
{!disableSelect && <td />}
|
2023-05-02 06:42:16 +00:00
|
|
|
<td colSpan={disableSelect ? cells.length : cells.length - 1}>
|
2022-11-22 12:16:34 +00:00
|
|
|
{renderSubRow(row)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|