eddev 2.0.0-beta.121 → 2.0.0-beta.123
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,10 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search param API object.
|
|
3
|
+
*/
|
|
1
4
|
export type SearchParamAPI<T> = {
|
|
5
|
+
/**
|
|
6
|
+
* Set an individual parameter value by key.
|
|
7
|
+
*
|
|
8
|
+
* If the value is an array, then one or more `key[]` parameters will be set. Otherwise, a single `key` parameter will be set.
|
|
9
|
+
*
|
|
10
|
+
* If the value is `null` or `undefined`, the parameter will be removed from the query string.
|
|
11
|
+
*
|
|
12
|
+
* @param key The search parameter name
|
|
13
|
+
* @param value The value to set
|
|
14
|
+
*/
|
|
2
15
|
set<K extends keyof T>(key: K, value: T[K]): void;
|
|
3
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Set multiple parameters at once.
|
|
18
|
+
*
|
|
19
|
+
* @param value
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
setAll: (value: Partial<T>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Returns a memoized setter function for a specific key.
|
|
25
|
+
*
|
|
26
|
+
* This is useful when you want to pass a setter function to a child component, without needing to pass the entire API object.
|
|
27
|
+
*
|
|
28
|
+
* For example:
|
|
29
|
+
* ```tsx
|
|
30
|
+
* <input value={params.mode} onChange={api.setter("mode")} />
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param key The search parameter name
|
|
34
|
+
* @returns (value: T) => void
|
|
35
|
+
*/
|
|
4
36
|
setter<K extends keyof T>(key: K): (value: T[K]) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Reset all parameters to their default values.
|
|
39
|
+
*/
|
|
5
40
|
reset(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Gets the current value of a parameter.
|
|
43
|
+
*
|
|
44
|
+
* In most cases, you should use the `params` object returned from `useSearchParams` instead of this method.
|
|
45
|
+
*
|
|
46
|
+
* However, this method will always return the current value of the parameter, even if it has been updated since the last render.
|
|
47
|
+
*/
|
|
6
48
|
get<K extends keyof T>(key: K): T[K];
|
|
7
49
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Provides type-safe read/write access to query string parameters, integrating with the routing system.
|
|
52
|
+
*
|
|
53
|
+
* - Call `useSearchParams` with a default value object, which defines the expected shape of the query string parameters.
|
|
54
|
+
* - Arrays of values are supported, and you should use a default value of `[]` for that key. You may want to cast the default parameter as an array of the expected type (eg. `myValue: [] as string[]`) to ensure that the parameter type is correct.
|
|
55
|
+
* - This hook returns a tuple with the current query string parameters object, and an API object with methods to interact with the query string. See {@link SearchParamAPI} for more details.
|
|
56
|
+
*
|
|
57
|
+
* For example:
|
|
58
|
+
* ```tsx
|
|
59
|
+
* const [params, api] = useSearchParams({
|
|
60
|
+
* mode: "list" as "list" | "grid",
|
|
61
|
+
* page: 1,
|
|
62
|
+
* tags: [] as string[],
|
|
63
|
+
* })
|
|
64
|
+
*
|
|
65
|
+
* api.set("mode", "grid")
|
|
66
|
+
* api.set("tags", ["tag1", "tag2"])
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* Note that if a parameter is set back to it's default value, it'll actually be _removed_ from the updated query string, keeping the URL clean when only the default values are set.
|
|
70
|
+
*
|
|
71
|
+
* ```tsx
|
|
72
|
+
* api.set("mode", "grid")
|
|
73
|
+
* // url becomes: "/?mode=grid"
|
|
74
|
+
* // params.mode === 'grid'
|
|
75
|
+
*
|
|
76
|
+
* api.set("mode", "list")
|
|
77
|
+
* // url becomes: "/"
|
|
78
|
+
* // params.mode === 'list'
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @param defaultValue - The default value object, defining the expected shape of the query string parameters.
|
|
82
|
+
* @returns A tuple of the resolve parameter object, and a {@link SearchParamAPI} object with methods to interact with the query string.
|
|
83
|
+
*/
|
|
84
|
+
export declare function useSearchParams<T extends object>(defaultValue: T): [T, SearchParamAPI<T>];
|
|
@@ -1,30 +1,78 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
2
|
import { useRoute } from "./useRoute.js";
|
|
3
3
|
import { useRouter } from "./useRouter.js";
|
|
4
|
+
import { parseQuery } from "../utils.js";
|
|
5
|
+
import { hash } from "object-code";
|
|
6
|
+
/**
|
|
7
|
+
* Provides type-safe read/write access to query string parameters, integrating with the routing system.
|
|
8
|
+
*
|
|
9
|
+
* - Call `useSearchParams` with a default value object, which defines the expected shape of the query string parameters.
|
|
10
|
+
* - Arrays of values are supported, and you should use a default value of `[]` for that key. You may want to cast the default parameter as an array of the expected type (eg. `myValue: [] as string[]`) to ensure that the parameter type is correct.
|
|
11
|
+
* - This hook returns a tuple with the current query string parameters object, and an API object with methods to interact with the query string. See {@link SearchParamAPI} for more details.
|
|
12
|
+
*
|
|
13
|
+
* For example:
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const [params, api] = useSearchParams({
|
|
16
|
+
* mode: "list" as "list" | "grid",
|
|
17
|
+
* page: 1,
|
|
18
|
+
* tags: [] as string[],
|
|
19
|
+
* })
|
|
20
|
+
*
|
|
21
|
+
* api.set("mode", "grid")
|
|
22
|
+
* api.set("tags", ["tag1", "tag2"])
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Note that if a parameter is set back to it's default value, it'll actually be _removed_ from the updated query string, keeping the URL clean when only the default values are set.
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* api.set("mode", "grid")
|
|
29
|
+
* // url becomes: "/?mode=grid"
|
|
30
|
+
* // params.mode === 'grid'
|
|
31
|
+
*
|
|
32
|
+
* api.set("mode", "list")
|
|
33
|
+
* // url becomes: "/"
|
|
34
|
+
* // params.mode === 'list'
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param defaultValue - The default value object, defining the expected shape of the query string parameters.
|
|
38
|
+
* @returns A tuple of the resolve parameter object, and a {@link SearchParamAPI} object with methods to interact with the query string.
|
|
39
|
+
*/
|
|
4
40
|
export function useSearchParams(defaultValue) {
|
|
5
41
|
const route = useRoute();
|
|
6
42
|
const replaceQuery = useRouter((r) => r.replaceQuery);
|
|
7
|
-
const
|
|
43
|
+
const merged = useMemo(() => ({
|
|
8
44
|
...defaultValue,
|
|
9
45
|
...route.query,
|
|
10
46
|
}), [route.query]);
|
|
47
|
+
const values = useRef(merged);
|
|
48
|
+
values.current = merged;
|
|
49
|
+
function get() {
|
|
50
|
+
if (env.client) {
|
|
51
|
+
return { ...defaultValue, ...parseQuery(document.location.search) };
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
11
57
|
const update = (patch) => {
|
|
12
|
-
const value = { ...
|
|
58
|
+
const value = { ...get(), ...patch };
|
|
13
59
|
for (let key in defaultValue) {
|
|
14
|
-
if (defaultValue[key] === value[key] || [null, undefined].includes(value[key])) {
|
|
60
|
+
if (hash(defaultValue[key]) === hash(value[key]) || [null, undefined].includes(value[key])) {
|
|
15
61
|
delete value[key];
|
|
16
62
|
}
|
|
17
63
|
}
|
|
64
|
+
values.current = value;
|
|
18
65
|
replaceQuery(value);
|
|
19
66
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
];
|
|
67
|
+
const api = useMemo(() => ({
|
|
68
|
+
set: (key, value) => update({ [key]: value }),
|
|
69
|
+
setAll: (value) => update({
|
|
70
|
+
...defaultValue,
|
|
71
|
+
...value,
|
|
72
|
+
}),
|
|
73
|
+
setter: (key) => (value) => update({ [key]: value }),
|
|
74
|
+
reset: () => update({}),
|
|
75
|
+
get: (key) => values.current[key],
|
|
76
|
+
}), []);
|
|
77
|
+
return [values.current, api];
|
|
30
78
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "2.0.0-beta.
|
|
1
|
+
export declare const VERSION = "2.0.0-beta.123";
|
package/dist/node/cli/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "2.0.0-beta.
|
|
1
|
+
export const VERSION = "2.0.0-beta.123";
|