Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(BasicForm):fix invaild defaultValue && split-setdefault-setvalue && add resetDefaultField #3815

Merged

Conversation

electroluxcode
Copy link

@electroluxcode electroluxcode commented May 8, 2024

Type

ℹ️ What types of changes does your code introduce?

👉 Put an x in the boxes that apply

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

fix #3814
resolve #3795

这个pr主要做了三件事

  • 解决reset在componentProps不能还原defaultValue 的bug
  • 将 setdefaultvalue 和 setFieldsValue 进行分离。之前的setFieldsValue耦合了设置默认值和设置值的逻辑,现在的setFieldsValue只设置值,移除了设置默认值的逻辑
  • 新增 resetDefaultField 方法,可以还原指定字段的 defaultValue

新功能测试 demo

<template>
  <Alert message="测试新功能" />
  <BasicForm @register="registerCustom" class="my-5" />
  <Button  @click="reset">点我还原默认值</Button>

  <Button  @click="set">点我设置默认值</Button>
</template>

<script setup lang="ts">
  import { Alert,Button } from 'ant-design-vue';
  import { BasicForm, FormSchema, useForm } from '@/components/Form';
  const schemasCustom: FormSchema[] = [
    {
      field: 'field1',
      component: 'ApiSelect',
      label: '字段1',
      componentProps: {
        defaultValue:"杰克",
        api: () => {
          return new Promise((resolve) => {
            resolve([
              {
                label: '111杰克',
                value: '杰克',
              },
              {
                label: '222伊莱克斯',
                value: '伊莱克斯',
              },
            ]);
          });
        },
      },
    },
    {
      field: 'field2',
      component: 'Input',
      label: '字段2',
      required:true,
      componentProps: {
        defaultValue:"默认input"
      },
    },
  ];
  const [registerCustom,{resetDefaultField,setFieldsValue}] = useForm({
    labelWidth: 160,
    schemas: schemasCustom,
  });
  const reset = ()=>{
    resetDefaultField(["other","field1","field2"])
  }
  const set = ()=>{
    setFieldsValue({
      "field1":"伊莱克斯"
    })
  }
</script>

@electroluxcode electroluxcode changed the title perf(BasicForm):fix invaild defaultValue && split-setdefault-setvalue perf(BasicForm):fix invaild defaultValue && split-setdefault-setvalue && add resetDefaultField May 8, 2024
@wangjue666 wangjue666 merged commit 88e77db into vbenjs:main May 8, 2024
2 checks passed
@vbenjs vbenjs locked and limited conversation to collaborators Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants