ant-design / pro-components

🏆 Use Ant Design like a Pro!
https://pro-components.antdigital.dev
MIT License
4.02k stars 1.28k forks source link

🐛[BUG]setFieldValue(undefined, undefined)会清空表单值 #8226

Closed lsy934127847 closed 1 month ago

lsy934127847 commented 1 month ago

如果设置 formRef.current?.setFieldValue(undefined, undefined); 会清空表单值 在老版本中,没有清空表单值,但是formRef.current?.getFieldsValue() 获取到的值全是 undefined 理想清空下 setFieldValue(undefined, undefined) 不应该对表单有任何影响

完整案例动图 https://www.yuque.com/youyudelonggui/gu3n9d/qkxgii44wk0x65gs

复现代码

import {
  ProForm,
  ProFormInstance,
  ProFormText,
} from '@ant-design/pro-components';
import { Button } from 'antd';
import { useRef } from 'react';

const Demo = () => {
  const formRef = useRef<ProFormInstance>();

  return (
    <div>
      <Button
        onClick={() => {
          formRef.current?.setFieldsValue({ name: 'lashiyong', age: '30' });
        }}
      >
        设置值
      </Button>
      <Button
        onClick={() => {
          formRef.current?.setFieldValue(undefined, undefined);
        }}
      >
        清空值
      </Button>
      <Button
        onClick={() => {
          console.log('asdasd', formRef.current?.getFieldsValue());
        }}
      >
        获取值
      </Button>
      <ProForm formRef={formRef}>
        <ProFormText name={'name'} />
        <ProFormText name={'age'} />
      </ProForm>
    </div>
  );
};

export default Demo;
chenshuai2144 commented 1 month ago

https://github.com/react-component/field-form/blob/8268294682853db89b8d3b6395aa05d0baea56e7/src/useForm.ts#L780

这里看看 源码, 这种边界情况应该没有测试用例,你可以帮忙写一个,你写了之后他就是官方标准了