manpreet singh
manpreet singh

Reputation: 73

React- Active next tab while clicking button

I have 4 tabs on my page. each tab contain different data.

i have button inside first tab, basically want to active next tab to show content when user clicks on that button.

     render(){
         return (
        <MuiThemeProvider>
            <div className="background">
                <Header/>
                <div>
                    <Card>
                        <Tabs>
                            <Tab label="General">
                                <div>
                                    <button>Normal button</button> 
     // when user clicks on this button description tab should be active
                                </div>
                            </Tab>
                            <Tab label="Descriptions">
                                <Description />
                            </Tab>

                            <Tab label="Content">
                                <Content />
                            </Tab>

                            <Tab label="Sidebar">
                                  <Sidebar/>
                            </Tab>
                        </Tabs>
                    </Card>
                </div>
            </div>
        </MuiThemeProvider>
    )
}

How i can do that??

Upvotes: 0

Views: 4284

Answers (1)

mrinalmech
mrinalmech

Reputation: 2165

Here's what you have to do - use controlled tabs. Assign a state value which determines which tab is open at a current time and use the click on the button to activate the next tab.

//The currentTab variable holds the currently active tab
constructor(props) {
    super(props);
    this.state = {
      currentTab: 'a',
    };
 }

handleChange = (value) => {
    this.setState({
      currentTab: value,
    });
 };

render(){
         return (
        <MuiThemeProvider>
            <div className="background">
                <Header/>
                <div>
                    <Card>
                        <Tabs 
                          value={this.state.currentTab}
                          onChange={this.handleChange}
                         >
                            <Tab label="General" value="a">
                                <div>
                                    //Sets the currentTab value to "b" which corresponds to the description tab 
                                    <button onClick={()=>this.handleChange("b")}>Normal button</button> 
                                </div>
                            </Tab>
                            <Tab label="Descriptions" value="b">
                                <Description />
                            </Tab>
                            <Tab label="Content" value="c">
                                <Content />
                            </Tab>
                            <Tab label="Sidebar" value="d">
                                <Sidebar/>
                            </Tab>
                        </Tabs>
                    </Card>
                </div>
            </div>
        </MuiThemeProvider>
    )
}

Upvotes: 3

Related Questions