-
Notifications
You must be signed in to change notification settings - Fork 13.5k
bug: Setting ion-datetime value programatically does not update the grid #26391
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
Comments
Thanks for the report. I can reproduce this behavior, but things appear to be working as intended. When the value changes asynchronously, we intentionally do not jump the datetime to that selected value. The reason is that if a user is swiping through January 2022 and the applications sets the value to March 2021, jumping the calendar view to March can be disorienting for the user since the component changed without them doing anything. In other words, once the datetime picker has been rendered for the first time, changes to the "working" state of the datetime must be done by user gestures. The working state in this case is the month that the user is viewing. Is there a reason why you need the date picker to jump back to the selected value? |
I am using an When setting the value programmatically, the value of the datetime and display text of the datetime button change as expected. But if the user clicks on the button, the displayed month is that of the initial (or last displayed) value, not the currently set value. This goes against against the user's expectation, because for the user the datetime picker is a completely new one each time the button is clicked. I agree that the displayed month should never be changed while the picker is visible. As far as I can tell, |
As an addition, this issue not only affects the calendar grid, it also affects wheels, where it is arguably even more of an issue. To give an example: 2023 is highlighted, even though the current value is 2025, and even though the user never actually interacted with this input before. If the user now wants to change the value to 2023, they can't, because clicking the already selected year doesn't register as an update. Instead, they have to select a different year, then select 2023 again. This is slightly different to the calendar view, where at least the correct date is highlighted, just the wrong month is shown. But the underlying issue is the same, in that an |
I fixed it by adding *ngIf="datevalue" , and it will show up with the preset value |
The only way I have managed to resolve this is by using the I placed an ion-button inside the ion-datetime tag with
I added css for the button to Then from the component.ts spoof a click event
...and this works, it resets the element with the value stored in dateValue. But this feels a bit hacky, to spoof a click from the contoller. I tried to import IonDateTime from @ionic/angular, and use ViewChild then tried to call But this did not work. |
Same strange behavior while using FormControl |
I'm experiencing the same issue. Ionic 7 and Angular 15. If we retrieve the users e.g. date of birth from the back end the correct date shows in the ion-date-button but when the button us 'clicked' ion-date shows the current date until the user interacts with the calendar / wheel. Then the date suddenly updates to the retrieved date. Very odd UX. |
Hi folks, here is a dev build if anyone is interested in testing the proposed fix for this issue:
Note that you may need to download Stackblitz apps before installing, as Stackblitz sometimes has issues with not installing the correct version. |
Issue number: Resolves #26391 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When updating the `value` programmatically on an `ion-datetime` after it has already been created: - With grid style: The selected date visually updates, but the calendar does not scroll to the newly selected month. - With wheel style: The selected date does not visually update, i.e. the wheels do not move to show the newly selected date. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Grid style datetimes now scroll to the selected date using the same animation as when clicking the next/prev month buttons. - This animation mirrors the behavior in both MUI and native iOS. See the [design doc](https://github.com/ionic-team/ionic-framework-design-documents/blob/main/projects/ionic-framework/components/datetime/0003-datetime-async-value.md) for more information and screen recordings. - The animation will not occur if the month/year did not change, or when the datetime is hidden. - Wheel style datetimes now visually update to the selected date. No animation occurs, also mirroring native. - The `parseDate` util has also had its type signatures updated to account for returning `undefined` when the date string is improperly formatted. This was missed when the util was refactored to support multiple date selection. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> - Docs PR: ionic-team/ionic-docs#3053 - While this can technically be considered a bug fix, we are merging it into a feature branch for safety; it's a fairly significant change to how datetime behaves, and may interfere with custom logic when updating a datetime's value async. - Jumping to the newly selected value is handled by replacing everything [here](https://github.com/ionic-team/ionic-framework/pull/27806/files#diff-4a407530c60e3cf72bcc11acdd21c4803a94bf47ea81b99e757db1c93d2735b8L364-L407) with `processValue()`. This covers both wheel and grid datetimes. - `activePartsClone` as a whole was also removed. It was added in #24244 to enable changing `activeParts` without triggering a rerender (and thus jumping to the new value) but since we now want to do that jump, the clone is no longer needed. - The animation code might be tricky to follow, so I recorded going through it: https://github.com/ionic-team/ionic-framework/assets/90629384/1afa5762-f493-441a-b662-f0429f2d86a7
This has been resolved via #27806 and the update will be available in a future minor release of Ionic. Thank you! |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Prerequisites
Ionic Framework Version
Current Behavior
If you change the value of an ion-datetime programatically, and then open the ion-datetime, the calendar grid still displays the month/year of the old date.
E.g. if you programatically change the date from today to May 5, 2022, and then open the ion-datetime, the calendar grid will show today's month instead of May 2022 (if you do then navigate to May 2022, you will see that May 5 is correctly selected, but it should have opened there in the first place)
Expected Behavior
Upon opening the ion-datetime, I would expect the calendar grid to initially display the month/year of the selected date.
Steps to Reproduce
The date picker shows the current month, instead of showing May 2022
(if you navigate back to May 2022, you will see that May 5 is highlighted as expected, but the date picker should have opened in that month to start with)
Code Reproduction URL
https://stackblitz.com/edit/angular-ramjpp-zz9vub?file=package.json
Ionic Info
Stackblitz
Additional Information
Prior to 6.3.2, the selected date was not updated either. This was reported in #25776, and I have used that as a template for this issue. Although the selected date does now get updated, the calendar grid does not open to the correct month, hence this new issue.
The text was updated successfully, but these errors were encountered: