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: INITIATE-3491 Data table Design fixes #561

Merged
merged 4 commits into from
Oct 16, 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
6 changes: 6 additions & 0 deletions .changeset/violet-impalas-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@project44-manifest/react': patch
'storybook-manifest': patch
---

Added new prop autoResetExpanded, Added new prop getSubRows, Added class for child rows.
11 changes: 10 additions & 1 deletion packages/react/src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export function DataTable<TData extends RowData>(props: DataTableProps<TData>) {
onRowSelectionChange,
onSortingChange,
onScroll,
autoResetExpanded,
getSubRows = (row: Row<TData>) => row?.subRows as TData[],
...other
} = props;

Expand Down Expand Up @@ -138,7 +140,7 @@ export function DataTable<TData extends RowData>(props: DataTableProps<TData>) {
getExpandedRowModel: getExpandedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(),
getSubRows: (row: Row<TData>) => row?.subRows as TData[],
getSubRows,
initialState,
isLoading,
manualExpanding,
Expand All @@ -154,6 +156,13 @@ export function DataTable<TData extends RowData>(props: DataTableProps<TData>) {
...(manualPagination && { onPaginationChange }),
...(manualSorting && { onSortingChange }),
} as TableOptions<TData>) as DataTable<TData>;

React.useEffect(() => {
if (autoResetExpanded) {
table.resetExpanded();
}
}, [data, autoResetExpanded, table]);

return (
<StyledDataTable className="manifest-data-table" {...other}>
<div
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/components/DataTable/DataTable.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export interface DataTableProps<TData extends RowData> {
* Whether the sorting state for the table is controlled.
*/
manualSorting?: boolean;
/**
* Enable this setting to automatically reset the expanded state of the table when expanding state changes.
*/
autoResetExpanded?: boolean;
/**
* A total pageCount of the table (controlled).
*/
Expand Down Expand Up @@ -223,6 +227,11 @@ export interface DataTableProps<TData extends RowData> {
* Handler that is invoked on scroll event on table container
*/
onScroll?: (event: React.SyntheticEvent) => void;
/**
* Optional function used to access the sub rows for the table .
* @default * (row: Row<TData>) => row?.subRows as TData[]
*/
getSubRows?: (Row: Row<TData>) => TData[];
}

export interface FooterPropsType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export function DataTableBody<TData extends RowData>(props: DataTableBodyProps<T
return (
<tbody className="manifest-table__body">
{table.getRowModel().rows.map((row) => (
<tr key={row.id} className="manifest-table__row">
<tr
key={row.id}
className={`manifest-table__row ${row.depth > 0 ? 'manifest-table__child-row' : ''}`}
>
{row.getVisibleCells().map((cell) => (
<DataTableCell key={cell.id} cell={cell} table={table} />
))}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react';
import React, { useState } from 'react';
import { faker } from '@faker-js/faker';
import { ClipboardWithCheck, Clock } from '@project44-manifest/react-icons';
import { RowSelectionState } from '@tanstack/react-table';
Expand Down Expand Up @@ -358,6 +358,80 @@ export const RowExpanding = () => {
return <DataTable enableExpanding columns={columns} data={data} />;
};

export const RowExpandingReset = () => {
interface Person {
firstName: string;
lastName: string;
age: number;
address: string;
phoneNumber: string;
}
const columns: DataTableColumnDef<Person>[] = [
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Age',
accessorKey: 'age',
},
{
header: 'Address',
accessorKey: 'address',
},
{
header: 'Phone Number',
accessorKey: 'phoneNumber',
},
];
const [data, setData] = useState(
[...Array.from({ length: 5 })].map(() => ({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
age: faker.datatype.number(80),
address: faker.address.streetAddress(),
phoneNumber: faker.phone.number(),
subRows: [...Array.from({ length: faker.datatype.number(4) })].map(() => ({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
age: faker.datatype.number(80),
address: faker.address.streetAddress(),
phoneNumber: faker.phone.number(),
})),
})),
);

const resetData = () => {
setData(
[...Array.from({ length: 5 })].map(() => ({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
age: faker.datatype.number(80),
address: faker.address.streetAddress(),
phoneNumber: faker.phone.number(),
subRows: [...Array.from({ length: faker.datatype.number(4) })].map(() => ({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
age: faker.datatype.number(80),
address: faker.address.streetAddress(),
phoneNumber: faker.phone.number(),
})),
})),
);
};

return (
<>
<button onClick={resetData}> reset </button>
<DataTable autoResetExpanded enableExpanding columns={columns} data={data} />
</>
);
};

export const RowExpandingAndSelection = () => {
interface Person {
firstName: string;
Expand Down
Loading