↕️ server-paged tables stop sorting a single page
Browse files at this commit0ce9d6b9cd8438d3e94d6268ee422a4b43378effParent: 9f9eebf- fix
- ↕️ server-paged tables stop sorting a single page
2 changed files +29 -2
src/components/ui/data-table.test.tsxmodified+23 -0
| @@ -170,3 +170,26 @@ test("a search that matches nothing says so and clears back to the list", () => | |||
| 170 | fireEvent.click(within(screen.getByRole("table")).getByRole("button", { name: /Clear search|Aramayı temizle/ })); | 170 | fireEvent.click(within(screen.getByRole("table")).getByRole("button", { name: /Clear search|Aramayı temizle/ })); |
| 171 | expect(screen.getByText("Ada")).toBeTruthy(); | 171 | expect(screen.getByText("Ada")).toBeTruthy(); |
| 172 | }); | 172 | }); |
| 173 | + | ||
| 174 | +test("a server-paged table offers no header sort, since it could only sort one page", () => { | ||
| 175 | + const sortable: ColumnDef<Row>[] = [{ accessorKey: "name", header: "Name" }]; | ||
| 176 | + const { unmount } = render(() => ( | ||
| 177 | + <PreferencesProvider> | ||
| 178 | + <DataTable columns={sortable} data={[{ name: "Ada" }]} enableColumnVisibility={false} /> | ||
| 179 | + </PreferencesProvider> | ||
| 180 | + )); | ||
| 181 | + expect(screen.getByRole("button", { name: /Name/ })).toBeTruthy(); | ||
| 182 | + unmount(); | ||
| 183 | + | ||
| 184 | + render(() => ( | ||
| 185 | + <PreferencesProvider> | ||
| 186 | + <DataTable | ||
| 187 | + columns={sortable} | ||
| 188 | + data={[{ name: "Ada" }]} | ||
| 189 | + enableColumnVisibility={false} | ||
| 190 | + manualPagination={{ pageIndex: 0, pageSize: 10, total: 40, onPageChange: () => {} }} | ||
| 191 | + /> | ||
| 192 | + </PreferencesProvider> | ||
| 193 | + )); | ||
| 194 | + expect(screen.queryByRole("button", { name: /^Name/ })).toBeNull(); | ||
| 195 | +}); | ||
src/components/ui/data-table.tsxmodified+6 -2
| @@ -95,6 +95,10 @@ export function DataTable<TData, TValue = unknown>(props: DataTableProps<TData, | |||
| 95 | }); | 95 | }); |
| 96 | const [search, setSearch] = createSignal(""); | 96 | const [search, setSearch] = createSignal(""); |
| 97 | const searchValue = () => props.searchValue ?? search(); | 97 | const searchValue = () => props.searchValue ?? search(); |
| 98 | + // Sorting is client-side only, and the backend takes no sort parameter: on | ||
| 99 | + // a server-paged table it would reorder the visible page alone while the | ||
| 100 | + // header claimed the whole list. Off there unless a caller opts in. | ||
| 101 | + const sortingEnabled = () => props.enableSorting ?? !props.manualPagination; | ||
| 98 | const tableData = () => { | 102 | const tableData = () => { |
| 99 | const query = searchValue().trim(); | 103 | const query = searchValue().trim(); |
| 100 | if (!query || !props.searchPredicate) return props.data; | 104 | if (!query || !props.searchPredicate) return props.data; |
| @@ -107,7 +111,7 @@ export function DataTable<TData, TValue = unknown>(props: DataTableProps<TData, | |||
| 107 | get columns() { | 111 | get columns() { |
| 108 | return props.columns; | 112 | return props.columns; |
| 109 | }, | 113 | }, |
| 110 | - enableSorting: props.enableSorting ?? true, | 114 | + enableSorting: sortingEnabled(), |
| 111 | getCoreRowModel: getCoreRowModel(), | 115 | getCoreRowModel: getCoreRowModel(), |
| 112 | getSortedRowModel: getSortedRowModel(), | 116 | getSortedRowModel: getSortedRowModel(), |
| 113 | getFilteredRowModel: getFilteredRowModel(), | 117 | getFilteredRowModel: getFilteredRowModel(), |
| @@ -218,7 +222,7 @@ export function DataTable<TData, TValue = unknown>(props: DataTableProps<TData, | |||
| 218 | const renderHeader = (header: ReturnType<typeof table.getHeaderGroups>[number]["headers"][number]) => { | 222 | const renderHeader = (header: ReturnType<typeof table.getHeaderGroups>[number]["headers"][number]) => { |
| 219 | const content = flexRender(header.column.columnDef.header, header.getContext()); | 223 | const content = flexRender(header.column.columnDef.header, header.getContext()); |
| 220 | const align = alignOf(header.column); | 224 | const align = alignOf(header.column); |
| 221 | - if (!(props.enableSorting ?? true) || !header.column.getCanSort()) { | 225 | + if (!sortingEnabled() || !header.column.getCanSort()) { |
| 222 | // The th already carries alignClass; the block span makes text-align resolve. | 226 | // The th already carries alignClass; the block span makes text-align resolve. |
| 223 | return <span class="block truncate whitespace-nowrap">{content}</span>; | 227 | return <span class="block truncate whitespace-nowrap">{content}</span>; |
| 224 | } | 228 | } |