Skip to content

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

Closed
4 of 7 tasks
charlesj1 opened this issue Nov 30, 2022 · 11 comments
Closed
4 of 7 tasks
Labels
package: core @ionic/core package type: bug a confirmed bug report

Comments

@charlesj1
Copy link

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x
  • Nightly

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

  1. Start with an ion-datetime, which defaults to today's date
  2. After 4 seconds, programatically update the date to May 5, 2022
  3. Open the date picker
    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.

@ionitron-bot ionitron-bot bot added the triage label Nov 30, 2022
@liamdebeasi liamdebeasi self-assigned this Dec 1, 2022
@liamdebeasi
Copy link
Contributor

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?

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Dec 1, 2022
@liamdebeasi liamdebeasi removed their assignment Dec 1, 2022
@ionitron-bot ionitron-bot bot removed the triage label Dec 1, 2022
@charlesj1
Copy link
Author

charlesj1 commented Dec 1, 2022

Thanks for the quick response. I'm trying to migrate from Ionic 5, so my scenarios are more like ion-datetime-button...i.e. I want to just show the selected date, and then tapping it should open the overlay.

First scenario is that I have arrow icons next to the date that let the user quickly go forward or back a day, with the option of tapping the date to open the overlay. The trouble is that if they use the arrow icons to go from Nov 30 to Dec 1, and then open the overlay, it is still set in November.
image

Second scenario is a profile page that displays their birthday, something like:
<ion-datetime value={this.birthday}></ion-datetime>.
They can switch their profile to a different family member, which changes the birthday. But if they then open the overlay, the month is still set to the last family member's birthday.

I guess I'm not using ion-datetime as intended in Ionic 6. Perhaps I should be creating a new ion-datetime with the appropriate date every time the overlay gets opened.

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Dec 1, 2022
@ReneZeidler
Copy link

I am using an ion-datetime-button with an ion-datetime inside a popover. The value of the datetime is bound to a form value, which can be changed programmatically (due to user actions elsewhere on the form).

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, ion-datetime already tracks whether it is hidden in a popover or modal. In my opinion, the best UX would be for the state of the datepicker to be reset when it is hidden, so that the next time it is opened, the month of the selected date is shown.

@ReneZeidler
Copy link

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:
I have an ion-datetime with presentation="year" bound to a form value with formControlName="myDate". It is inside a popover and bound to an ion-datetime-button.
myDate initially is set to a Date in 2023, which through user interactions in other parts of the form gets updated to 2025. Now, when the user clicks on the ion-datetime-button to change the date, they see this:

image

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 ion-datetime hidden in a modal or popover should update to reflect the current value when it is opened.

@mmamongy
Copy link

I fixed it by adding *ngIf="datevalue" , and it will show up with the preset value

@brandyscarney brandyscarney added the type: bug a confirmed bug report label Mar 27, 2023
@ionitron-bot ionitron-bot bot removed the triage label Mar 27, 2023
@cra1gs
Copy link

cra1gs commented Apr 21, 2023

The only way I have managed to resolve this is by using the reset() method of the ion-datetime.

I placed an ion-button inside the ion-datetime tag with (click)="datepicker.reset( )"

<ion-datetime  
  #datepicker   
   presentation="date"
  [(ngModel)]="dateValue" 
  [showDefaultButtons]="true"
  ngDefaultControl 
>
       <ion-button color="danger" class="datepicker_reset" #datepicker_reset (click)="datepicker.reset( dateValue )">Reset</ion-button>
</ion-datetime>

I added css for the button to display:none

Then from the component.ts spoof a click event

constructor( private elem: ElementRef )

this.elem.nativeElement.querySelector('.datepicker_reset').click()

...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
@ViewChild('datepicker') datepicker : IonDatetime;

then tried to call
this.datepicker.reset()

But this did not work.

@7eXx
Copy link

7eXx commented May 9, 2023

Same strange behavior while using FormControl

@connectMyClub
Copy link

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.

@averyjohnston
Copy link
Contributor

Hi folks, here is a dev build if anyone is interested in testing the proposed fix for this issue:

7.2.3-dev.11692214763.1b85c7df

Note that you may need to download Stackblitz apps before installing, as Stackblitz sometimes has issues with not installing the correct version.

averyjohnston added a commit that referenced this issue Aug 23, 2023
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
@averyjohnston
Copy link
Contributor

This has been resolved via #27806 and the update will be available in a future minor release of Ionic. Thank you!

@ionitron-bot
Copy link

ionitron-bot bot commented Sep 22, 2023

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: core @ionic/core package type: bug a confirmed bug report
Projects
None yet
Development

No branches or pull requests

9 participants