From ec57c2754299664271328916c1ee605911b484ed Mon Sep 17 00:00:00 2001 From: Dinika Saxena Date: Tue, 7 Jun 2022 10:57:37 +0200 Subject: [PATCH] Allow selecting the last date user chose when they close the datetimepicker When the user selects a date and closes the datetimepicker window without clicking on the "Ok" (check-mark) button, it should be possible to set the input value to the date that the user selected before closing the window. Signed-off-by: Dinika Saxena --- projects/datetime-picker/src/lib/datetime-input.ts | 5 ++++- .../src/lib/datetime-picker.component.ts | 10 +++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/projects/datetime-picker/src/lib/datetime-input.ts b/projects/datetime-picker/src/lib/datetime-input.ts index 59ca3eb4..1b1eb5e4 100644 --- a/projects/datetime-picker/src/lib/datetime-input.ts +++ b/projects/datetime-picker/src/lib/datetime-input.ts @@ -121,6 +121,9 @@ export class NgxMatDatetimeInput implements ControlValueAccessor, OnDestroy, } private _value: D | null; + @Input() + emitSelectedValueOnCancel = false; + /** The minimum valid date. */ @Input() get min(): D | null { return this._min; } @@ -344,7 +347,7 @@ export class NgxMatDatetimeInput implements ControlValueAccessor, OnDestroy, _onFocus() { // Close datetime picker if opened if (this._datepicker && this._datepicker.opened) { - this._datepicker.cancel(); + this._datepicker.cancel(this.emitSelectedValueOnCancel); } } diff --git a/projects/datetime-picker/src/lib/datetime-picker.component.ts b/projects/datetime-picker/src/lib/datetime-picker.component.ts index ef6055e0..4c1ec188 100644 --- a/projects/datetime-picker/src/lib/datetime-picker.component.ts +++ b/projects/datetime-picker/src/lib/datetime-picker.component.ts @@ -377,9 +377,13 @@ export class NgxMatDatetimePicker implements OnDestroy, CanColor { } /** Cancel and close */ - public cancel(): void { - this._selected = this._rawValue; - this.close(); + public cancel(emitSelectedValueOnCancel: boolean): void { + if (emitSelectedValueOnCancel) { + this.ok(); + } else { + this._selected = this._rawValue; + this.close(); + } } /**