kailong321200875 1 سال پیش
والد
کامیت
08665a35ac
3فایلهای تغییر یافته به همراه15 افزوده شده و 1 حذف شده
  1. 1 1
      .vscode/settings.json
  2. 8 0
      src/components/Form/src/helper/index.ts
  3. 6 0
      src/views/Components/Form/UseFormDemo.vue

+ 1 - 1
.vscode/settings.json

@@ -5,7 +5,7 @@
     "source.fixAll.eslint": "explicit"
   },
   "[vue]": {
-    "editor.defaultFormatter": "Vue.volar"
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
   },
   "i18n-ally.localesPaths": ["src/locales"],
   "i18n-ally.keystyle": "nested",

+ 8 - 0
src/components/Form/src/helper/index.ts

@@ -143,6 +143,7 @@ export const setItemComponentSlots = (slotsProps: Recordable = {}): Recordable =
  */
 export const initModel = (schema: FormSchema[], formModel: Recordable) => {
   const model: Recordable = { ...formModel }
+  console.log('【model】:', model)
   schema.map((v) => {
     if (v.remove) {
       delete model[v.field]
@@ -158,5 +159,12 @@ export const initModel = (schema: FormSchema[], formModel: Recordable) => {
       // model[v.field] = hasField ? model[v.field] : v.value !== void 0 ? v.value : undefined
     }
   })
+  // 如果 schema 对应的 field 不存在,则删除 model 中的对应的 field
+  Object.keys(model).forEach((key) => {
+    const isExist = schema.some((item) => item.field === key)
+    if (!isExist) {
+      delete model[key]
+    }
+  })
   return model
 }

+ 6 - 0
src/views/Components/Form/UseFormDemo.vue

@@ -407,6 +407,11 @@ setTimeout(async () => {
   const formData = await getFormData()
   console.log(formData)
 }, 2000)
+
+const getData = async () => {
+  const formData = await getFormData()
+  console.log(formData)
+}
 </script>
 
 <template>
@@ -451,6 +456,7 @@ setTimeout(async () => {
     <BaseButton @click="inoutValidation">
       {{ `${t('formDemo.input')} ${t('formDemo.formValidation')}` }}
     </BaseButton>
+    <BaseButton @click="getData"> 获取值 </BaseButton>
   </ContentWrap>
   <ContentWrap :title="`UseForm ${t('formDemo.example')}`">
     <Form :schema="schema" @register="formRegister" @validate="formValidate" />