@coveord/plasma-mantine 55.4.0 → 55.5.1
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.
- package/.turbo/turbo-build.log +3 -3
- package/.turbo/turbo-test.log +51 -46
- package/dist/.tsbuildinfo +1 -1
- package/dist/cjs/components/table/index.d.ts +1 -0
- package/dist/cjs/components/table/index.d.ts.map +1 -1
- package/dist/cjs/components/table/index.js +4 -0
- package/dist/cjs/components/table/index.js.map +1 -1
- package/dist/cjs/components/table/table-predicate/TablePredicate.d.ts.map +1 -1
- package/dist/cjs/components/table/table-predicate/TablePredicate.js +4 -1
- package/dist/cjs/components/table/table-predicate/TablePredicate.js.map +1 -1
- package/dist/cjs/components/table/use-table.d.ts.map +1 -1
- package/dist/cjs/components/table/use-table.js +11 -7
- package/dist/cjs/components/table/use-table.js.map +1 -1
- package/dist/cjs/components/table/use-url-synced-state.d.ts +18 -7
- package/dist/cjs/components/table/use-url-synced-state.d.ts.map +1 -1
- package/dist/cjs/components/table/use-url-synced-state.js +81 -30
- package/dist/cjs/components/table/use-url-synced-state.js.map +1 -1
- package/dist/esm/components/table/index.d.ts +1 -0
- package/dist/esm/components/table/index.d.ts.map +1 -1
- package/dist/esm/components/table/index.js +1 -0
- package/dist/esm/components/table/index.js.map +1 -1
- package/dist/esm/components/table/table-predicate/TablePredicate.d.ts.map +1 -1
- package/dist/esm/components/table/table-predicate/TablePredicate.js +3 -0
- package/dist/esm/components/table/table-predicate/TablePredicate.js.map +1 -1
- package/dist/esm/components/table/use-table.d.ts.map +1 -1
- package/dist/esm/components/table/use-table.js +11 -7
- package/dist/esm/components/table/use-table.js.map +1 -1
- package/dist/esm/components/table/use-url-synced-state.d.ts +18 -7
- package/dist/esm/components/table/use-url-synced-state.d.ts.map +1 -1
- package/dist/esm/components/table/use-url-synced-state.js +57 -27
- package/dist/esm/components/table/use-url-synced-state.js.map +1 -1
- package/package.json +2 -2
- package/src/components/table/__tests__/use-url-synced-state.unit.spec.ts +74 -21
- package/src/components/table/index.ts +1 -0
- package/src/components/table/table-predicate/TablePredicate.tsx +1 -0
- package/src/components/table/use-table.ts +12 -7
- package/src/components/table/use-url-synced-state.ts +89 -39
|
@@ -1,35 +1,75 @@
|
|
|
1
|
-
import {useMemo, useState} from 'react';
|
|
1
|
+
import {Dispatch, SetStateAction, useMemo, useState} from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A search param entry defines the key (index 0) and value (index 1) of a search parameter.
|
|
5
|
+
*/
|
|
6
|
+
export type SearchParamEntry = [string, string | null | undefined];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Iterates over the `SearchParamEntry` values, and applies them to `target`, optionally filtering values.
|
|
10
|
+
*
|
|
11
|
+
* @param target The target to write values to, can be a Map (for the initial values) or `URLSearchParams`.
|
|
12
|
+
* @param entries The entries to apply (as returned by the serializer).
|
|
13
|
+
* @param filter Optional filter that allows to treat non-empty values as empty (e.g. to not set default values).
|
|
14
|
+
*/
|
|
15
|
+
const applyValues = (
|
|
16
|
+
target: Map<string, string> | URLSearchParams,
|
|
17
|
+
entries: Iterable<SearchParamEntry>,
|
|
18
|
+
filter: (entry: Readonly<SearchParamEntry>) => boolean,
|
|
19
|
+
): void => {
|
|
20
|
+
for (const entry of entries) {
|
|
21
|
+
if (entry[1] && filter(entry)) {
|
|
22
|
+
target.set(entry[0], entry[1]);
|
|
23
|
+
} else {
|
|
24
|
+
target.delete(entry[0]);
|
|
25
|
+
}
|
|
9
26
|
}
|
|
10
|
-
window.history.replaceState(null, '', url.toString());
|
|
11
27
|
};
|
|
12
28
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Read the **current** search params from `window.location`, with support for detecting React's HashRouter.
|
|
31
|
+
* Also returns a method that will yield the href (string) value, after any changes made on the params object.
|
|
32
|
+
*
|
|
33
|
+
* @returns The `URLSearchParams` instance, and a function that can be used to get an updated href.
|
|
34
|
+
*/
|
|
35
|
+
const getSearchParams = (): [URLSearchParams, () => string] => {
|
|
36
|
+
const href = window.location.href;
|
|
37
|
+
// Search for '#/' to detect hash router urls
|
|
38
|
+
const searchStart = href.indexOf('?', href.indexOf('#/') + 1);
|
|
39
|
+
const params = new URLSearchParams(searchStart < 0 ? '' : href.substring(searchStart));
|
|
40
|
+
return [
|
|
41
|
+
params,
|
|
42
|
+
() => {
|
|
43
|
+
let result = searchStart < 0 ? href : href.substring(0, searchStart);
|
|
44
|
+
if (params.size > 0) {
|
|
45
|
+
result = result.concat('?', params.toString());
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
},
|
|
49
|
+
];
|
|
16
50
|
};
|
|
17
51
|
|
|
18
52
|
export interface UseUrlSyncedStateOptions<T> {
|
|
19
53
|
/**
|
|
20
|
-
* The initial state
|
|
54
|
+
* The initial state to use, if there would be no search params to deserialize from.
|
|
55
|
+
* These values are also treated as defaults, and if the current state matches the initialState,
|
|
56
|
+
* no value will be written to the search params.
|
|
21
57
|
*/
|
|
22
|
-
initialState: T;
|
|
58
|
+
initialState: T extends object ? Readonly<T> : T;
|
|
23
59
|
/**
|
|
24
60
|
* The serializer function is used to determine how the state is translated to url search parameters.
|
|
25
61
|
* Called each time the state changes.
|
|
62
|
+
* Note that the serializer should always return entries for keys it controls, also if the current value is "unset" (`null` or empty).
|
|
63
|
+
* This ensures params get removed from the search when they are being unset.
|
|
64
|
+
*
|
|
26
65
|
* @param stateValue The new state value to serialize.
|
|
27
|
-
* @returns
|
|
66
|
+
* @returns An iterable of `[key, value]` to set as url search parameters.
|
|
28
67
|
* @example (filterValue) => [['filter', filterValue]] // ?filter=filterValue
|
|
29
68
|
*/
|
|
30
|
-
serializer: (stateValue: T) =>
|
|
69
|
+
serializer: (stateValue: T) => Iterable<SearchParamEntry>;
|
|
31
70
|
/**
|
|
32
71
|
* The deserializer function is used to determine how the url parameters influence the initial state.
|
|
72
|
+
* May return a partial state, values that are not deserialed are taken from the `initialState`.
|
|
33
73
|
* Called only once when initializing the state.
|
|
34
74
|
* @param params All the search parameters of the current url.
|
|
35
75
|
* @returns The initial state based on the current url.
|
|
@@ -37,34 +77,44 @@ export interface UseUrlSyncedStateOptions<T> {
|
|
|
37
77
|
*/
|
|
38
78
|
deserializer: (params: URLSearchParams) => T;
|
|
39
79
|
/**
|
|
40
|
-
* Whether the state should be synced with the url
|
|
41
|
-
* When set to false
|
|
80
|
+
* Whether the state should be synced with the url, defaults to `true`.
|
|
81
|
+
* When set to `false`, the hook behaves just like a regular `useState` hook from react.
|
|
42
82
|
*/
|
|
43
83
|
sync?: boolean;
|
|
44
84
|
}
|
|
45
85
|
|
|
46
|
-
export const useUrlSyncedState = <T>(
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
86
|
+
export const useUrlSyncedState = <T>(options: UseUrlSyncedStateOptions<T>) => {
|
|
87
|
+
const sync = options.sync !== false;
|
|
88
|
+
const initialState = useMemo(
|
|
89
|
+
() => (sync ? options.deserializer(getSearchParams()[0]) : options.initialState),
|
|
90
|
+
[options.initialState, options.sync],
|
|
91
|
+
);
|
|
92
|
+
const [state, setState] = useState<T>(initialState);
|
|
93
|
+
// Capture the initial state as a map, to compare values and not set them if they match.
|
|
94
|
+
const initialStateSerialized = useMemo(() => {
|
|
95
|
+
const v = new Map<string, string>();
|
|
96
|
+
applyValues(v, options.serializer(options.initialState), (_) => true);
|
|
97
|
+
return v;
|
|
98
|
+
}, [initialState, options.serializer]);
|
|
99
|
+
const enhancedSetState = useMemo<Dispatch<SetStateAction<T>>>(
|
|
100
|
+
() =>
|
|
101
|
+
sync
|
|
102
|
+
? (updater: SetStateAction<T>) => {
|
|
103
|
+
setState((old) => {
|
|
104
|
+
const [search, getUrl] = getSearchParams();
|
|
105
|
+
const newValue = updater instanceof Function ? updater(old) : updater;
|
|
106
|
+
applyValues(
|
|
107
|
+
search,
|
|
108
|
+
options.serializer(newValue),
|
|
109
|
+
([key, value]) => initialStateSerialized.get(key) !== value,
|
|
110
|
+
);
|
|
111
|
+
window.history.replaceState(null, '', getUrl());
|
|
112
|
+
return newValue;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
: setState,
|
|
116
|
+
[sync],
|
|
117
|
+
);
|
|
68
118
|
|
|
69
119
|
return [state, enhancedSetState] as const;
|
|
70
120
|
};
|