Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focus is lost when using react-tabs as a controlled component #74

Closed
mziwisky opened this issue Feb 11, 2016 · 2 comments
Closed

focus is lost when using react-tabs as a controlled component #74

mziwisky opened this issue Feb 11, 2016 · 2 comments

Comments

@mziwisky
Copy link

i tried using react-tabs as a "controlled component", a la https://facebook.github.io/react/docs/forms.html#controlled-components

for react-tabs to behave as a proper controlled component, the behavior i would expect is as such:

if you pass <Tabs> a selectedIndex property, then it will ALWAYS be respected. i.e., the prop is not used merely to determine which tab to render initially, it's used to determine which tab to render all the time. so even if a user clicks a tab, that tab won't be selected unless the parent component reacts to the click by changing the selectedIndex. therefore, onSelect gets invoked when a Tab is clicked or keyboard-navigated to, which allows the developer to update selectedIndex appropriately.

a minimal example is as follows:

var MyComponent = React.createComponent({
  getInitialState () {
    return { currentTab: 0 };
  },
  handleTabSelect (index) {
    this.setState({ currentTab: index });
  },
  render () {
    return (
      <Tabs
        selectedIndex={ this.state.currentTab },
        onSelect={ this.handleTabSelect }
      >
        <TabList><Tab>One</Tab><Tab>Two</Tab></TabList>
        <TabPanel><p>Content of first tab</p></TabPanel>
        <TabPanel><p>Content of second tab</p></TabPanel>
      </Tabs>
    );
  }
});

If you try it, this minimal example actually mostly behaves as expected, but the problem I discovered is if you focus one of the tabs and then use the arrow key to navigate to the other, the other tab gets selected but your focus is immediately lost. If you remove both of those props from <Tabs>, then this issue goes away, but now MyComponent isn't keeping track of the selected tab in its state. (In our particular case, we want MyComponent to keep track of that index because we sometimes programmatically select a different tab based on other things going on in the component.)

Also, if you change the example such that handleTabSelect doesn't update this.state.currentTab, then react-tabs still changes its selected tab anyway -- i.e. it ignores its selectedIndex prop.

So, what do you think? Does controlled component behavior make sense for react-tabs? Is it perhaps something that's already on the roadmap?

@OliverJAsh
Copy link

👍 !

@Acezhangloo
Copy link

@danez I believe currently when we switch tabs, the focus will still lost. Is this issue fixed?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants