@bagelink/vue 1.10.42 → 1.11.3
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/dist/composables/index.d.ts.map +1 -1
- package/dist/composables/useQuery.d.ts +18 -0
- package/dist/composables/useQuery.d.ts.map +1 -0
- package/dist/index.cjs +41 -41
- package/dist/index.mjs +8378 -8358
- package/package.json +2 -2
- package/src/composables/index.ts +2 -1
- package/src/composables/useQuery.ts +50 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bagelink/vue",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.11.3",
|
|
5
5
|
"description": "Bagel core sdk packages",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Bagel Studio",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"signature_pad": "^5.0.9",
|
|
91
91
|
"vue-i18n": "^11.2.8",
|
|
92
92
|
"vue-toastification": "^2.0.0-rc.5",
|
|
93
|
-
"@bagelink/utils": "1.
|
|
93
|
+
"@bagelink/utils": "1.11.3"
|
|
94
94
|
},
|
|
95
95
|
"scripts": {
|
|
96
96
|
"dev": "tsx watch src/index.ts",
|
package/src/composables/index.ts
CHANGED
|
@@ -9,10 +9,10 @@ export { useDevice } from './useDevice'
|
|
|
9
9
|
export { useExcel } from './useExcel'
|
|
10
10
|
export { useLocalStore } from './useLocalStore'
|
|
11
11
|
export { usePolling } from './usePolling'
|
|
12
|
+
export { useQuery } from './useQuery'
|
|
12
13
|
export { useResizableLayoutProvider } from './useResizableLayout'
|
|
13
14
|
export type { PanelConfig, PanelState, ResizableContext } from './useResizableLayout'
|
|
14
15
|
export { useResizeObserver } from './useResizeObserver'
|
|
15
|
-
export { useTheme } from './useTheme'
|
|
16
16
|
interface UseBglSchemaParamsT<T> {
|
|
17
17
|
schema?: MaybeRefOrGetter<BglFormSchemaT<T>>
|
|
18
18
|
columns?: MaybeRefOrGetter<string[]>
|
|
@@ -35,4 +35,5 @@ export function useBglSchema<T = { [key: string]: unknown }>(
|
|
|
35
35
|
return getFallbackSchema(data, _columns.value)
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
export { useTheme } from './useTheme'
|
|
38
39
|
export { useValidateFieldValue } from './useValidateFieldValue'
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { WritableComputedRef } from 'vue'
|
|
2
|
+
import type { LocationQuery, LocationQueryRaw } from 'vue-router'
|
|
3
|
+
import { computed } from 'vue'
|
|
4
|
+
import { useRoute, useRouter } from 'vue-router'
|
|
5
|
+
|
|
6
|
+
function buildQuery(
|
|
7
|
+
current: LocationQuery,
|
|
8
|
+
key: string,
|
|
9
|
+
value: string | undefined,
|
|
10
|
+
): LocationQueryRaw {
|
|
11
|
+
const query: LocationQueryRaw = { ...current }
|
|
12
|
+
if (value === undefined) delete query[key]
|
|
13
|
+
else query[key] = value
|
|
14
|
+
return query
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type QueryDefaults<T> = { [K in keyof T]?: T[K] }
|
|
18
|
+
type QueryResult<T, D extends QueryDefaults<T>> = {
|
|
19
|
+
[K in keyof T]: WritableComputedRef<K extends keyof D ? NonNullable<T[K]> : T[K] | undefined>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reactive URL query param bindings.
|
|
24
|
+
* Each key becomes a writable computed that reads/writes a single query param.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const q = useQuery<{ search: string; page: string }>({ page: '1' })
|
|
28
|
+
* q.search.value = 'hello' // → ?search=hello&page=1
|
|
29
|
+
*/
|
|
30
|
+
export function useQuery<
|
|
31
|
+
T extends Record<string, string> = Record<string, string>,
|
|
32
|
+
D extends QueryDefaults<T> = QueryDefaults<T>,
|
|
33
|
+
>(defaults?: D): QueryResult<T, D> {
|
|
34
|
+
const route = useRoute()
|
|
35
|
+
const router = useRouter()
|
|
36
|
+
const cache: Record<string, WritableComputedRef<string | undefined>> = {}
|
|
37
|
+
|
|
38
|
+
return new Proxy({} as QueryResult<T, D>, {
|
|
39
|
+
get(_, key: string) {
|
|
40
|
+
if (!(key in cache)) {
|
|
41
|
+
const fallback = defaults?.[key as keyof T] as string | undefined
|
|
42
|
+
cache[key] = computed({
|
|
43
|
+
get: () => (route.query[key] as string | undefined) ?? fallback,
|
|
44
|
+
set: value => router.replace({ query: buildQuery(route.query, key, value) }),
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
return cache[key] as QueryResult<T, D>[keyof T]
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
}
|