Skip to content

Commit cf1de4a

Browse files
authored
Merge pull request #43 from taro-28/support-default-value-option
Support default value option
2 parents 9df1bd6 + 56babf0 commit cf1de4a

36 files changed

+1819
-881
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ const table = useReactTable({
124124

125125
## Advanced
126126

127+
- [Custom query param name](#custom-query-param-name)
128+
- [Custom encoder/decoder](#custom-encoderdecoder)
129+
- [Custom default value](#custom-default-value)
130+
127131
### Custom query param name
128132

129133
Query parameter names can be customized.
@@ -199,6 +203,38 @@ const stateAndOnChanges = useTableSearchParams(router, {
199203
});
200204
```
201205

206+
### Custom default value
207+
208+
Default values can be customized.
209+
210+
"default value" means the value that is used as value of `state` when the query parameter is not present.
211+
212+
- [demo](https://tanstack-table-search-paramsexample-git-56132d-taro28s-projects.vercel.app/custom-default-value)
213+
- [code](https://github.com/taro-28/tanstack-table-search-params/tree/main/examples/next-pages-router/src/pages/custom-default-value.tsx)
214+
215+
```tsx
216+
const stateAndOnChanges = useTableSearchParams(router, {
217+
sorting: {
218+
// Sort by name in descending order when query parameter is not present
219+
defaultValue: [{ id: "name", desc: true }],
220+
},
221+
});
222+
```
223+
224+
If you want to set initial values for query parameters, either transition with the query parameter or add the query parameter after the transition, depending on the router you are using.
225+
226+
```tsx
227+
// Transition with the query parameter
228+
<Link href={{ pathname: "/users", query: { globalFilter: "foo" } }}>
229+
Users
230+
</Link>;
231+
232+
// Add the query parameter after the transition
233+
useEffect(() => {
234+
router.replace({ query: { globalFilter: "foo" } });
235+
}, [router.replace]);
236+
```
237+
202238
## Supported
203239

204240
List of supported TanStack table states
@@ -219,7 +255,6 @@ List of supported TanStack table states
219255

220256
## TODO
221257

222-
- [ ] initial state
223258
- [ ] disable specific state
224259

225260
# License
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Suspense } from "react";
2+
import { Table } from "./table";
3+
4+
export default function Page() {
5+
return (
6+
<main className="space-y-2 mx-6">
7+
<h1 className="text-lg font-semibold">Custom default value</h1>
8+
<Suspense>
9+
<Table />
10+
</Suspense>
11+
</main>
12+
);
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use client";
2+
3+
import { UserTable } from "@examples/lib/src/components/UserTable";
4+
import {
5+
useUserData,
6+
userColumns,
7+
} from "@examples/lib/src/components/userData";
8+
import {
9+
getCoreRowModel,
10+
getFilteredRowModel,
11+
getPaginationRowModel,
12+
getSortedRowModel,
13+
useReactTable,
14+
} from "@tanstack/react-table";
15+
import { useRouter, useSearchParams } from "next/navigation";
16+
import { useTableSearchParams } from "tanstack-table-search-params";
17+
18+
export const Table = () => {
19+
const data = useUserData();
20+
21+
const push = useRouter().push;
22+
const query = useSearchParams();
23+
24+
const router = { push, query };
25+
26+
const stateAndOnChanges = useTableSearchParams(router, {
27+
globalFilter: { defaultValue: "a" },
28+
sorting: { defaultValue: [{ id: "name", desc: true }] },
29+
pagination: { defaultValue: { pageIndex: 2, pageSize: 20 } },
30+
columnFilters: { defaultValue: [{ id: "name", value: "b" }] },
31+
});
32+
33+
const table = useReactTable({
34+
data,
35+
columns: userColumns,
36+
getCoreRowModel: getCoreRowModel(),
37+
getSortedRowModel: getSortedRowModel(),
38+
getFilteredRowModel: getFilteredRowModel(),
39+
getPaginationRowModel: getPaginationRowModel(),
40+
...stateAndOnChanges,
41+
});
42+
return <UserTable table={table} />;
43+
};

examples/next-app-router/src/app/layout.tsx

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import Link from "next/link";
22
import type { ReactNode } from "react";
33
import "./globals.css";
44

5+
type Menu = {
6+
title: string;
7+
href: string;
8+
};
9+
10+
const menus = [
11+
{
12+
title: "Basic",
13+
href: "/",
14+
},
15+
{
16+
title: "Custom query param name",
17+
href: "/custom-param-name",
18+
},
19+
{
20+
title: "Custom encoder/decoder",
21+
href: "/custom-encoder-decoder",
22+
},
23+
{
24+
title: "Custom default value",
25+
href: "/custom-default-value",
26+
},
27+
] as const satisfies Menu[];
28+
529
export default function RootLayout({
630
children,
731
}: Readonly<{
@@ -16,27 +40,13 @@ export default function RootLayout({
1640
</Link>
1741
<nav>
1842
<ul className="flex items-center">
19-
<li>
20-
<Link href="/" className="hover:text-gray-500 p-2">
21-
Basic
22-
</Link>
23-
</li>
24-
<li>
25-
<Link
26-
href="/custom-param-name"
27-
className="hover:text-gray-500 p-2"
28-
>
29-
Custom query param name
30-
</Link>
31-
</li>
32-
<li>
33-
<Link
34-
href="/custom-encoder-decoder"
35-
className="hover:text-gray-500 p-2"
36-
>
37-
Custom encoder/decoder
38-
</Link>
39-
</li>
43+
{menus.map((menu) => (
44+
<li key={menu.href}>
45+
<Link href={menu.href} className="hover:text-gray-500 p-2">
46+
{menu.title}
47+
</Link>
48+
</li>
49+
))}
4050
</ul>
4151
</nav>
4252
</header>

examples/next-pages-router/src/pages/_document.tsx

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
import { Head, Html, Main, NextScript } from "next/document";
22
import Link from "next/link";
33

4+
type Menu = {
5+
title: string;
6+
href: string;
7+
};
8+
9+
const menus = [
10+
{
11+
title: "Basic",
12+
href: "/",
13+
},
14+
{
15+
title: "Custom query param name",
16+
href: "/custom-param-name",
17+
},
18+
{
19+
title: "Custom encoder/decoder",
20+
href: "/custom-encoder-decoder",
21+
},
22+
{
23+
title: "Custom default value",
24+
href: "/custom-default-value",
25+
},
26+
] as const satisfies Menu[];
27+
428
export default function Document() {
529
return (
630
<Html lang="en">
@@ -12,27 +36,13 @@ export default function Document() {
1236
</Link>
1337
<nav>
1438
<ul className="flex items-center">
15-
<li>
16-
<Link href="/" className="hover:text-gray-500 p-2">
17-
Basic
18-
</Link>
19-
</li>
20-
<li>
21-
<Link
22-
href="/custom-param-name"
23-
className="hover:text-gray-500 p-2"
24-
>
25-
Custom query param name
26-
</Link>
27-
</li>
28-
<li>
29-
<Link
30-
href="/custom-encoder-decoder"
31-
className="hover:text-gray-500 p-2"
32-
>
33-
Custom encoder/decoder
34-
</Link>
35-
</li>
39+
{menus.map((menu) => (
40+
<li key={menu.href}>
41+
<Link href={menu.href} className="hover:text-gray-500 p-2">
42+
{menu.title}
43+
</Link>
44+
</li>
45+
))}
3646
</ul>
3747
</nav>
3848
</header>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { UserTable } from "@examples/lib/src/components/UserTable";
2+
import {
3+
useUserData,
4+
userColumns,
5+
} from "@examples/lib/src/components/userData";
6+
import {
7+
getCoreRowModel,
8+
getFilteredRowModel,
9+
getPaginationRowModel,
10+
getSortedRowModel,
11+
useReactTable,
12+
} from "@tanstack/react-table";
13+
import { useRouter } from "next/router";
14+
import { useTableSearchParams } from "tanstack-table-search-params";
15+
16+
export default function CustomParamNames() {
17+
const data = useUserData();
18+
19+
const router = useRouter();
20+
const stateAndOnChanges = useTableSearchParams(router, {
21+
globalFilter: { defaultValue: "a" },
22+
sorting: { defaultValue: [{ id: "name", desc: true }] },
23+
pagination: { defaultValue: { pageIndex: 2, pageSize: 20 } },
24+
columnFilters: { defaultValue: [{ id: "name", value: "b" }] },
25+
});
26+
27+
const table = useReactTable({
28+
data,
29+
columns: userColumns,
30+
getCoreRowModel: getCoreRowModel(),
31+
getSortedRowModel: getSortedRowModel(),
32+
getFilteredRowModel: getFilteredRowModel(),
33+
getPaginationRowModel: getPaginationRowModel(),
34+
...stateAndOnChanges,
35+
});
36+
37+
return (
38+
<div className="space-y-2 mx-6">
39+
<h1 className="text-lg font-semibold">Custom default value</h1>
40+
<UserTable table={table} />
41+
</div>
42+
);
43+
}

examples/tanstack-router/src/routeTree.gen.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { Route as rootRoute } from './routes/__root'
1414
import { Route as CustomParamNameImport } from './routes/custom-param-name'
1515
import { Route as CustomEncoderDecoderImport } from './routes/custom-encoder-decoder'
16+
import { Route as CustomDefaultValueImport } from './routes/custom-default-value'
1617
import { Route as IndexImport } from './routes/index'
1718

1819
// Create/Update Routes
@@ -27,6 +28,11 @@ const CustomEncoderDecoderRoute = CustomEncoderDecoderImport.update({
2728
getParentRoute: () => rootRoute,
2829
} as any)
2930

31+
const CustomDefaultValueRoute = CustomDefaultValueImport.update({
32+
path: '/custom-default-value',
33+
getParentRoute: () => rootRoute,
34+
} as any)
35+
3036
const IndexRoute = IndexImport.update({
3137
path: '/',
3238
getParentRoute: () => rootRoute,
@@ -43,6 +49,13 @@ declare module '@tanstack/react-router' {
4349
preLoaderRoute: typeof IndexImport
4450
parentRoute: typeof rootRoute
4551
}
52+
'/custom-default-value': {
53+
id: '/custom-default-value'
54+
path: '/custom-default-value'
55+
fullPath: '/custom-default-value'
56+
preLoaderRoute: typeof CustomDefaultValueImport
57+
parentRoute: typeof rootRoute
58+
}
4659
'/custom-encoder-decoder': {
4760
id: '/custom-encoder-decoder'
4861
path: '/custom-encoder-decoder'
@@ -64,6 +77,7 @@ declare module '@tanstack/react-router' {
6477

6578
export const routeTree = rootRoute.addChildren({
6679
IndexRoute,
80+
CustomDefaultValueRoute,
6781
CustomEncoderDecoderRoute,
6882
CustomParamNameRoute,
6983
})
@@ -77,13 +91,17 @@ export const routeTree = rootRoute.addChildren({
7791
"filePath": "__root.tsx",
7892
"children": [
7993
"/",
94+
"/custom-default-value",
8095
"/custom-encoder-decoder",
8196
"/custom-param-name"
8297
]
8398
},
8499
"/": {
85100
"filePath": "index.tsx"
86101
},
102+
"/custom-default-value": {
103+
"filePath": "custom-default-value.tsx"
104+
},
87105
"/custom-encoder-decoder": {
88106
"filePath": "custom-encoder-decoder.tsx"
89107
},

0 commit comments

Comments
 (0)