mirror of
https://github.com/fugary/simple-element-plus-template.git
synced 2025-11-12 14:27:49 +00:00
优化表单赋值与验证
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { $i18nBundle } from '@/messages'
|
||||
import ControlChild from '@/components/common-form-control/control-child.vue'
|
||||
import { useInputType } from '@/components/utils'
|
||||
@@ -100,6 +100,20 @@ const rules = computed(() => {
|
||||
return _rules
|
||||
})
|
||||
|
||||
const initFormModel = () => {
|
||||
if (formModel.value) {
|
||||
const option = props.option
|
||||
if (option.prop) {
|
||||
const defaultVal = get(formModel.value, option.prop)
|
||||
set(formModel.value, option.prop, defaultVal || option.value || undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initFormModel()
|
||||
|
||||
watch(() => props.option, initFormModel, { deep: true })
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<script setup>
|
||||
import { ref, watch } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
import { useVModel } from '@vueuse/core'
|
||||
import { set } from 'lodash'
|
||||
|
||||
/**
|
||||
* @type {CommonFormProps}
|
||||
@@ -60,27 +59,13 @@ const props = defineProps({
|
||||
}
|
||||
})
|
||||
|
||||
//= ============form暴露============//
|
||||
|
||||
const form = ref()
|
||||
|
||||
const emit = defineEmits(['submitForm', 'update:model'])
|
||||
|
||||
const formModel = useVModel(props, 'model', emit)
|
||||
|
||||
const initFormModel = () => {
|
||||
if (formModel.value) {
|
||||
props.options.forEach(option => {
|
||||
if (option.prop) {
|
||||
set(formModel.value, option.prop, option.value || undefined)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
//= ============form暴露============//
|
||||
|
||||
initFormModel()
|
||||
|
||||
watch(() => props.options, initFormModel, { deep: true })
|
||||
const form = ref()
|
||||
|
||||
defineExpose({
|
||||
form
|
||||
|
||||
Reference in New Issue
Block a user