3

How to add a tab Dynamically on button click

i have tried all examples but none of them helped me

For example :- below intially 2 bootstrap tabs are there Named Tab1 and Tab2 .. Add Tab button is there. When we click on that automatically tab3 needs to bind How to do this guyz give me suggetions please

Reference Link :- https://www.bootply.com/61679

6
  • Can you provide an example component you'd like to implement this for? You'd want to set the tab3 visibility in your component state Commented Jul 19, 2019 at 7:31
  • Hii @Kellen thanks for your quick response ... i want exactly the same implementation without ANY JQUERY or javascript can u check below link please bootply.com/61679 Commented Jul 19, 2019 at 7:34
  • Can you clarify what you mean by "without any javascript"? As in no other dependencies? ReactJS is javascript Commented Jul 19, 2019 at 7:41
  • sorry updating my question without JQUERY @Kellen Commented Jul 19, 2019 at 7:43
  • Can you update the question with already worked out code? Commented Jul 19, 2019 at 7:59

1 Answer 1

5

I suppose this would be the basic template you need to accomplish this feature in React.

See this sandbox here: https://codesandbox.io/s/romantic-wiles-w2uwh

Working code:

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class App extends React.Component {
  state = {
    tabs: [
      { name: "Tab 1", content: "Wow this is tab 1" },
      { name: "Tab 2", content: "Look at me, it's Tab 2" }
    ],
    currentTab: { name: "Tab 1", content: "Wow this is tab 1" }
  };

  createTabs = () => {
    const { tabs, currentTab } = this.state;

    const allTabs = tabs.map(tab => {
      return (
        <li>
          <button
            className={currentTab.name == tab.name ? "tab active" : "tab"}
            onClick={() => this.handleSelectTab(tab)}
          >
            {tab.name}
          </button>
        </li>
      );
    });

    return <ul className="nav nav-tabs">{allTabs}</ul>;
  };

  handleSelectTab = tab => {
    this.setState({
      currentTab: tab
    });
  };

  handleAddTab = () => {
    const { tabs } = this.state;

    const newTabObject = {
      name: `Tab ${tabs.length + 1}`,
      content: `This is Tab ${tabs.length + 1}`
    };

    this.setState({
      tabs: [...tabs, newTabObject],
      currentTab: newTabObject
    });
  };

  render() {
    const { currentTab } = this.state;
    return (
      <div className="container">
        <div className="well">
          <button className="add-tab-button" onClick={this.handleAddTab}>
            <i className="text-primary fas fa-plus-square" /> Add Tab
          </button>
          {this.createTabs()}
          <div className="tab-content">{currentTab.content}</div>
        </div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up to request clarification or add additional context in comments.

24 Comments

Thank you so much @Christopher ... i went through your code and its pretty good what i expected is the same and may i know one more thing now it was adding tab1, tab2 and so on right... how can we change tab names dynamically..For example i have created 4 tabs how can i change those tab names
@sagarbhanu I just added some additional functionality to the sandbox here: codesandbox.io/s/romantic-wiles-w2uwh Take a look at the onBlur and DoubleClick event-handlers. That should give you a good idea of what is happening :). Also, please consider marking my solution as the answer and upvoting !
thanks bro upvoted @Christopher Ngo...I will take a look what u did on current snackbox ..
@sagarbhanu thanks bro! To active the tab-editting feature, just double-click on the tab you want to edit. When you are finished typing the new name, click away from the tab and it will be set :). Please mark as answer as well if it meets your requirements.
sure bro and can u take a look at this issue as well please i solved this in a diff way but i want a proper solution for this stackoverflow.com/questions/57019906/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.