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

fix(batch-add-formitem):fix the form not working when setFieldsValue through form-groups and add a demo with form groups #3765

Merged
merged 2 commits into from
Apr 20, 2024

Conversation

electroluxcode
Copy link

@electroluxcode electroluxcode commented Apr 18, 2024

General

✏️ Mark the necessary items without changing the structure of the PR template.

  • [ x] Pull request template structure not broken

  • 1.修复不能通过form组件的 setFieldsValue和modal 进行表单组赋值的问题

  • 2.添加增删表单组的示例,可以一组组添加并且增删与赋值(之前表单增删示例的demo重命名成表单项了,因为从源码看这里本质是一项项添加的)

@electroluxcode electroluxcode changed the title Fix/batch add form item fix(batch-add-formitem):fix the form not working when setFieldsValue through form-groups and add a demo with form groups Apr 18, 2024
@electroluxcode electroluxcode marked this pull request as draft April 19, 2024 03:28
@electroluxcode electroluxcode marked this pull request as ready for review April 19, 2024 03:31
@wangjue666
Copy link
Collaborator

  • 1.修复不能通过form组件的 setFieldsValue和modal 进行表单组赋值的问题

这个有相关issue吗?

@electroluxcode
Copy link
Author

electroluxcode commented Apr 19, 2024

  • 1.修复不能通过form组件的 setFieldsValue和modal 进行表单组赋值的问题

这个有相关issue吗?

issue区中没有看到相关的issue,这是我在实际开发中发现的问题。可以通过pr demo中的 setFieldsValue 来进行测试,在这个pr之前,对这个demo代码使用 setFieldsValue对于表单组是不生效的

@wangjue666 wangjue666 self-requested a review April 19, 2024 09:31
@wangjue666
Copy link
Collaborator

issue区中没有看到相关的issue,这是我在实际开发中发现的问题。可以通过pr demo中的 setFieldsValue 来进行测试,在这个pr之前,对这个demo代码使用 setFieldsValue对于表单组是不生效的

好的,我晚点测试测试

@electroluxcode
Copy link
Author

electroluxcode commented Apr 19, 2024

issue区中没有看到相关的issue,这是我在实际开发中发现的问题。可以通过pr demo中的 setFieldsValue 来进行测试,在这个pr之前,对这个demo代码使用 setFieldsValue对于表单组是不生效的

好的,我晚点测试测试

