Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update options interface #53

Merged
merged 3 commits into from
Aug 29, 2024
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
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ Query parameter names can be customized.

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
globalFilter: {
paramNames: {
// Customize query parameter name by passing a string
paramName: "userTable-globalFilter",
},
sorting: {
globalFilter: "userTable-globalFilter",
// Add prefix by passing a function
paramName: (defaultParamName) => `userTable-${defaultParamName}`,
sorting: (defaultParamName) => `userTable-${defaultParamName}`,
},
});
```
Expand All @@ -158,9 +156,9 @@ Default values can be customized.

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
sorting: {
defaultValues: {
// Sort by name in descending order when query parameter is not present
defaultValue: [{ id: "name", desc: true }],
sorting: [{ id: "name", desc: true }],
},
});
```
Expand Down Expand Up @@ -189,41 +187,57 @@ Encoder and decoder can be customized.
```tsx
const stateAndOnChanges = useTableSearchParams(router, {
// Use JSON.stringify/JSON.parse for encoding/decoding
globalFilter: {
encoders: {
// foo -> { "globalFilter": "foo" }
encoder: (globalFilter) => ({
globalFilter: (globalFilter) => ({
globalFilter: JSON.stringify(globalFilter),
}),
},
decoders: {
// { "globalFilter": "foo" } -> foo
decoder: (query) =>
globalFilter: (query) =>
query["globalFilter"]
? JSON.parse(query["globalFilter"] as string)
: (query["globalFilter"] ?? ""),
},
});

// ...

const stateAndOnChanges = useTableSearchParams(router, {
// Encoders/decoders with different query parameter names can also be used.
sorting: {
encoders: {
// [{ id: "name", desc: true }] -> { "userTable-sorting": "[{ \"id\": \"name\", \"desc\": true }]" }
encoder: (sorting) => ({
sorting: (sorting) => ({
"userTable-sorting": JSON.stringify(sorting),
}),
},
decoders: {
// { "userTable-sorting": "[{ \"id\": \"name\", \"desc\": true }]" } -> [{ id: "name", desc: true }]
decoder: (query) =>
sorting: (query) =>
query["userTable-sorting"]
? JSON.parse(query["userTable-sorting"] as string)
: query["userTable-sorting"],
},
});

// ...

const stateAndOnChanges = useTableSearchParams(router, {
// Encoders/decoders with different numbers of query parameters can also be used.
columnFilters: {
encoders: {
// [{ id: "name", value: "foo" }] -> { "columnFilters.name": "\"foo\"" }
encoder: (columnFilters) =>
columnFilters: (columnFilters) =>
Object.fromEntries(
columnFilters.map(({ id, value }) => [
`columnFilters.${id}`,
JSON.stringify(value),
]),
),
},
decoders: {
// { "columnFilters.name": "\"foo\"" } -> [{ id: "name", value: "foo" }]
decoder: (query) =>
columnFilters: (query) =>
Object.entries(query)
.filter(([key]) => key.startsWith("columnFilters."))
.map(([key, value]) => ({
Expand Down
10 changes: 6 additions & 4 deletions examples/next-app-router/src/app/custom-default-value/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export const Table = () => {
const stateAndOnChanges = useTableSearchParams(
{ push, query, pathname },
{
globalFilter: { defaultValue: "a" },
sorting: { defaultValue: [{ id: "name", desc: true }] },
pagination: { defaultValue: { pageIndex: 2, pageSize: 20 } },
columnFilters: { defaultValue: [{ id: "name", value: "b" }] },
defaultValues: {
globalFilter: "a",
sorting: [{ id: "name", desc: true }],
pagination: { pageIndex: 2, pageSize: 20 },
columnFilters: [{ id: "name", value: "b" }],
},
},
);

Expand Down
48 changes: 22 additions & 26 deletions examples/next-app-router/src/app/custom-encoder-decoder/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,48 @@ export const Table = () => {
const stateAndOnChanges = useTableSearchParams(
{ push, query, pathname },
{
globalFilter: {
encoder: (globalFilter) => ({
encoders: {
globalFilter: (globalFilter) => ({
globalFilter: JSON.stringify(globalFilter),
}),
decoder: (query) =>
query["globalFilter"]
? JSON.parse(query["globalFilter"] as string)
: query["globalFilter"] ?? "",
},
sorting: {
encoder: (sorting) =>
sorting: (sorting) =>
Object.fromEntries(
sorting.map(({ id, desc }) => [
`sorting.${id}`,
desc ? "desc" : "asc",
]),
),
decoder: (query) =>
pagination: (pagination) => ({
pagination: JSON.stringify(pagination),
}),
columnFilters: (columnFilters) =>
Object.fromEntries(
columnFilters.map(({ id, value }) => [
`columnFilters.${id}`,
JSON.stringify(value),
]),
),
},
decoders: {
globalFilter: (query) =>
query["globalFilter"]
? JSON.parse(query["globalFilter"] as string)
: query["globalFilter"] ?? "",
sorting: (query) =>
Object.entries(query)
.filter(([key]) => key.startsWith("sorting."))
.map(([key, desc]) => ({
id: key.replace("sorting.", ""),
desc: desc === "desc",
})),
},
pagination: {
encoder: (pagination) => ({
pagination: JSON.stringify(pagination),
}),
decoder: (query) =>
pagination: (query) =>
query["pagination"]
? JSON.parse(query["pagination"] as string)
: {
pageIndex: 0,
pageSize: 10,
},
},
columnFilters: {
encoder: (columnFilters) =>
Object.fromEntries(
columnFilters.map(({ id, value }) => [
`columnFilters.${id}`,
JSON.stringify(value),
]),
),
decoder: (query) =>
columnFilters: (query) =>
Object.entries(query)
.filter(([key]) => key.startsWith("columnFilters."))
.map(([key, value]) => ({
Expand Down
16 changes: 5 additions & 11 deletions examples/next-app-router/src/app/custom-param-name/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ export const Table = () => {
const stateAndOnChanges = useTableSearchParams(
{ push, query, pathname },
{
globalFilter: {
paramName: "userTable-globalFilter",
},
sorting: {
paramName: (defaultParamName) => `userTable-${defaultParamName}`,
},
pagination: {
paramName: {
paramNames: {
globalFilter: "userTable-globalFilter",
sorting: (defaultParamName) => `userTable-${defaultParamName}`,
pagination: {
pageIndex: "userTable-pageIndex",
pageSize: "userTable-pageSize",
},
},
columnFilters: {
paramName: (defaultParamName) => `userTable-${defaultParamName}`,
columnFilters: (defaultParamName) => `userTable-${defaultParamName}`,
},
},
);
Expand Down
10 changes: 6 additions & 4 deletions examples/next-pages-router/src/pages/custom-default-value.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ export default function CustomParamNames() {

const router = useRouter();
const stateAndOnChanges = useTableSearchParams(router, {
globalFilter: { defaultValue: "a" },
sorting: { defaultValue: [{ id: "name", desc: true }] },
pagination: { defaultValue: { pageIndex: 2, pageSize: 20 } },
columnFilters: { defaultValue: [{ id: "name", value: "b" }] },
defaultValues: {
globalFilter: "a",
sorting: [{ id: "name", desc: true }],
pagination: { pageIndex: 2, pageSize: 20 },
columnFilters: [{ id: "name", value: "b" }],
},
});

const table = useReactTable({
Expand Down
48 changes: 22 additions & 26 deletions examples/next-pages-router/src/pages/custom-encoder-decoder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,48 @@ export default function CustomEncoderDecoder() {

const router = useRouter();
const stateAndOnChanges = useTableSearchParams(router, {
globalFilter: {
encoder: (globalFilter) => ({
encoders: {
globalFilter: (globalFilter) => ({
globalFilter: JSON.stringify(globalFilter),
}),
decoder: (query) =>
query["globalFilter"]
? JSON.parse(query["globalFilter"] as string)
: query["globalFilter"] ?? "",
},
sorting: {
encoder: (sorting) =>
sorting: (sorting) =>
Object.fromEntries(
sorting.map(({ id, desc }) => [
`sorting.${id}`,
desc ? "desc" : "asc",
]),
),
decoder: (query) =>
pagination: (pagination) => ({
pagination: JSON.stringify(pagination),
}),
columnFilters: (columnFilters) =>
Object.fromEntries(
columnFilters.map(({ id, value }) => [
`columnFilters.${id}`,
JSON.stringify(value),
]),
),
},
decoders: {
globalFilter: (query) =>
query["globalFilter"]
? JSON.parse(query["globalFilter"] as string)
: query["globalFilter"] ?? "",
sorting: (query) =>
Object.entries(query)
.filter(([key]) => key.startsWith("sorting."))
.map(([key, desc]) => ({
id: key.replace("sorting.", ""),
desc: desc === "desc",
})),
},
pagination: {
encoder: (pagination) => ({
pagination: JSON.stringify(pagination),
}),
decoder: (query) =>
pagination: (query) =>
query["pagination"]
? JSON.parse(query["pagination"] as string)
: {
pageIndex: 0,
pageSize: 10,
},
},
columnFilters: {
encoder: (columnFilters) =>
Object.fromEntries(
columnFilters.map(({ id, value }) => [
`columnFilters.${id}`,
JSON.stringify(value),
]),
),
decoder: (query) =>
columnFilters: (query) =>
Object.entries(query)
.filter(([key]) => key.startsWith("columnFilters."))
.map(([key, value]) => ({
Expand Down
16 changes: 5 additions & 11 deletions examples/next-pages-router/src/pages/custom-param-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@ export default function CustomParamNames() {

const router = useRouter();
const stateAndOnChanges = useTableSearchParams(router, {
globalFilter: {
paramName: "userTable-globalFilter",
},
sorting: {
paramName: (defaultParamName) => `userTable-${defaultParamName}`,
},
pagination: {
paramName: {
paramNames: {
globalFilter: "userTable-globalFilter",
sorting: (defaultParamName) => `userTable-${defaultParamName}`,
pagination: {
pageIndex: "userTable-pageIndex",
pageSize: "userTable-pageSize",
},
},
columnFilters: {
paramName: (defaultParamName) => `userTable-${defaultParamName}`,
columnFilters: (defaultParamName) => `userTable-${defaultParamName}`,
},
});

Expand Down
10 changes: 6 additions & 4 deletions examples/tanstack-router/src/routes/custom-default-value.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ function Page() {
pathname: Route.path,
},
{
globalFilter: { defaultValue: "a" },
sorting: { defaultValue: [{ id: "name", desc: true }] },
pagination: { defaultValue: { pageIndex: 2, pageSize: 20 } },
columnFilters: { defaultValue: [{ id: "name", value: "b" }] },
defaultValues: {
globalFilter: "a",
sorting: [{ id: "name", desc: true }],
pagination: { pageIndex: 2, pageSize: 20 },
columnFilters: [{ id: "name", value: "b" }],
},
},
);

Expand Down
Loading