mymth / vanillajs-datepicker

A vanilla JavaScript remake of bootstrap-datepicker for Bulma and other CSS frameworks
MIT License
751 stars 155 forks source link

Incorrect year is focused when `defaultViewDate` is set and `startView` is set to show decades #187

Open jochan opened 4 months ago

jochan commented 4 months ago

With the following configs, the decades view is initially shown but when you select the focused decade (1920 in this case), the subsequent focused year is the first year of the selected decade (1920) and not the year in defaultViewDate (1922).

const configs = {
  startView: 3,
  defaultViewDate: "3-10-1922"
}

At first glance, it seems to be due to the year and decade sharing the same view in YearsView, with navStep set incorrectly for the year view (but correctly for the decade view). https://github.com/mymth/vanillajs-datepicker/blob/master/js/picker/views/YearsView.js#L18

mymth commented 4 months ago

It looks like you misunderstand what defaultViewDate is.

The date to be focused when the date picker opens with no selected date(s). — https://mymth.github.io/vanillajs-datepicker/#/options?id=defaultviewdate

The default view date is not the start view date. The start view date is the last selected date, and if no dates are selected, then the default view date is used as a fallback.

I recommend trying selecting a date in a different century and decade from the defaultViewDate's and see what happens.

jochan commented 4 months ago

In my example, there is no selected date, so it's the defaultViewDate that should be start view date. The key to the issue here is having a startView of 3 (decades), which then makes the incorrect year selection.

It's even evident with no defaultViewDate set, when the current date expected. Here's an example using your online demo with only startView set as 3. You'd expect today's date (06/17/2024) to be focused when using the datepicker but it's actually 06/17/2020 that is focused. Am I still missing something here?

https://github.com/mymth/vanillajs-datepicker/assets/2473272/92a7b0ec-5be9-4280-a013-3c1dd2f61a82

mymth commented 4 months ago

I'm so sorry, it's me who misunderstood. I didn't read carefully and thought you were talking about the behavior after setting a date in 1920 as a selected date.

It's indeed a bug, and inconsistent with the behavior of other views. So, I'll include the fix for this in the next update.

Thank you for reporting the bug. And my apologies for being careless.