Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tab switching conditional

Tags:

reactjs

antd

I have a basic TAB interface and was wondering if there is a way upon tab click, to check a condition. Depending on the condition we go ahead with the tab change or not.

Looking at the methods available, i could not find anything that would help me.

onChange and onTabClick do not seem to work for what im trying to do, any ideas ?

Thanks!

like image 787
Pablo Rincon Avatar asked Aug 31 '25 03:08

Pablo Rincon


1 Answers

I don't know if is possible in your version but u can do it like this: (In the exemple i don't set up any logic but you can do what ever you want in the button)

class App extends React.Component {
  state = {
    activeKey: "1"
  };

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

  render() {
    return (
      <div className="App">
        <p>Current antd version: {version}</p>
        <div>
          <Tabs
            type="primary"
            onChange={this.handleChange}
            activeKey={this.state.activeKey}
          >
            <TabPane tab="First" key="1">
              <Button
                onClick={() => {
                  this.setState({ activeKey: "2" });
                }}
              >
                Go to Second Tab
              </Button>
            </TabPane>
            <TabPane tab="Second" key="2">
              <Button
                onClick={() => {
                  this.setState({ activeKey: "1" });
                }}
              >
                Back to first tab
              </Button>
            </TabPane>
          </Tabs>
        </div>
      </div>
    );
  }
}

Edit broken-sound-5897p

like image 191
Filipe Avatar answered Sep 02 '25 20:09

Filipe