Skip to content
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: 23 additions & 1 deletion src/components/DataTable/__tests__/DataTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState } from 'react';
import { faker } from '@faker-js/faker';
import type { ColumnDef } from '@tanstack/table-core';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { range } from 'lodash-es';
import { noop, range } from 'lodash-es';
import { describe, expect, it } from 'vitest';

import { DataTable } from '../DataTable.tsx';
Expand Down Expand Up @@ -152,3 +152,25 @@ describe('DataTable pagination', () => {
expect(screen.getAllByTestId('data-table-row').length).toBe(1);
});
});

describe('DataTable server mode', () => {
const serverProps = {
columns,
data: data.slice(0, 10),
mode: 'server',
onPaginationChange: noop,
pageCount: 3
} as const;

it('should pin a column named by its initial state, so a server-mode table can size it', () => {
render(<DataTable {...serverProps} initialState={{ columnPinning: { right: ['amount'] } }} />);
expect(screen.getByText('Amount')).toHaveStyle({ position: 'sticky' });
expect(screen.getByText('Status')).not.toHaveStyle({ position: 'sticky' });
expect(screen.getByText('Email')).not.toHaveStyle({ position: 'sticky' });
});

it('should pin nothing when given no initial state', () => {
render(<DataTable {...serverProps} />);
expect(screen.getByText('Amount')).not.toHaveStyle({ position: 'sticky' });
});
});
23 changes: 17 additions & 6 deletions src/components/DataTable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ export type MemoizedHandle<T extends (...args: any[]) => any> = T & {
* CLIENT
*********************************************************************/

export type ClientDataTableInitialState = {
columnFilters?: ColumnFiltersState;
columnPinning?: ColumnPinningState;
sorting?: SortingState;
};
export type ClientDataTableInitialState = Simplify<
ServerDataTableInitialState & {
columnFilters?: ColumnFiltersState;
sorting?: SortingState;
}
>;

export type ClientDataTableStoreParams<T extends RowData> = Simplify<
BaseDataTableStoreParams<T> & {
Expand All @@ -117,9 +118,19 @@ export type ClientDataTableProps<T extends RowData> = Simplify<
* SERVER
*********************************************************************/

/**
* Pinning is layout rather than a view of the data: `calculateColumnSizing` honours `size` on a
* pinned column and splits what is left between the rest, so it is how a table of any mode gives
* one column the room its content needs. Filtering and sorting in server mode belong to the
* request, so they stay out.
*/
export type ServerDataTableInitialState = {
columnPinning?: ColumnPinningState;
};

export type ServerDataTableStoreParams<T extends RowData> = Simplify<
BaseDataTableStoreParams<T> & {
initialState?: never;
initialState?: ServerDataTableInitialState;
mode: 'server';
onPaginationChange: (state: PaginationState) => void;
onSortingChange?: (state: SortingState) => void;
Expand Down
6 changes: 5 additions & 1 deletion src/components/DataTable/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DataTableRowActionCell } from './DataTableRowActionCell.tsx';

import type {
BaseDataTableStoreParams,
ClientDataTableInitialState,
DataTableColumnBreakpoints,
DataTableRowAction,
DataTableStoreParams,
Expand Down Expand Up @@ -136,7 +137,10 @@ function getColumnPinningWithActions<T extends RowData>(
}

function getTanstackTableState<T>({ initialState, rowActions }: DataTableStoreParams<T>): TableState {
const { columnFilters = [], columnPinning = {}, sorting = [] } = initialState ?? {};
// Widened to the client shape, which the server one is a subset of, so the union's narrower
// member does not make `columnFilters` and `sorting` unreadable here.
const initial: ClientDataTableInitialState = initialState ?? {};
const { columnFilters = [], columnPinning = {}, sorting = [] } = initial;
const state: TableState = {
columnFilters,
columnOrder: [],
Expand Down
Loading