Skip to content

Commit a6b6f4f

Browse files
committed
fix: fix union cache break default, fix koishijs/webui#158
See: Akegarasu/lora-scripts#230
1 parent 0cec7ac commit a6b6f4f

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

packages/form/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "schemastery-vue",
33
"description": "Type driven schema validator",
4-
"version": "7.0.1",
4+
"version": "7.0.3",
55
"main": "src/index.ts",
66
"repository": {
77
"type": "git",

packages/form/src/extensions/union.vue

+28-37
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import { computed, PropType, ref, watch, WatchStopHandle } from 'vue'
4545
import { deepEqual, isNullable } from 'cosmokit'
4646
import { useI18n } from 'vue-i18n'
47-
import { check, getChoices, getFallback, Schema, useI18nText } from '../utils'
47+
import { check, getChoices, getFallback, Schema, useModel, useI18nText } from '../utils'
4848
import zhCN from '../locales/zh-CN.yml'
4949
import enUS from '../locales/en-US.yml'
5050
@@ -57,21 +57,13 @@ const props = defineProps({
5757
extra: {} as PropType<any>,
5858
})
5959
60-
const emit = defineEmits(['update:modelValue'])
60+
defineEmits(['update:modelValue'])
6161
6262
const tt = useI18nText()
6363
64-
const config = ref()
6564
const choices = ref<Schema[]>()
6665
const cache = ref<any[]>()
6766
const active = ref<Schema>()
68-
let stop: WatchStopHandle
69-
70-
const doWatch = () => watch(config, (value) => {
71-
const index = choices.value.indexOf(active.value)
72-
if (index >= 0) cache.value[index] = value
73-
emit('update:modelValue', deepEqual(value, props.schema.meta.default) ? null : value)
74-
}, { deep: true })
7567
7668
watch(() => props.schema, (value) => {
7769
choices.value = getChoices(props.schema)
@@ -81,36 +73,35 @@ watch(() => props.schema, (value) => {
8173
})
8274
}, { immediate: true })
8375
84-
watch(() => [props.modelValue, props.schema] as const, ([value, schema]) => {
85-
stop?.()
86-
config.value = value
87-
value ??= schema.meta.default
88-
active.value = null
89-
let hasTransform = true, depth = 0
90-
while (!active.value && hasTransform && ++depth < 10) {
91-
hasTransform = false
92-
for (const item of schema.list) {
93-
if (item.meta.hidden) continue
94-
if (!check(item, value)) continue
95-
if (item.type === 'transform') {
96-
if (!item.callback) continue
97-
try {
98-
value = item.callback(value)
99-
} catch (error) {
100-
console.error(error)
101-
continue
76+
const config = useModel({
77+
input(value) {
78+
active.value = null
79+
let hasTransform = true, depth = 0
80+
while (!active.value && hasTransform && ++depth < 10) {
81+
hasTransform = false
82+
for (const [index, item] of props.schema.list.entries()) {
83+
if (item.meta.hidden) continue
84+
if (!check(item, value)) continue
85+
if (item.type === 'transform') {
86+
if (!item.callback) continue
87+
try {
88+
value = item.callback(value)
89+
} catch (error) {
90+
console.error(error)
91+
continue
92+
}
93+
hasTransform = true
94+
value ??= getFallback(props.schema)
95+
} else {
96+
active.value = item
97+
cache.value[index] = value
10298
}
103-
hasTransform = true
104-
config.value = value
105-
value ??= schema.meta.default
106-
} else {
107-
active.value = item
99+
break
108100
}
109-
break
110101
}
111-
}
112-
stop = doWatch()
113-
}, { immediate: true, deep: true })
102+
return value
103+
},
104+
})
114105
115106
const selectModel = computed({
116107
get() {

packages/form/src/utils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export function useDisabled() {
9090

9191
interface ConfigOptions<T> {
9292
strict?: boolean
93-
input(value: any): T
94-
output(value: T): any
93+
input?(value: any): T
94+
output?(value: T): any
9595
}
9696

9797
export function useModel<T = any>(options?: ConfigOptions<T>) {
@@ -101,21 +101,21 @@ export function useModel<T = any>(options?: ConfigOptions<T>) {
101101

102102
const doWatch = () => watch(config, (value) => {
103103
try {
104-
if (options) value = options.output(value)
104+
if (options?.output) value = options.output(value)
105105
} catch {
106106
return
107107
}
108108
if (deepEqual(value, props.schema.meta.default, options?.strict)) value = null
109109
emit('update:modelValue', value)
110110
}, { deep: true })
111111

112-
watch([() => props.modelValue, () => props.schema], ([value, schema]) => {
112+
watch(() => [props.modelValue, props.schema], ([value, schema]) => {
113113
stop?.()
114114
value ??= getFallback(schema)
115-
if (options) value = options.input(value)
115+
if (options?.input) value = options.input(value)
116116
config.value = value
117117
stop = doWatch()
118-
}, { immediate: true })
118+
}, { deep: true, immediate: true })
119119

120120
return config
121121
}

0 commit comments

Comments
 (0)