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

[Tabs] onChange fire even if value didn't change #22378

Closed
2 tasks done
jjoselv opened this issue Aug 27, 2020 · 13 comments · Fixed by #22381
Closed
2 tasks done

[Tabs] onChange fire even if value didn't change #22378

jjoselv opened this issue Aug 27, 2020 · 13 comments · Fixed by #22381
Labels
bug 🐛 Something doesn't work component: tabs This is the name of the generic UI component, not the React module! ready to take Help wanted. Guidance available. There is a high chance the change will be accepted

Comments

@jjoselv
Copy link
Contributor

jjoselv commented Aug 27, 2020

Given option selectionFollowsFocus is true on Tabs component, when a Tab is clicked, onChange handler runs twice (one on focus and the other on clicked).

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Given option selectionFollowsFocus is true on Tabs component, when a Tab is clicked, onChange handler runs twice (one on focus and the other on clicked).

Expected Behavior 🤔

It should only run once (check if already selected maybe).

Steps to Reproduce 🕹

Click on each Tab
https://codesandbox.io/s/friendly-sound-30c2y?file=/index.js

Steps:

  1. Click on each Tab
Tech Version
Material-UI v4.11.0
React 16.13.1
React DOM 16.13.1
Browser Chrome 85
@jjoselv jjoselv added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 27, 2020
@eps1lon eps1lon added bug 🐛 Something doesn't work component: tabs This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 27, 2020
@eps1lon
Copy link
Member

eps1lon commented Aug 27, 2020

Thanks for the report. The underlying issue is that onChange is called whether the value changes or not: you can repeatedly click on the selected tab and onChange will be called (regardless of selectionFollowsFocus).

@eps1lon eps1lon changed the title [Tab][Tabs] Tabs with option selectionFollowsFocus, when Tab clicked, OnChange handlers runs twice (one on focus and the other on clicked) [Tabs] onChange fire even if value didn't change Aug 27, 2020
@eps1lon eps1lon self-assigned this Aug 27, 2020
@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

May I provide a PR?

@eps1lon eps1lon assigned jjoselv and unassigned eps1lon Aug 27, 2020
@eps1lon
Copy link
Member

eps1lon commented Aug 27, 2020

May I provide a PR?

Sure, go for it. Don't forget to add a test.

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

Sure

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

Let me check for Contribution Guidelines

@oliviertassinari
Copy link
Member

@jjoselv #20361 might help as a prior change in the same direction (sorry that the test isn't very descriptive)

@oliviertassinari oliviertassinari added the ready to take Help wanted. Guidance available. There is a high chance the change will be accepted label Aug 27, 2020
@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

Thanks olivier!

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

@oliviertassinari @eps1lon I'm having trouble making the test pass (select variable is not getting value true)
Can you take a look? github.com:jjoselv/material-ui.git at fix-on-change-fire-even-if-value-did-not-change

I run yarn run test inside material-ui/packages/material-ui

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

Maybe I have to let it elapse some time between clicks?

@oliviertassinari
Copy link
Member

@jjoselv The value is controlled in your test, it stays on 0, even after the first click. Click on the first item instead

      fireEvent.click(getAllByRole('tab')[1]);
      expect(handleChange.callCount).to.equal(1);
      expect(handleChange.args[0][1]).to.equal(1);

      fireEvent.click(getAllByRole('tab')[0]);
      expect(handleChange.callCount).to.equal(1);

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

@oliviertassinari The thing is that I want to do the following test:
Initial Conditions
Tab 0 selected.

Act -> Click on Tab 1
Assert that handle change has been called once

Act -> Click on Tab 1 (again)
Assert that handle change has not been called (apart from that first time)

And that way the onChange handle does not run for already selected elements

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

I see now, you code checks the same but in a different way. Ok, I'll submit your proposal 👍

@jjoselv
Copy link
Contributor Author

jjoselv commented Aug 27, 2020

#22381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: tabs This is the name of the generic UI component, not the React module! ready to take Help wanted. Guidance available. There is a high chance the change will be accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants