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

Generic state type parameter #9

Merged
merged 5 commits into from
Nov 12, 2023
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
6 changes: 6 additions & 0 deletions .changeset/tame-vans-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@use-search-params-state/react": patch
"@use-search-params-state/next": patch
---

Added generic type parameter to the useSearchParamsState hooks
2 changes: 1 addition & 1 deletion apps/playground/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { redirect } from "next/navigation";

export default function IndexPage() {
redirect("/default");
redirect("/basic");
}
3 changes: 3 additions & 0 deletions apps/playground/src/app/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const Sidebar = () => {
With default values
</SidebarLink>
<SidebarLink href="/with-zod">With Zod</SidebarLink>
<SidebarLink href="/with-weak-typesafety">
With weak type safety
</SidebarLink>
</ul>
</div>
</aside>
Expand Down
35 changes: 35 additions & 0 deletions apps/playground/src/app/with-weak-typesafety/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use client";

import { SetKeyValueInputs } from "@/components/set-key-value-inputs";
import { Alert } from "@/components/ui/alert";

import { useSearchParamsState } from "@use-search-params-state/next";

interface SearchParamsType {
page: string;
search?: string;
}

export default function Page() {
const [state, setState] = useSearchParamsState<SearchParamsType>({
defaultValues: {
page: "1",
},
});

return (
<div>
<Alert>
{
"Check source code for this page. It has a generic type parameter passed to useSearchParamsState and enforces usage of the state inline with the provided type."
}
</Alert>

<SetKeyValueInputs
setState={(k, v) => setState(k as keyof SearchParamsType, v)}
/>

<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
}
2 changes: 1 addition & 1 deletion apps/playground/src/app/with-zod/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function Page() {
return <div>with zod</div>;
return <div>TODO: with zod</div>;
}
18 changes: 18 additions & 0 deletions packages/next/src/example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useSearchParamsState } from "./index";

const Component = () => {
const [state, setState] = useSearchParamsState<{
greeting?: string;
hello: string;
}>();

const handleClick = () => {
state.greeting === "hello"
? setState("greeting", "world")
: setState("greeting", "hello");
};

return <button onClick={handleClick}>{state.greeting ?? "hello"}</button>;
};

<Component />;
10 changes: 8 additions & 2 deletions packages/next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useSearchParamsState as useSearchParamsStateReact } from "@use-search-params-state/react";
import type { UseSearchParamsStateOptions } from "@use-search-params-state/react";

export const useSearchParamsState = (opts?: UseSearchParamsStateOptions) => {
export const useSearchParamsState = <
State extends
| { [K in keyof State]: string }
| Record<string, string> = Record<string, string>,
>(
opts?: UseSearchParamsStateOptions<State>,
) => {
const searchParams = useSearchParams();
const router = useRouter();
const pathname = usePathname();
Expand All @@ -12,7 +18,7 @@ export const useSearchParamsState = (opts?: UseSearchParamsStateOptions) => {
router.push(pathname + "?" + newSearchParams.toString());
};

return useSearchParamsStateReact({
return useSearchParamsStateReact<State>({
searchParams,
setSearchParams,
...opts,
Expand Down
54 changes: 54 additions & 0 deletions packages/react/src/example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useState } from "react";

import { useSearchParamsState } from "./index";

const Component1 = () => {
const [searchParams, setSearchParams] = useState(new URLSearchParams());

const [state, setState] = useSearchParamsState<{
greeting?: string;
hello: string;
}>({
searchParams,
setSearchParams: (newSearchParams) => {
console.log("newSearchParams", newSearchParams);
setSearchParams(newSearchParams);
},
});

const handleClick = () => {
state.greeting === "hello"
? setState("greeting", "world")
: setState("greeting", "hello");
};

return <button onClick={handleClick}>{state.greeting ?? "hello"}</button>;
};

<Component1 />;

const Comp2 = () => {
const [searchParams, setSearchParams] = useState(new URLSearchParams());

const [state, setState] = useSearchParamsState<{
page: string;
hello: string;
}>({
defaultValues: {
hello: "world",
},
searchParams,
setSearchParams: (newSearchParams) => {
console.log("newSearchParams", newSearchParams);
setSearchParams(newSearchParams);
},
});

const handleClick = () => {
state.page === "1" ? setState("page", "2") : setState("page", "1");
};

return <button onClick={handleClick}>{state.page}</button>;
};

<Comp2 />;
Loading