-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] Reset the value of the input field using controlled value #10424
Comments
Thank you for creating this issue. 🙏 A possible solution could be to add an optional ref equality check: diff --git a/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts b/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts
index 6cb85178f..7a9b504c9 100644
--- a/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts
+++ b/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts
@@ -310,7 +310,8 @@ export const usePickerValue = <
if (
inValue !== undefined &&
(dateState.lastControlledValue === undefined ||
- !valueManager.areValuesEqual(utils, dateState.lastControlledValue, inValue))
+ !valueManager.areValuesEqual(utils, dateState.lastControlledValue, inValue) ||
+ dateState.lastControlledValue !== inValue)
) {
const isUpdateComingFromPicker = valueManager.areValuesEqual(utils, dateState.draft, inValue); This would allow re-setting the picker value when the bound value reference changes, i.e. by doing Are there any problems that I don't see with this or better suggestions? 🤔 |
hi @LukasTy , Thank you for quick response. I have already tried to set the state like this but that didn't help. Do I need to make any other change as well except this setState ? If you want me to make a change in some package file, that wont help, because before each deployment on server, it install the npm modules again,, so my change will be lost |
My question was more open-ended and targeted to any other team member who would take a look at this issue. |
Thank you. Really amazed by your quick responses. I am sorry, but I don't know how this patch thing work and I am afraid of sending it to the production. Is there any way that we can stop invalid dates in the textbox itself ? I mean not allow user to enter invalid numbers such as 13th month or 31st of November etc. ? Then we won't even need to reset it through refs. |
The initial version of fields did work like that, but it was changed with #7934 |
okay, is there any plan to merge this fix as well ? |
I'll try creating a PR this week. 👌 |
Thank you very much. Will keep an eye :) |
@satwinder8294 @LukasTy we would like to pick this up |
@gitstart Thank you for being interested in contributing to the Pickers codebase! 🙏 |
Thanks for getting back to us @LukasTy, we are willing to work on as many of these issues as you like. |
Another case to test and cover when fixing this: #10727. |
I just hit the same problem. I want to limit the range used to 3 years, so I want to programatically control the Any idea when we can expect that fixed? also, may I suggest that if it's difficult to fix now, you put a note in the doc saying that there is currently no "controlled" version of the component? I needed quite a bit of time to figure out that I was doing things right and that there was a bug in the library Thanks |
@gbataille given your use case, a Forcibly replacing the changed value with the one you need sounds like a bit strange UX. I think that relying on form validation and preventing form submission with validation errors present is a better UX for both regular users as well as visually impaired relying on a11y features. As for the issue, thank you for bumping it, we put it in the |
I quite agree for the UX side of what I'm doing (I mean that as you say it's not awesome). I did not think to change the |
We agreed to work on aligning the controlled behavior as close to the regular |
Steps to reproduce 🕹
Link to live example:
Steps:
1.
2.
3.
Current behavior 😯
I am using in my React + MUI application... My calendar date selections are working fine as expected. But the user is allowed to type invalid dates in the input field e.g 31st of November or 13th month of the year. if user types an invalid value in the input field then instead of updating the state value my code reset it to previous value by using the inputRefs.
It changes the value to previous one but when i open the calendar it again change the value(in input field) to invalid date.
Can you help how to handle this ?
Expected behavior 🤔
Expectation is that if the date is invalid, set it back to valid one programmatically both in calendar and the input field.
Context 🔦
as a work around, i have used key attribute and re-render on each invalid date. I would like to get a proper solution.
Your environment 🌎
npx @mui/envinfo
Order ID or Support key 💳 (optional)
No response
The text was updated successfully, but these errors were encountered: