Skip to content

Commit aaa3b4d

Browse files
rastr5189yhnavein
authored andcommitted
fix(datepicker): Changed handling of value change
1 parent 6f0d491 commit aaa3b4d

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

demo/src/app/pages/modules/datepicker/datepicker.page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const exampleMinMaxTemplate = `
5454
<div class="ui left icon input">
5555
<i class="calendar icon"></i>
5656
<input suiDatepicker
57+
[(ngModel)]="date"
5758
[pickerMinDate]="min"
5859
[pickerMaxDate]="max"
5960
[pickerUseNativeOnMobile]="false">

src/modules/datepicker/directives/datepicker.directive.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class SuiDatepickerDirective
2929
public set selectedDate(date:Date | undefined) {
3030
this._selectedDate = date;
3131
this.onSelectedDateChange.emit(date);
32+
this.onViewValueChange.emit(date);
3233
}
3334

3435
private _mode:DatepickerMode;
@@ -104,6 +105,8 @@ export class SuiDatepickerDirective
104105
@Output("pickerValidatorChange")
105106
public onValidatorChange:EventEmitter<void>;
106107

108+
public onViewValueChange:EventEmitter<Date>;
109+
107110
constructor(public renderer:Renderer2,
108111
element:ElementRef,
109112
componentFactory:SuiComponentFactory,
@@ -125,6 +128,7 @@ export class SuiDatepickerDirective
125128

126129
this.onSelectedDateChange = new EventEmitter<Date>();
127130
this.onValidatorChange = new EventEmitter<void>();
131+
this.onViewValueChange = new EventEmitter<Date>();
128132

129133
this.mode = DatepickerMode.Datetime;
130134
}
@@ -181,11 +185,8 @@ export class SuiDatepickerDirective
181185
}
182186

183187
public writeValue(value:Date | undefined):void {
184-
this.selectedDate = value;
185-
186-
if (this.componentInstance) {
187-
this.componentInstance.service.selectedDate = value;
188-
}
188+
this._selectedDate = value;
189+
this.onViewValueChange.emit(value);
189190
}
190191

191192
@HostListener("keydown", ["$event"])

src/modules/datepicker/directives/input.directive.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class SuiDatepickerInputDirective {
9696
this.fallbackActive = false;
9797

9898
// Whenever the datepicker value updates, update the input text alongside it.
99-
this.datepicker.onSelectedDateChange.subscribe(() =>
99+
this.datepicker.onViewValueChange.subscribe(() =>
100100
this.updateValue(this.selectedDateString));
101101

102102
localizationService.onLanguageUpdate.subscribe(() =>
@@ -120,14 +120,18 @@ export class SuiDatepickerInputDirective {
120120

121121
if (!value) {
122122
// Delete the selected date if no date was entered manually.
123-
return this.datepicker.writeValue(undefined);
123+
this.datepicker.selectedDate = undefined;
124+
return;
124125
}
125126

126-
const parsed = this.parser.parse(value, this.datepicker.selectedDate);
127+
const parsed:Date = this.parser.parse(value, this.datepicker.selectedDate);
128+
127129
if (!isNaN(parsed.getTime()) && value === this.parser.format(parsed)) {
128-
return this.datepicker.writeValue(parsed);
130+
this.datepicker.selectedDate = parsed;
131+
return;
129132
}
130-
return this.datepicker.writeValue(undefined);
133+
134+
this.datepicker.selectedDate = undefined;
131135
}
132136

133137
@HostListener("focusout")

0 commit comments

Comments
 (0)