Skip to content

Commit

Permalink
fix: do not show model when initial value is not compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 12, 2022
1 parent 8ac18a7 commit 24bf6be
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/form/src/schema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</template>

<template v-if="isPrimitive">
<schema-primitive v-model="config" :schema="active" :disabled="disabled"></schema-primitive>
<schema-primitive v-model="config" :schema="active" :disabled="disabled" v-if="!check(schema, initial)"></schema-primitive>
</template>

<template v-else-if="isComposite">
Expand Down Expand Up @@ -158,6 +158,15 @@ const props = defineProps({
const emit = defineEmits(['update:modelValue', 'command'])
function check(schema: any, value: any) {
try {
optional(schema)(value)
return true
} catch {
return false
}
}
const changed = computed(() => {
return !props.instant && !deepEqual(props.initial, props.modelValue)
})
Expand Down Expand Up @@ -213,28 +222,24 @@ const config = ref()
const signal = ref(false)
function optional(schema: Schema): Schema {
if (schema.type === 'const') return schema
if (schema.type === 'object') {
return Schema.object(valueMap(schema.dict, (item) => {
return item.type === 'const' ? item : item.required(false)
}))
return Schema.object(valueMap(schema.dict, optional))
} else if (schema.type === 'intersect') {
return Schema.intersect(schema.list.map(optional))
} else if (schema.type === 'union') {
return Schema.union(schema.list.map(optional))
} else {
return schema
return schema.required(false)
}
}
watch(() => props.modelValue, (value) => {
config.value = value ?? getFallback(props.schema)
active.value = props.schema
for (const item of choices.value) {
try {
optional(item)(config.value)
active.value = item
break
} catch {}
if (!check(item, config.value)) continue
active.value = item
}
}, { immediate: true, deep: true })
Expand Down

0 comments on commit 24bf6be

Please sign in to comment.