标题:新手求助 如何实现Tabs标签的可编辑?
只看楼主
lihaoy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-4-23
 问题点数:0 回复次数:0 
新手求助 如何实现Tabs标签的可编辑?
希望实现Tabs标签页的文字可以编辑;

目前我的思路是给Tab元素添加一个input输入框,点击时触发,鼠标移开时保存输入文字,但不知道代码该如何写;

这是我已有的代码,展示了Tabs标签页,并实现了新增删除功能;

框架语言是React+JS,使用了Antd组件库;
程序代码:
import { Tabs, Input } from 'antd';
import React from 'react';

const { TabPane } = Tabs;

const initialPanes = [
  { title: 'Tab 1', content: 'Content of Tab 1', key: '1' },
  { title: 'Tab 2', content: 'Content of Tab 2', key: '2' },
  {
    title: 'Tab 3',
    content: 'Content of Tab 3',
    key: '3',
  },
];

class Demo extends  {
  newTabIndex = 0;

  state = {
    activeKey: initialPanes[0].key,
    panes: initialPanes,
  };

  onChange = (activeKey) => {
    this.setState({ activeKey });
  };

  onEdit = (targetKey, action) => {
    this[action](targetKey);
  };

  add = () => {
    const { panes } = this.state;
    const activeKey = `newTab${this.newTabIndex++}`;
    const newPanes = [...panes];
    newPanes.push({ title: 'New Tab', content: 'Content of new Tab', key: activeKey });
    this.setState({
      panes: newPanes,
      activeKey,
    });
  };

  remove = (targetKey) => {
    const { panes, activeKey } = this.state;
    let newActiveKey = activeKey;
    let lastIndex;
    panes.forEach((pane, i) => {
      if (pane.key === targetKey) {
        lastIndex = i - 1;
      }
    });
    const newPanes = panes.filter((pane) => pane.key !== targetKey);
    if (newPanes.length && newActiveKey === targetKey) {
      if (lastIndex >= 0) {
        newActiveKey = newPanes[lastIndex].key;
      } else {
        newActiveKey = newPanes[0].key;
      }
    }
    this.setState({
      panes: newPanes,
      activeKey: newActiveKey,
    });
  };

  render() {
    const { panes, activeKey } = this.state;
    return (
      // eslint-disable-next-line react/react-in-jsx-scope
      // eslint-disable-next-line react/jsx-filename-extension
      <Tabs
        type="editable-card"
        onChange={this.onChange}
        activeKey={activeKey}
        onEdit={this.onEdit}
      >
        <Input type="text" />
        {panes.map((pane) => (
          <TabPane tab={pane.title} key={pane.key} closable={pane.closable}>
            {pane.content}
          </TabPane>
        ))}
      </Tabs>
    );
  }
}

export default Demo;


如果大佬可以指点一下思路,或能够提供具体代码的话更好,万分感谢!
搜索更多相关主题的帖子: const this key Tab content 
2021-04-23 10:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505545-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.224572 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved