@xeplr/ui-utils 1.1.0 → 1.1.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/package.json +1 -1
- package/src/url/urlState.js +25 -0
- package/src/url/useUrlState.js +8 -10
package/package.json
CHANGED
package/src/url/urlState.js
CHANGED
|
@@ -31,3 +31,28 @@ export function readSearch(params, key, fallback) {
|
|
|
31
31
|
const v = params.get(key)
|
|
32
32
|
return v === null || v === '' ? (fallback === undefined ? null : fallback) : v
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
// TWO WRITES IN ONE CLICK MUST BOTH LAND. react-router builds each
|
|
36
|
+
// setSearchParams from the query the last render saw, so a second write in
|
|
37
|
+
// the same handler — from this hook or another one on the page — would start
|
|
38
|
+
// from the old query and undo the first. Each write is chained onto the one
|
|
39
|
+
// before it while the page has not re-rendered: `pending` is the query last
|
|
40
|
+
// written, `pendingFrom` the rendered query it was built on. Once the URL
|
|
41
|
+
// has moved on, they no longer match and are ignored.
|
|
42
|
+
let pending = null
|
|
43
|
+
let pendingFrom = null
|
|
44
|
+
|
|
45
|
+
export function writeFrom(rendered, patch, defaults) {
|
|
46
|
+
const seen = rendered.toString()
|
|
47
|
+
const base = pending !== null && pendingFrom === seen ? pending : rendered
|
|
48
|
+
const next = patchSearch(base, patch, defaults)
|
|
49
|
+
pending = next
|
|
50
|
+
pendingFrom = seen
|
|
51
|
+
return next
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The query a write in this click should read from (see writeFrom). */
|
|
55
|
+
export function pendingBase(rendered) {
|
|
56
|
+
return pending !== null && pendingFrom === rendered.toString() ? pending : rendered
|
|
57
|
+
}
|
|
58
|
+
|
package/src/url/useUrlState.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCallback } from 'react'
|
|
2
2
|
import { useSearchParams } from 'react-router-dom'
|
|
3
|
-
import {
|
|
3
|
+
import { readSearch, writeFrom, pendingBase } from './urlState.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Read and write screen state in the URL's query (see urlState.js).
|
|
@@ -22,8 +22,8 @@ export function useUrlParams(defaults) {
|
|
|
22
22
|
const [params, setParams] = useSearchParams()
|
|
23
23
|
const get = useCallback((key) => readSearch(params, key, defaults ? defaults[key] : undefined), [params, defaults])
|
|
24
24
|
const set = useCallback((patch, opts) => {
|
|
25
|
-
setParams((
|
|
26
|
-
}, [setParams, defaults])
|
|
25
|
+
setParams(writeFrom(params, patch, defaults), { replace: Boolean(opts && opts.replace) })
|
|
26
|
+
}, [params, setParams, defaults])
|
|
27
27
|
return { get: get, set: set, params: params }
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -38,12 +38,10 @@ export function useUrlState(key, opts) {
|
|
|
38
38
|
const value = readSearch(params, key, o.default)
|
|
39
39
|
const setValue = useCallback((v, callOpts) => {
|
|
40
40
|
const replace = callOpts && callOpts.replace !== undefined ? callOpts.replace : Boolean(o.replace)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}, { replace: replace })
|
|
47
|
-
}, [setParams, key, o.default, o.replace])
|
|
41
|
+
const current = readSearch(pendingBase(params), key, o.default)
|
|
42
|
+
const next = typeof v === 'function' ? v(current) : v
|
|
43
|
+
const defaults = o.default !== undefined ? { [key]: o.default } : undefined
|
|
44
|
+
setParams(writeFrom(params, { [key]: next }, defaults), { replace: replace })
|
|
45
|
+
}, [params, setParams, key, o.default, o.replace])
|
|
48
46
|
return [value, setValue]
|
|
49
47
|
}
|