Eonasdan / tempus-dominus

A powerful Date/time picker widget.
https://getdatepicker.com
MIT License
7.15k stars 4.4k forks source link

Error when trying to change AM/PM before selecting a time #2908

Open scoldwell opened 4 months ago

scoldwell commented 4 months ago

Prerequisites

Describe the issue

When using tempus dominus to collect a time only with the useCurrent option set to false, if the user tries to change the AM/PM before choosing a time the following error is thrown:

actions.ts:116 Uncaught TypeError: Cannot read properties of undefined (reading 'hours')
    at Actions.do (actions.ts:116:33)
    at actions.ts:31:14
    at event-emitter.ts:21:7
    at Array.forEach (<anonymous>)
    at EventEmitter.emit (event-emitter.ts:20:22)
    at Display._actionsClickEvent (index.ts:806:32)

This is coming from the following case block (this.dates.lastPicked is undefined):

    case ActionTypes.toggleMeridiem:
        this.manipulateAndSet(
          lastPicked,
          Unit.hours,
          this.dates.lastPicked.hours >= 12 ? -12 : 12
        );
        break;

StackBlitz fork

https://tempus-dominus-v6-simple-setup-y31bad.stackblitz.io

What operating system(s) are you seeing the problem on?

macOS

What browser(s) are you seeing the problem on?

Chrome

What version of are you using? You can find this information from the sample StackBlitz.

6.9.7

What your browser's locale? You can find this information from the sample StackBlitz.

en-US

scoldwell commented 3 months ago

I would be happy to fix this and submit a PR, but I'm not sure of the correct solution.