nrkno / core-components

Accessible and lightweight Javascript components
https://static.nrk.no/core-components/latest/
MIT License
117 stars 10 forks source link

core-datepicker: Not possible to choose todays date #633

Closed talepre closed 2 years ago

talepre commented 2 years ago

Todays date is marked as selected when no initial date is provided, making it impossible to click it without clicking another date first.

skjalgepalg commented 2 years ago

If no initial date is supplied through the timestamp attribute, the datepicker will fallback and set its date to Date.now(). When clicking todays date in the table, no datepicker.change-event will be fired. A datepicker.click.day -event is however dispatched on every click inside the table. This can be used to set the value of an empty input like this:

  // Handle click on today when input is empty
  document.addEventListener('datepicker.click.day', function (event) {
    if (event.target.id !== 'my-datepicker') return
    const input = document.getElementById('my-datepicker-output')
    if (input.value === '' && event.target.date.toDateString() === new Date().toDateString()) {
      input.value = event.target.date.toLocaleString()
    }
  })
skjalgepalg commented 2 years ago

In core-datepicker v4.0.0, no initial value will dispatch a change-event on selecting today More info and migration guides can be found in 9.0.0