From 77e62b9b5840e8a59018f1ac5c66f48f4b86f98f Mon Sep 17 00:00:00 2001 From: Jules Date: Tue, 30 Jul 2024 15:39:42 +0200 Subject: [PATCH] [sparkle] - feature: enhance Table component with filter state management - Integrate `useState` for `columnFilters` and `setColumnFilters` to manage column filter states - Remove `onSort` prop from `TableProps` and directly use `setSorting` in `useReactTable` for sorting logic simplification - Change Table stories to use `row.original` for cell values, ensuring consistency with new filter logic - Default the width of the first column to "normal" unless specified otherwise in `TableProps` --- sparkle/src/components/Table.tsx | 33 +++++++++++++++------------ sparkle/src/stories/Table.stories.tsx | 14 ++++++------ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/sparkle/src/components/Table.tsx b/sparkle/src/components/Table.tsx index 7a6e2bc8e3662..5d6a6d47872bf 100644 --- a/sparkle/src/components/Table.tsx +++ b/sparkle/src/components/Table.tsx @@ -1,12 +1,14 @@ import { type ColumnDef, + ColumnFiltersState, flexRender, getCoreRowModel, + getFilteredRowModel, getSortedRowModel, type SortingState, useReactTable, } from "@tanstack/react-table"; -import React, { ReactNode } from "react"; +import React, { ReactNode, useState } from "react"; import { Avatar, MoreIcon } from "@sparkle/index"; import { ArrowDownIcon, ArrowUpIcon } from "@sparkle/index"; @@ -14,35 +16,36 @@ import { classNames } from "@sparkle/lib/utils"; import { Icon } from "./Icon"; +type FirstColumnWidth = "expanded" | "normal"; + interface TableProps { data: TData[]; columns: ColumnDef[]; - onSort?: (sorting: SortingState) => void; className?: string; - width?: "expanded" | "normal"; + width?: FirstColumnWidth; } export function Table({ data, columns, - onSort, className, - width, + width = "normal", }: TableProps) { - const [sorting, setSorting] = React.useState([]); + const [sorting, setSorting] = useState([]); + const [columnFilters, setColumnFilters] = useState([]); const table = useReactTable({ data, columns, - state: { sorting }, - onSortingChange: (updater) => { - const newSorting = - typeof updater === "function" ? updater(sorting) : updater; - setSorting(newSorting); - onSort?.(newSorting); - }, + onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), + getFilteredRowModel: getFilteredRowModel(), + onColumnFiltersChange: setColumnFilters, + state: { + columnFilters, + sorting, + }, }); return ( @@ -53,7 +56,7 @@ export function Table({ {headerGroup.headers.map((header) => ( @@ -114,7 +117,7 @@ interface HeaderProps extends React.HTMLAttributes { interface HeadProps extends React.ThHTMLAttributes { children?: ReactNode; - width?: "normal" | "expanded"; + width?: FirstColumnWidth; } interface CellProps extends React.TdHTMLAttributes { diff --git a/sparkle/src/stories/Table.stories.tsx b/sparkle/src/stories/Table.stories.tsx index dcef7cc18e519..0aa0f07ef6377 100644 --- a/sparkle/src/stories/Table.stories.tsx +++ b/sparkle/src/stories/Table.stories.tsx @@ -2,9 +2,10 @@ import type { Meta, StoryObj } from "@storybook/react"; import { ColumnDef } from "@tanstack/react-table"; import React from 'react'; -import { FolderIcon, Table } from "../index_with_tw_base"; import { TableData } from "@sparkle/components/Table"; +import { FolderIcon, Table } from "../index_with_tw_base"; + const meta = { title: "Components/Table", component: Table @@ -55,7 +56,7 @@ const TableExample = () => { icon={info.row.original.icon} description={info.row.original.description} > - {info.getValue()} + {info.row.original.name} ), meta: { width: "expanded" } @@ -63,23 +64,23 @@ const TableExample = () => { { accessorKey: "usedBy", header: "Used by", - cell: info => {info.getValue()} + cell: info => {info.row.original.usedBy} }, { accessorKey: "addedBy", header: "Added by", - cell: info => {info.getValue()} + cell: info => {info.row.original.addedBy} }, { accessorKey: "lastUpdated", header: "Last updated", - cell: info => {info.getValue()}, + cell: info => {info.row.original.lastUpdated}, enableSorting: false }, { accessorKey: "size", header: "Size", - cell: info => {info.getValue()} + cell: info => {info.row.original.size} } ]; @@ -88,7 +89,6 @@ const TableExample = () => { console.log("New sorting:", sorting)} /> );