ProFormList --复杂数据联动ProFormDependency

 需求:

(1)数据联动:测试数据1、2互相依赖,测试数据1<=测试数据2,测试数据2>=测试数据1。

(2)点击添加按钮,添加一行。

(3)自定义操作按钮。

(4)点击自定义操作按钮(禁用),禁用当前行。

代码实现:

import { StopOutlined } from '@ant-design/icons';
import { FormListActionType, ProCard, ProForm, ProFormDependency, ProFormList, ProFormText } from '@ant-design/pro-components';
import { gte, isEmpty, lte } from 'lodash';
import { useRef, useState } from 'react';

const Demo = () => {
    const [refresh, setRefresh] = useState<boolean>(false);
    const actionRef = useRef<
        FormListActionType<{
            name: string;
            [key: string]: any;
        }>
    >();
    const childrenDom = (record: Record<string, any>) => {
        return (
            <ProForm.Group key="group">
                <ProFormDependency name={['test2']}>
                    {(depValues) => {
                        return (
                            <ProFormText
                                disabled={record.disabled}
                                width="md"
                                name="test1"
                                label="测试数据1"
                                rules={[
                                    {
                                        required: true,
                                        message: '必选字段不能为空',
                                    },
                                    {
                                        pattern: /^[-+]?[0-9]+(\.[0-9]+)?$/,
                                        message: '请输入正确的数字',
                                    },
                                    {
                                        validator: async (_, value) => {
                                            if (isEmpty(value) || isEmpty(depValues.test2)) {
                                                return Promise.resolve();
                                            }
                                            if (lte(parseInt(value), parseInt(depValues.test2))) {
                                                return Promise.resolve();
                                            } else {
                                                return Promise.reject(new Error('测试数据1不能大于测试数据2'));
                                            }
                                        },
                                    },
                                ]}
                            />
                        );
                    }}
                </ProFormDependency>
                <ProFormDependency key="globalUseMode" name={['test1']}>
                    {(depValues) => {
                        return (
                            <ProFormText
                                disabled={record.disabled}
                                width="md"
                                name="test2"
                                label="测试数据2"
                                rules={[
                                    {
                                        required: true,
                                        message: '必选字段不能为空',
                                    },
                                    {
                                        pattern: /^[-+]?[0-9]+(\.[0-9]+)?$/,
                                        message: '请输入正确的数字',
                                    },
                                    {
                                        validator: async (_, value) => {
                                            if (isEmpty(value) || isEmpty(depValues.test1)) {
                                                return Promise.resolve();
                                            }
                                            if (gte(parseInt(value), parseInt(depValues.test1))) {
                                                return Promise.resolve();
                                            } else {
                                                return Promise.reject(new Error('测试数据2不能小于测试数据1'));
                                            }
                                        },
                                    },
                                ]}
                            />
                        );
                    }}
                </ProFormDependency>
            </ProForm.Group>
        );
    };
    return (
        <ProForm submitter={false}>
            <ProFormList
                name={'Test'}
                label="Test"
                initialValue={[{}]}
                actionRef={actionRef}
                actionRender={(field, action, defaultActionDom, count) => {
                    return [
                        ...defaultActionDom,
                        <StopOutlined
                            key="disable"
                            style={{ marginLeft: '5px' }}
                            onClick={() => {
                                const data = actionRef.current?.get(field.name);
                                if (data) {
                                    data.disabled = true;
                                    setRefresh(!refresh);
                                }
                            }}
                        />,
                    ];
                }}
                itemRender={({ listDom, action }, { index, record }) => (
                    <ProCard bordered style={{ marginBlockEnd: 8 }} title={`Test${index + 1}`} extra={action} bodyStyle={{ paddingBlockEnd: 0 }}>
                        {childrenDom(record)}
                    </ProCard>
                )}
            />
        </ProForm>
    );
};

export default Demo;

结果展示:

重点代码截图:

(1)数据联动:测试数据1、2互相依赖,测试数据1<=测试数据2,测试数据2>=测试数据1。

(2)点击添加按钮,添加一行。

(3)自定义操作按钮。

(4)点击自定义操作按钮(禁用),禁用当前行。

相关推荐

  1. FormData传送复杂数据

    2024-07-12 13:08:03       47 阅读
  2. 数据结构——复杂

    2024-07-12 13:08:03       28 阅读
  3. 数据结构——A/复杂

    2024-07-12 13:08:03       43 阅读
  4. 数据结构——时间复杂

    2024-07-12 13:08:03       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 13:08:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 13:08:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 13:08:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 13:08:03       69 阅读

热门阅读

  1. 原来没分库分表,后期如何分库分表?

    2024-07-12 13:08:03       22 阅读
  2. js 移动数组元素的几个方法

    2024-07-12 13:08:03       18 阅读
  3. 使用C# 实现期望最大化算法

    2024-07-12 13:08:03       20 阅读
  4. [NLP Begin] Classical NLP Methods - HMM

    2024-07-12 13:08:03       26 阅读
  5. 【ELK】filebeat 和logstash区别

    2024-07-12 13:08:03       18 阅读
  6. 行为模式9.策略模式------促销活动设计方案

    2024-07-12 13:08:03       21 阅读
  7. Vim 编辑文件时中文乱码的解决方法

    2024-07-12 13:08:03       18 阅读
  8. vim删除多行

    2024-07-12 13:08:03       25 阅读