【问题标题】:how to add Customized Add Button to horizontal Tab using AntDesign如何使用 Ant Design 将自定义添加按钮添加到水平选项卡
【发布时间】:2021-08-12 11:30:10
【问题描述】:

我正在使用 AntDesign 绘制水平选项卡,我想在选项卡旁边添加自定义添加按钮,如何使用 Ant design 来做到这一点,我们在 Antdesign 中内置了带有 + Symbol 的配置我该怎么做。到现在是这个样子。

这是代码

import React,{useState} from 'react'
import { Tabs, Button,Row,Col } from 'antd';

const { TabPane } = Tabs;

const CompoundsTabs = () => {

    const initialPanes = [
        { title: "Tab 1", content: "Content of Tab 1", key: "1" },
        { title: "Tab 2", content: "Content of Tab 2", key: "2" }
      ];
     let newTabIndex = 0;

     const [activeState, setActiveState] = useState(initialPanes[0].key);
     const [panes, setPanes] = useState(initialPanes);
     
     const add = () => {
        const activeKey =
          (panes && panes.length ? +panes[panes.length - 1].key : 0) + 1;
        setActiveState(activeKey);
        setPanes((prev) => [
          ...prev,
          {
            title: "Tab " + activeKey,
            content: "Content of new Tab",
            key: activeKey
          }
        ]);
      };

      const remove = (key) => {
        setPanes((prev) => {
          const idx = prev.findIndex((item) => +item.key === +key);
          prev.splice(idx, 1);
          return [...prev];
        });
      };

      
      const onChange = (activeKey) => {
        setActiveState(activeKey);
      };

      
      const onEdit = (targetKey, action) => {
        if (action === "remove") {
          console.log("sdsds", targetKey);
          remove(targetKey);
        } else if (action === "add") {
          add();
        }
      };

      const deleteCompound=()=>{
        console.log("delete")
      }
    return (
        <div>
        <Tabs
          type="editable-card"
          onChange={onChange}
          activeKey={activeState}
          onEdit={onEdit}
          hideRemove
        >
          {panes.map((pane) => (
            <TabPane tab={<Row><Col span={18}>{pane.title} </Col>
            <Col span={4}></Col>
            <Col span={2} onClick={deleteCompound}>X</Col>
            </Row>} key={pane.key}>
           
                {pane.content}
         
                
           
            </TabPane>
          ))}
        </Tabs>
      </div>
    )
}

export default CompoundsTabs

我推荐了https://ant.design/components/tabs/https://codepen.io/pen/?&editors=001 这里是标签上方的自定义按钮,我希望它在最后一个标签旁边。

谁能帮我解决这个问题?

谢谢

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    新建按钮组件:-

        const MyButton = () => {
      return <Button type="primary">Customize Button</Button>;
    };
    

    然后在 addIcon 属性中使用:-

     <Tabs
            type="editable-card"
            onChange={this.onChange}
            activeKey={activeKey}
            addIcon={<MyButton />}
            onEdit={this.onEdit}
          >
            {panes.map(pane => (
              <TabPane tab={pane.title} key={pane.key} closable={pane.closable}>
                {pane.content}
              </TabPane>
            ))}
          </Tabs>
    

    【讨论】:

    • 谢谢你,它正在工作
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    相关资源
    最近更新 更多