Skip to content

Commit 87dd285

Browse files
committed
Add optionnal nullable to datepicker for offer possibility to set date empty
1 parent 762c6c6 commit 87dd285

13 files changed

+19
-7
lines changed

bundles/ng2-datepicker.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundles/ng2-datepicker.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/fonts/OpenSans-Bold.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-BoldItalic.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-ExtraBold.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-ExtraBoldItalic.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-Italic.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-Light.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-LightItalic.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-Regular.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-Semibold.ttf

100755100644
File mode changed.

src/assets/fonts/OpenSans-SemiboldItalic.ttf

100755100644
File mode changed.

src/ng-datepicker/ng-datepicker.component.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface DatepickerOptions {
3131
readonly?: boolean;
3232
minDate?: Date;
3333
maxDate?: Date;
34+
nullable?: boolean;
35+
3436
}
3537

3638
/**
@@ -69,15 +71,16 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
6971

7072
private positions = ['bottom-left', 'bottom-right', 'top-left', 'top-right'];
7173

72-
innerValue: Date;
74+
innerValue?: Date;
7375
readonly: boolean;
74-
displayValue: string;
76+
displayValue?: string;
7577
displayFormat: string;
76-
date: Date;
78+
date?: Date;
7779
barTitle: string;
7880
barTitleFormat: string;
7981
minYear: number;
8082
maxYear: number;
83+
nullable: boolean;
8184
firstCalendarDay: number;
8285
view: string;
8386
years: { year: number; isThisYear: boolean }[];
@@ -148,13 +151,20 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
148151
this.date = new Date(`${splittedDate[2]}-${splittedDate[1]}-${splittedDate[0]}`);
149152
this.value = new Date(Date.UTC(this.date.getFullYear(), this.date.getMonth(), this.date.getDate()));
150153
this.init();
154+
}else if(this.nullable){
155+
this.date = null;
156+
this.value = null;
157+
this.innerValue = null;
158+
this.displayValue = '';
159+
this.init();
151160
}
152161
}
153162

154163
setOptions(): void {
155164
const today = new Date(); // this const was added because during my tests, I noticed that at this level this.date is undefined
156165
this.minYear = this.options && this.options.minYear || getYear(today) - 30;
157166
this.maxYear = this.options && this.options.maxYear || getYear(today) + 30;
167+
this.nullable = this.options && this.options.nullable || false;
158168
this.displayFormat = this.options && this.options.displayFormat || 'MMM D[,] YYYY';
159169
this.barTitleFormat = this.options && this.options.barTitleFormat || 'MMMM YYYY';
160170
this.firstCalendarDay = this.options && this.options.firstCalendarDay || 0;
@@ -273,15 +283,17 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
273283
this.isOpened = false;
274284
}
275285

276-
writeValue(val: Date) {
286+
writeValue(val?: Date) {
287+
277288
if (val) {
278289
this.date = val;
279290
this.innerValue = val;
280291
this.init();
281292
this.displayValue = this.innerValue ? format(this.innerValue, this.displayFormat, this.locale) : '';
282293
this.barTitle = format(startOfMonth(val), this.barTitleFormat, this.locale);
283294
} else {
284-
this.date = new Date();
295+
296+
this.date = this.nullable?null:new Date();
285297
this.innerValue = val;
286298
this.init();
287299
}

0 commit comments

Comments
 (0)