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>
|
<script setup>
|
||||||
import { computed, ref } from 'vue'
|
import { computed, ref, watch } from 'vue'
|
||||||
import { $i18nBundle } from '@/messages'
|
import { $i18nBundle } from '@/messages'
|
||||||
import ControlChild from '@/components/common-form-control/control-child.vue'
|
import ControlChild from '@/components/common-form-control/control-child.vue'
|
||||||
import { useInputType } from '@/components/utils'
|
import { useInputType } from '@/components/utils'
|
||||||
@@ -100,6 +100,20 @@ const rules = computed(() => {
|
|||||||
return _rules
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useVModel } from '@vueuse/core'
|
import { useVModel } from '@vueuse/core'
|
||||||
import { set } from 'lodash'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {CommonFormProps}
|
* @type {CommonFormProps}
|
||||||
@@ -60,27 +59,13 @@ const props = defineProps({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
//= ============form暴露============//
|
|
||||||
|
|
||||||
const form = ref()
|
|
||||||
|
|
||||||
const emit = defineEmits(['submitForm', 'update:model'])
|
const emit = defineEmits(['submitForm', 'update:model'])
|
||||||
|
|
||||||
const formModel = useVModel(props, 'model', emit)
|
const formModel = useVModel(props, 'model', emit)
|
||||||
|
|
||||||
const initFormModel = () => {
|
//= ============form暴露============//
|
||||||
if (formModel.value) {
|
|
||||||
props.options.forEach(option => {
|
|
||||||
if (option.prop) {
|
|
||||||
set(formModel.value, option.prop, option.value || undefined)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
initFormModel()
|
const form = ref()
|
||||||
|
|
||||||
watch(() => props.options, initFormModel, { deep: true })
|
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
form
|
form
|
||||||
|
|||||||
Reference in New Issue
Block a user