Skip to content

Commit

Permalink
fix: fix optional behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 19, 2023
1 parent 681a2f4 commit 5b42108
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/form/src/extensions/textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
<div class="bottom">
<el-input
type="textarea"
v-model="config"
:autosize="autosize"
:disabled="disabled"
:modelValue="modelValue"
@update:modelValue="$emit('update:modelValue', $event)"
></el-input>
</div>
</schema-base>
Expand All @@ -20,7 +19,7 @@
<script lang="ts" setup>
import { computed, PropType } from 'vue'
import { Schema } from '../utils'
import { Schema, useModel } from '../utils'
import SchemaBase from '../base.vue'
const props = defineProps({
Expand All @@ -33,6 +32,8 @@ const props = defineProps({
defineEmits(['update:modelValue'])
const config = useModel()
const autosize = computed(() => {
const { rows } = props.schema.meta.extra || {}
if (typeof rows === 'number') return { minRows: rows, maxRows: rows }
Expand Down
16 changes: 9 additions & 7 deletions packages/form/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,23 @@ export function inferFallback(schema: Schema) {

function optional(schema: Schema): Schema {
if (schema.type === 'const') return schema
schema = new Schema(schema)
if (schema.type === 'object') {
return Schema.object(valueMap(schema.dict, optional))
schema.dict = valueMap(schema.dict, optional)
} else if (schema.type === 'tuple') {
return Schema.tuple(schema.list.map(optional))
schema.list = schema.list.map(optional)
} else if (schema.type === 'intersect') {
return Schema.intersect(schema.list.map(optional))
schema.list = schema.list.map(optional)
} else if (schema.type === 'union') {
return Schema.union(schema.list.map(optional))
schema.list = schema.list.map(optional)
} else if (schema.type === 'dict') {
return Schema.dict(optional(schema.inner))
schema.inner = optional(schema.inner)
} else if (schema.type === 'array') {
return Schema.array(optional(schema.inner))
schema.inner = optional(schema.inner)
} else {
return Schema(schema).required(false)
schema.meta.required = false
}
return schema
}

export function check(schema: any, value: any) {
Expand Down

0 comments on commit 5b42108

Please sign in to comment.