Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bring-shrubbery committed Mar 4, 2024
1 parent d4f43f5 commit 4c49c94
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 392 deletions.
14 changes: 11 additions & 3 deletions apps/playground/src/app/array-values/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"use client";

import { useState } from "react";
import { useSearchParams } from "next/navigation";
import { SetKeyValueArrayInputs } from "@/components/set-key-value-inputs";
import { Alert } from "@/components/ui/alert";
import { useObserveAndStore } from "@sp-hooks/next";

import { searchParamsToObject, useObserveAndStore } from "@sp-hooks/next";

export default function Page() {
const [state, setState] = useState({});
const sp = useSearchParams();

const [state, setState] = useState(searchParamsToObject(sp));

useObserveAndStore(state);

Expand All @@ -16,7 +20,11 @@ export default function Page() {
{"This demonstration shows how to use the library with arrays."}
</Alert>

<SetKeyValueArrayInputs setState={setState} />
<SetKeyValueArrayInputs
setState={(key, values) => {
setState((s) => ({ ...s, [key]: values }));
}}
/>

<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
Expand Down
7 changes: 3 additions & 4 deletions apps/playground/src/app/basic/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"use client";

import { useState } from "react";
import { useSearchParams } from "next/navigation";
import { SetKeyValueInputs } from "@/components/set-key-value-inputs";

import { useObserveAndStore, } from "@sp-hooks/next";
import { searchParamsToObject } from "@sp-hooks/react"
import { useSearchParams } from "next/navigation";
import { useState } from "react";
import { searchParamsToObject, useObserveAndStore } from "@sp-hooks/next";

export default function Page() {
const sp = useSearchParams();
Expand Down
22 changes: 16 additions & 6 deletions apps/playground/src/app/with-default-values/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
"use client";

import { useState } from "react";
import { useSearchParams } from "next/navigation";
import { SetKeyValueInputs } from "@/components/set-key-value-inputs";
import { Alert } from "@/components/ui/alert";
import { useObserveAndStore } from "@sp-hooks/next";

import { searchParamsToObject, useObserveAndStore } from "@sp-hooks/next";

const defaultValues = {
hello: "world",
};

export default function Page() {
const [state, setState] = useState<Record<string, string | string[]>>({
hello: "world",
});
const sp = useSearchParams();

const [state, setState] = useState(
searchParamsToObject(sp, { defaultValues }),
);

useObserveAndStore(state);
useObserveAndStore(state, { defaultValues });

return (
<div>
<Alert>
{'This page has a default value of "world" for the key "hello".'}
{
'This page has a default value of "world" for the key "hello". The key/value pair is removed from URL if its set to the default value.'
}
</Alert>

<SetKeyValueInputs setState={setState} />
Expand Down
5 changes: 3 additions & 2 deletions apps/playground/src/app/with-weak-typesafety/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { useState } from "react";
import { SetKeyValueInputs } from "@/components/set-key-value-inputs";
import { Alert } from "@/components/ui/alert";
import { useObserveAndStore } from "@sp-hooks/next";

interface SearchParamsType extends Record<string, string | string[]> {
import { SPHooksStateType, useObserveAndStore } from "@sp-hooks/next";

interface SearchParamsType extends SPHooksStateType {
page: string;
search: string;
testArray: string[];
Expand Down
41 changes: 14 additions & 27 deletions packages/next/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import {
useObserveAndStore as useObserveAndStoreReact,
type SupportedValues,
} from "@sp-hooks/react";
import { usePathname, useRouter } from "next/navigation";

// export const useSearchParamsState = <
// S extends Partial<Record<string, string | string[]>>,
// >(
// opts?: UseSearchParamsStateOptions<S>,
// ) => {
// const searchParams = useSearchParams();
// const router = useRouter();
// const pathname = usePathname();
import { useObserveAndStore as useObserveAndStoreReact } from "@sp-hooks/react";
import type { SPHooksStateType } from "@sp-hooks/react";

// const setSearchParams = (newSearchParams: URLSearchParams) => {
// router.push(pathname + "?" + newSearchParams.toString());
// };
export * from "@sp-hooks/react";
export type * from "@sp-hooks/react";

// return useSearchParamsStateReact<S>({
// searchParams,
// setSearchParams,
// ...opts,
// });
// };

export function useObserveAndStore<S extends Record<string, SupportedValues>>(
export function useObserveAndStore<S extends SPHooksStateType>(
state: S,
options?: { defaultValues?: Partial<S> },
) {
const router = useRouter();
const pathname = usePathname();

useObserveAndStoreReact(state, (newSearchParams) => {
router.push(pathname + "?" + newSearchParams.toString());
});
useObserveAndStoreReact(
state,
(newSearchParams) => {
router.push(pathname + "?" + newSearchParams.toString());
},
options,
);
}
Loading

0 comments on commit 4c49c94

Please sign in to comment.