From 9721266532fd00af6a63451d2eeaf38aebd76d95 Mon Sep 17 00:00:00 2001 From: Charles de Dreuille Date: Wed, 9 Apr 2025 10:00:30 +0200 Subject: [PATCH] Improve pagination + styles Signed-off-by: Charles de Dreuille --- packages/canon/report.api.md | 95 +++++++++++-------- .../DataTable/DataTable.stories.tsx | 16 +--- .../DataTablePagination.stories.tsx | 29 ++++-- .../Pagination/DataTablePagination.styles.css | 1 - .../Pagination/DataTablePagination.tsx | 50 +++++----- .../components/DataTable/Pagination/types.ts | 36 +------ .../DataTable/Root/DataTableRoot.stories.tsx | 1 - .../components/DataTable/mocked-columns.tsx | 2 +- packages/canon/src/components/Table/Table.tsx | 17 ++-- .../canon/src/components/Table/styles.css | 47 +++------ 10 files changed, 129 insertions(+), 165 deletions(-) diff --git a/packages/canon/report.api.md b/packages/canon/report.api.md index 0a16c2abcf..a52a83c308 100644 --- a/packages/canon/report.api.md +++ b/packages/canon/report.api.md @@ -10,15 +10,14 @@ import type { CSSProperties } from 'react'; import { FC } from 'react'; import { FocusEvent as FocusEvent_2 } from 'react'; import { ForwardRefExoticComponent } from 'react'; -import { HTMLAttributes } from 'react'; import { JSX as JSX_2 } from 'react/jsx-runtime'; import { Menu as Menu_2 } from '@base-ui-components/react/menu'; +import { default as React_2 } from 'react'; import { ReactNode } from 'react'; import { RefAttributes } from 'react'; import type { RemixiconComponentType } from '@remixicon/react'; import { ScrollArea as ScrollArea_2 } from '@base-ui-components/react/scroll-area'; -import { TdHTMLAttributes } from 'react'; -import { ThHTMLAttributes } from 'react'; +import { Table as Table_2 } from '@tanstack/react-table'; import { Tooltip as Tooltip_2 } from '@base-ui-components/react/tooltip'; import type { useRender } from '@base-ui-components/react/use-render'; @@ -219,6 +218,28 @@ export interface ContainerProps { style?: React.CSSProperties; } +// @public +export const DataTable: { + Root: ForwardRefExoticComponent< + DataTableRootProps & RefAttributes + >; + Pagination: ( + props: DataTablePaginationProps & { + ref?: React.ForwardedRef; + }, + ) => React.ReactElement; +}; + +// @public (undocumented) +export interface DataTablePaginationProps + extends React.HTMLAttributes { + table?: Table_2; +} + +// @public (undocumented) +export interface DataTableRootProps + extends React.HTMLAttributes {} + // @public (undocumented) export type Display = 'none' | 'flex' | 'block' | 'inline'; @@ -1028,43 +1049,37 @@ export type StylingPropDef = { parseValue?: (value: string) => string | undefined; }; -// @public (undocumented) -export const Table: ForwardRefExoticComponent< - HTMLAttributes & RefAttributes ->; - -// @public (undocumented) -export const TableBody: ForwardRefExoticComponent< - HTMLAttributes & - RefAttributes ->; - -// @public (undocumented) -export const TableCell: ForwardRefExoticComponent< - TdHTMLAttributes & RefAttributes ->; - -// @public (undocumented) -export const TableFooter: ForwardRefExoticComponent< - HTMLAttributes & - RefAttributes ->; - -// @public (undocumented) -export const TableHead: ForwardRefExoticComponent< - ThHTMLAttributes & RefAttributes ->; - -// @public (undocumented) -export const TableHeader: ForwardRefExoticComponent< - HTMLAttributes & - RefAttributes ->; - -// @public (undocumented) -export const TableRow: ForwardRefExoticComponent< - HTMLAttributes & RefAttributes ->; +// @public +export const Table: { + Root: React_2.ForwardRefExoticComponent< + React_2.HTMLAttributes & + React_2.RefAttributes + >; + Header: React_2.ForwardRefExoticComponent< + React_2.HTMLAttributes & + React_2.RefAttributes + >; + Body: React_2.ForwardRefExoticComponent< + React_2.HTMLAttributes & + React_2.RefAttributes + >; + Head: React_2.ForwardRefExoticComponent< + React_2.ThHTMLAttributes & + React_2.RefAttributes + >; + Row: React_2.ForwardRefExoticComponent< + React_2.HTMLAttributes & + React_2.RefAttributes + >; + Cell: React_2.ForwardRefExoticComponent< + React_2.TdHTMLAttributes & + React_2.RefAttributes + >; + Caption: React_2.ForwardRefExoticComponent< + React_2.HTMLAttributes & + React_2.RefAttributes + >; +}; // @public (undocumented) const Text_2: ForwardRefExoticComponent< diff --git a/packages/canon/src/components/DataTable/DataTable.stories.tsx b/packages/canon/src/components/DataTable/DataTable.stories.tsx index 46bc81d08c..5a7c5dbfbc 100644 --- a/packages/canon/src/components/DataTable/DataTable.stories.tsx +++ b/packages/canon/src/components/DataTable/DataTable.stories.tsx @@ -14,11 +14,10 @@ * limitations under the License. */ -import React from 'react'; import type { Meta, StoryObj } from '@storybook/react'; import { Table } from '../Table'; import { DataTable } from '.'; -import { components } from './mocked-components'; +import { components, Component } from './mocked-components'; import { columns } from './mocked-columns'; import { flexRender, @@ -38,7 +37,7 @@ type Story = StoryObj; export const Default: Story = { render: () => { - const table = useReactTable({ + const table = useReactTable({ data: components, columns, getCoreRowModel: getCoreRowModel(), @@ -97,16 +96,7 @@ export const Default: Story = { )} - table.previousPage()} - onClickNext={() => table.nextPage()} - canPrevious={table.getCanPreviousPage()} - canNext={table.getCanNextPage()} - setPageSize={pageSize => table.setPageSize(pageSize)} - /> + ); }, diff --git a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.stories.tsx b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.stories.tsx index 54563cd98a..ce3c8288a7 100644 --- a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.stories.tsx +++ b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.stories.tsx @@ -16,6 +16,15 @@ import type { Meta, StoryObj } from '@storybook/react'; import { DataTablePagination } from './DataTablePagination'; +import { + getCoreRowModel, + getFilteredRowModel, + getPaginationRowModel, + getSortedRowModel, + useReactTable, +} from '@tanstack/react-table'; +import { components, Component } from '../mocked-components'; +import { columns } from '../mocked-columns'; const meta = { title: 'Components/DataTable/Pagination', @@ -26,14 +35,16 @@ export default meta; type Story = StoryObj; export const Default: Story = { - args: { - pageIndex: 0, - pageSize: 10, - totalRows: 100, - onClickPrevious: () => {}, - onClickNext: () => {}, - canPrevious: true, - canNext: true, - setPageSize: () => {}, + render: () => { + const table = useReactTable({ + data: components, + columns, + getCoreRowModel: getCoreRowModel(), + getPaginationRowModel: getPaginationRowModel(), + getSortedRowModel: getSortedRowModel(), + getFilteredRowModel: getFilteredRowModel(), + }); + + return ; }, }; diff --git a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.styles.css b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.styles.css index 460a6a5874..0117552401 100644 --- a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.styles.css +++ b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.styles.css @@ -4,7 +4,6 @@ justify-content: space-between; padding-top: var(--canon-space-3); border-top: 1px solid var(--canon-border); - margin-top: var(--canon-space-3); } .canon-TablePagination--left { diff --git a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.tsx b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.tsx index 3a80ba3270..51144aa690 100644 --- a/packages/canon/src/components/DataTable/Pagination/DataTablePagination.tsx +++ b/packages/canon/src/components/DataTable/Pagination/DataTablePagination.tsx @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import * as React from 'react'; + +import { forwardRef } from 'react'; import { Text } from '../../Text'; import { DataTablePaginationProps } from './types'; import { IconButton } from '../../IconButton'; @@ -21,25 +22,19 @@ import clsx from 'clsx'; import { Select } from '../../Select'; /** @public */ -const DataTablePagination = React.forwardRef< - HTMLDivElement, - DataTablePaginationProps ->(({ className, ...props }, ref) => { - const { - pageIndex, - pageSize, - onClickPrevious, - onClickNext, - canPrevious, - canNext, - totalRows, - setPageSize, - } = props; +function DataTablePagination( + props: DataTablePaginationProps, + ref: React.ForwardedRef, +) { + const { className, table, ...rest } = props; + const pageIndex = table?.getState().pagination.pageIndex; + const pageSize = table?.getState().pagination.pageSize; + return (