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

[Pickers] Fix showTodayButton not returning the current time #24650

Merged
merged 6 commits into from Jan 28, 2021

Conversation

anthonyraymond
Copy link
Contributor

@anthonyraymond anthonyraymond commented Jan 27, 2021

Problem

When clicking on the "Today" button in MobileDate[Time]Picker, the returned value is always the same => the moment at which the component was first mounted.

How to reproduce

Example: https://codesandbox.io/s/62sdn

  • Load the page
  • Open the DateTimePicker
  • Click on the "Now" button
  • Wait for 5 sec
  • Open the DateTimePicker
  • Click on the "Now" button
  • The date has not changed at all, because the useNow() always return the same value

Expected behaviour

The "Now" value should represent the current time when the user click the button, and not the time when the component was first mounted.

Describe the changes

Somehow, removing the useReft fix the issue, i suspect some hook cache magic being used somewhere in React, but i don't know enough about the Hook implementation so...

Here is a sandbox using the fix proposed in the PR: https://codesandbox.io/s/funny-liskov-8pdp7

There must be some React magic i don't completly understand, but wrapping the "now" in a React.useRef have the effect of now updating the value in the components
@mui-pr-bot
Copy link

mui-pr-bot commented Jan 27, 2021

Details of bundle changes

Generated by 🚫 dangerJS against 4ef58e9

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me whether we want these semantics everywhere.

As a first step, please add a test so that we can check if it makes sense to create two different implementations.

useNow might be used as default value of sorts and that shouldn't change over time. That might be implied by how it's used though.

@eps1lon eps1lon added the PR: needs test The pull request can't be merged label Jan 27, 2021
@oliviertassinari oliviertassinari added the component: pickers This is the name of the generic UI component, not the React module! label Jan 27, 2021
@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work and removed PR: needs test The pull request can't be merged labels Jan 27, 2021
@oliviertassinari oliviertassinari changed the title [Pickers] useNow hook was always returning the same value [Pickers] Fix showTodayButton not returning the current time Jan 27, 2021
@oliviertassinari oliviertassinari dismissed eps1lon’s stale review January 27, 2021 19:29

issue solved and test case added


expect(onCloseMock.callCount).to.equal(1);
expect(handleChange.callCount).to.equal(1);
expect(adapterToUse.getDiff(handleChange.args[0][0], start)).to.equal(10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

@oliviertassinari oliviertassinari merged commit 28b36a2 into mui:next Jan 28, 2021
@oliviertassinari
Copy link
Member

@anthonyraymond Thanks for raising the issue to our attention, it's half of the job done. Sebastian was definitely right about the useNow semantic for default values, we needed to keep a ref.

@anthonyraymond anthonyraymond deleted the patch-1 branch January 28, 2021 16:13
natac13 pushed a commit to natac13/material-ui that referenced this pull request Jan 30, 2021
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: pickers This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants