-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Manage input value without using the focus #4486
Conversation
These are the results for the performance tests:
|
9e1da61
to
45ba6a1
Compare
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
dbe9a35
to
d730ac2
Compare
It appears that adding To handle case when user type onChange={(x)=>{
setValue(x);
if(validate(x)){
onValidate(x)
}
} As long as they do not modify the date from the |
packages/x-date-pickers/src/DesktopDatePicker/DesktopDatePicker.test.tsx
Outdated
Show resolved
Hide resolved
packages/x-date-pickers/src/DesktopDateTimePicker/DesktopDateTimePicker.test.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior looks good to me
It's probably worth reproducing by hand each issue linked in the PR description to see if the behavior is now good.
And link those issues as fixed to have GitHub close them automatically
I added the "Fix" before each issue. If someone else also wants to experiment the bugs with this PR fix, I linked a codesandbox for each issue. The codesandbox is the initial minimal reproduction of the issue, with the package generated by this PR |
DatePicker in Seems like it was "fixed" but in version 5.0.4 which I use it's still prevalent |
Hi @Lukortech, could you open an issue with a code sandbox demo of what you want to achieve and what is the unexpected behavior? Otherwise we will not be able to keep track of your bug |
Related issues (are they fixed?)
The original problem
The
useMaskedInput
track theinput
focus state for the following reason (a comment can be found here https://github.com/mui/material-ui/pull/25972/files )If a user enters
'01/01/1'
date adapters consider it as the 1st of January 0001. ThenonChange
is called withnew Date(1,1,1)
. Since we encourage developers to do the following, the TextFieldvalue
will be set to'01/01/0001'
, leading to really difficult editing behavior (impossible to delete a single character in the year).Why it is not a good solution
The assumption is
But this behavior lets the components have two different renderings for the same props, leading to bug #4479 in which the dev wants to implement the following feature: "When I press Del it erases the date".
The straightforward solution is to use
onKeyDow
and set the value tonull
if Del is pressed. But if the input has the focus the modification will be ignored by the component, and so the previous value will still be visible.Proposed solution
In this PR I remove the focus management since it leads to inconsistencies between the controlled value and the displayed one
Add
acceptInconsistentFormat
prop with the default set tofalse
. an input is considered as aninconsistentFormat
if parsing and formatting is not the identity function. For example with inputFormatdd/MM/YYYY
, it makes the following transformationsSo with
acceptInconsistentFormat=false
the first input will not callonChange
Why it does not work
We need to be able to desync the displayed text from the date input. Because we need to be able to type partial date which corresponds to invalid date
When filling the date we can wait to reach a complete one with the
acceptInconsistentFormat=true
But if the user entered a date (for example
'01/01/1998'
) and deletes a character, it will now be'01/01/199'
which should callonChange
to say that the date is now invalidNew solution
I observed that currently the
onChange
is called even with date such as01/01/19
which is not formatted because the component has the focus. Now it will still callonChange
with date01/01/19
but not format the new date, because the current text already corresponds to the new date