Buttons代码块

import React, { Fragment, useEffect, useState } from "react";
import { Button, Form, Select, Space } from "antd";
interface ButtonsProps {}
const options = [
  { value: "jack", label: "Jack" },
  { value: "lucy", label: "Lucy" },
  { value: "Yiminghe", label: "yiminghe" },
  { value: "disabled", label: "Disabled", disabled: true },
];
const Buttons: React.FC<ButtonsProps> = (props) => {
  const [conditionItems, setConditionItems] = useState([{ id: 1 }]); // 初始一个条件项
  const onFinish = (values: any) => {
    console.log("Success:", values);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  // 处理添加条件按钮点击事件

  // 处理添加条件按钮点击事件
  const handleAddCondition = (id?: any) => {
    const newCondition = {
      id: Date.now(), // 使用唯一标识符作为条件的ID
    };
    if(conditionItems.length)
    {
      const updatedItems = conditionItems.map((item) => {
        if (item.id === id) {
          return [item, newCondition];
        }
        return item;
      });
      setConditionItems(updatedItems.flat());
    }
    else{
      setConditionItems([newCondition])
    }

  };
  console.log(conditionItems)
  // 处理删除条件按钮点击事件
  const handleDeleteCondition = (id: any) => {
    const updatedItems = conditionItems.filter((item) => item.id !== id);
    setConditionItems(updatedItems);
  };
  return (
    <Fragment>
      <Form
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        style={{ maxWidth: 600 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        {conditionItems.map((item) => (
          <Fragment key={item.id}>
            <Form.Item name={item.id} key={item.id}>
              <div>
                <span>{item.id}</span>
                <Form.Item name={[item.id, "street"]}>
                  <Select options={options} />
                </Form.Item>
                <Form.Item name={[item.id, "street1"]}>
                  <Select options={options} />
                </Form.Item>
                <Form.Item name={[item.id, "street2"]}>
                  <Select options={options} />
                </Form.Item>
                <Form.Item name={[item.id, "street3"]}>
                  <Select options={options} />
                </Form.Item>
              </div>
            </Form.Item>
            <Button onClick={() => handleDeleteCondition(item.id)}>删除</Button>
            <Button onClick={() => handleAddCondition(item.id)}>添加</Button>
          </Fragment>
        ))}
        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
          <Button type="primary" htmlType="submit">
            Submit
          </Button>
        </Form.Item>
      </Form>
      <Button
        onClick={() =>
          handleAddCondition(conditionItems.length&&conditionItems[0].id)
        }
      >
        添加条件
      </Button>
    </Fragment>
  );
};
export default Buttons;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值