好嘞,麻烦你拉,我clone了一个main分支下的vben admin ,可以直观看到修改前bug的复现(url: #/comp/form/appendForm)

https://codesandbox.io/p/github/electroluxcode/vue-vben-admin/main?file=%2Fsrc%2Fviews%2Fdemo%2Fform%2FAppendForm.vue%3A5%2C36&import=true&workspaceId=137471a6-f9d2-4b79-bfb2-49bc9d3932ec

在点击 上方链接的 设置初始值 的时候,可以复现bug

@wangjue666
Copy link
Collaborator

@electroluxcode 你提供的codesandbox链接我无法打开

const [register, { appendSchemaByField, removeSchemaByField, validate, setFieldsValue }] =
    useForm({
      schemas: [
        {
          field: 'field0a',
          component: 'Input',
          label: '字段0',
          required: true,
        },
        {
          field: 'field0b',
          component: 'Input',
          label: '字段0',
          required: true,
        },
        {
          field: '0',
          // component: 'Input',
          label: ' ',
          slot: 'add',
        },
      ],
      labelWidth: 100,
      actionColOptions: { span: 24 },
      baseColProps: { span: 8 },
    });

  async function handleSubmit() {
    setFieldsValue({ field0a: '1', field0b: '2' });
    try {
      const data = await validate();
      console.log(data);
    } catch (e) {
      console.log(e);
    }
  }


Screenity.video.-.Apr.20.2024.mp4

我测试下 没有复现你的问题,是哪个步骤出差错了吗?

@electroluxcode
Copy link
Author

electroluxcode commented Apr 20, 2024

@electroluxcode 你提供的codesandbox链接我无法打开

const [register, { appendSchemaByField, removeSchemaByField, validate, setFieldsValue }] =
    useForm({
      schemas: [
        {
          field: 'field0a',
          component: 'Input',
          label: '字段0',
          required: true,
        },
        {
          field: 'field0b',
          component: 'Input',
          label: '字段0',
          required: true,
        },
        {
          field: '0',
          // component: 'Input',
          label: ' ',
          slot: 'add',
        },
      ],
      labelWidth: 100,
      actionColOptions: { span: 24 },
      baseColProps: { span: 8 },
    });

  async function handleSubmit() {
    setFieldsValue({ field0a: '1', field0b: '2' });
    try {
      const data = await validate();
      console.log(data);
    } catch (e) {
      console.log(e);
    }
  }

Screenity.video.-.Apr.20.2024.mp4
我测试下 没有复现你的问题,是哪个步骤出差错了吗?

你的这个demo其是复现的是表单项的增删,这个地方没有问题。而批量增删表单组,例如设置一个数组的默认值是有问题的。示例如下

<template>
  <PageWrapper title="表单增删示例">
    <CollapseContainer title="表单组增删" class="my-3">
      <a-button @click="setGroup">设置初始值</a-button>
      <a-button class="m-2" @click="addGroup"> 批量添加表单 </a-button>
      <a-button @click="delGroup">批量减少表单</a-button>
      <BasicForm @register="registerGroup" @submit="handleSubmitGroup">
      </BasicForm>
    </CollapseContainer>
  </PageWrapper>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { BasicForm, useForm } from "@/components/Form";
import { CollapseContainer } from "@/components/Container";
import { PageWrapper } from "@/components/Page";

import { useMessage } from "@/hooks/web/useMessage";
const { createMessage } = useMessage();

const count = ref(0);
const [
  registerGroup,
  {
    appendSchemaByField: appendSchemaByFieldGroup,
    removeSchemaByField: removeSchemaByFieldGroup,
    getFieldsValue: getFieldsValueGroup,
    setFieldsValue,
  },
] = useForm({
  schemas: [
    {
      field: `field[${count.value}].a`,
      component: "Input",
      label: "字段a",
      colProps: { span: 9 },
    },
    {
      field: `field[${count.value}].b`,
      colProps: { span: 9 },
      component: "Input",
      label: "字段b",
    },
  ],
  labelWidth: 100,
  actionColOptions: { span: 24 },
  baseColProps: { span: 8 },
});

function addGroup() {
  count.value++;
  appendSchemaByFieldGroup(
    [
      {
        field: `field[${count.value}].a`,
        component: "Input",
        colProps: { span: 9 },
        label: "字段a",
      },
      {
        field: `field[${count.value}].b`,
        component: "Input",
        colProps: { span: 9 },
        label: "字段b",
      },
    ],
    ""
  );
}

function delGroup() {
  removeSchemaByFieldGroup([
    `field[${count.value}].a`,
    `field[${count.value}].b`,
  ]);
  count.value--;
}

function setGroup() {
  setFieldsValue({
    field: [
      {
        a: "默认a",
        b: "默认b",
      },
    ],
  });
}

function handleSubmitGroup() {
  createMessage.success("请前往控制台查看输出");
  console.log(getFieldsValueGroup());
}
</script>

最后重新补一下示例的demo链接:https://codesandbox.io/p/github/electroluxcode/vue-vben-admin/main?file=%2Fsrc%2Fviews%2Fdemo%2Fform%2FAppendForm.vue%3A5%2C36&import=true&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clv6imzsp0006356o4pluuk0b%2522%252C%2522sizes%2522%253A%255B100%252C0%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clv6imzsp0002356o5x2e917e%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clv6imzsp0004356ozhu880cb%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clv6imzsp0005356ovr803bi2%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clv6imzsp0002356o5x2e917e%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clv6imzsp0001356oihui7stq%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252FREADME.md%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%252C%257B%2522id%2522%253A%2522clv6ittcd0002356pmrl3icl6%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A5%252C%2522startColumn%2522%253A36%252C%2522endLineNumber%2522%253A5%252C%2522endColumn%2522%253A36%257D%255D%252C%2522filepath%2522%253A%2522%252Fsrc%252Fviews%252Fdemo%252Fform%252FAppendForm.vue%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%252C%2522id%2522%253A%2522clv6imzsp0002356o5x2e917e%2522%252C%2522activeTabId%2522%253A%2522clv6ittcd0002356pmrl3icl6%2522%257D%252C%2522clv6imzsp0005356ovr803bi2%2522%253A%257B%2522id%2522%253A%2522clv6imzsp0005356ovr803bi2%2522%252C%2522activeTabId%2522%253A%2522clv6ionfh004d356olu7z0d83%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A5173%252C%2522id%2522%253A%2522clv6ionfh004d356olu7z0d83%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%252F%2523%252Fcomp%252Fform%252FappendForm%2522%257D%252C%257B%2522type%2522%253A%2522ENV_SETUP%2522%252C%2522id%2522%253A%2522clv7u1msr0004356o10p5z4k8%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%257D%252C%2522clv6imzsp0004356ozhu880cb%2522%253A%257B%2522id%2522%253A%2522clv6imzsp0004356ozhu880cb%2522%252C%2522activeTabId%2522%253A%2522clv6irxmg003v356o2wvln4ub%2522%252C%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clv6imzsp0003356otm9ogww1%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TERMINAL%2522%252C%2522shellId%2522%253A%2522clv6inrrx012ddlilbt20fqaf%2522%257D%252C%257B%2522id%2522%253A%2522clv6irxmg003v356o2wvln4ub%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TERMINAL%2522%252C%2522shellId%2522%253A%2522clv6isnme013mdlil0342ei1m%2522%257D%255D%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Afalse%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D

@wangjue666
Copy link
Collaborator

@electroluxcode LGTM! 下次可以先开个issue, pr链接到issue上

@wangjue666 wangjue666 merged commit 82bf009 into vbenjs:main Apr 20, 2024
2 checks passed
wangjue666 pushed a commit that referenced this pull request Apr 20, 2024
… add a demo with form groups (#3765)

* fix(util): 修复form表单批量添加的时候用setFieldsValue和model设置不生效的问题

* feat(demo): 为表单组增删添加表单组增删的demo与给出表单组赋值修复后setFieldsValue的示例
@vbenjs vbenjs locked and limited conversation to collaborators Aug 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants