Skip to content

Commit 0f6389f

Browse files
committed
feat(datepicker): Added functionality to add a custom class to specific dates. Supports empty custom class.
1 parent 01b0184 commit 0f6389f

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

components/datepicker/datepicker-inner.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ export class DatePickerInner implements OnInit {
9191
private onlyCurrentMonth:boolean;
9292
@Input()
9393
private shortcutPropagation:boolean;
94-
// todo: change type during implementation
9594
@Input()
96-
private customClass:any;
95+
private customClass:Array<{date:Date, mode:string, clazz:string}>;
9796
// todo: change type during implementation
9897
@Input()
9998
private dateDisabled:any;
@@ -232,15 +231,25 @@ export class DatePickerInner implements OnInit {
232231
private createDateObject(date:Date, format:string):any {
233232
let dateObject:any = {};
234233
dateObject.date = date;
234+
dateObject.date.setHours(0, 0, 0, 0);
235235
dateObject.label = this.dateFilter(date, format);
236236
dateObject.selected = this.compare(date, this.selectedDate) === 0;
237237
dateObject.disabled = this.isDisabled(date);
238238
dateObject.current = this.compare(date, new Date()) === 0;
239-
// todo: do it
240-
// dateObject.customClass = this.customClass({date: date, mode: this.datepickerMode}) || {};
239+
dateObject.customClass = this.getCustomClassForDate(dateObject.date);
241240
return dateObject;
242241
}
243242

243+
private getCustomClassForDate(date:Date) {
244+
if (!this.customClass) {
245+
return '';
246+
}
247+
var customClassObject:{date:Date, mode:string, clazz:string} = this.customClass.find(customClass => {
248+
return customClass.date.valueOf() === date.valueOf() && customClass.mode === this.datepickerMode;
249+
}, this);
250+
return customClassObject === undefined ? '' : customClassObject.clazz;
251+
};
252+
244253
private isDisabled(date:Date):boolean {
245254
// todo: implement dateDisabled attribute
246255
return ((this.minDate && this.compare(date, this.minDate) < 0) ||

components/datepicker/datepicker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ export class DatePicker implements ControlValueAccessor {
6464
@Input() public yearRange:number;
6565
@Input() public onlyCurrentMonth:boolean;
6666
@Input() public shortcutPropagation:boolean;
67+
@Input() public customClass:Array<{date:Date, mode:string, clazz:string}>;
6768
@Input() public get activeDate():Date {
6869
return this._activeDate || this._now;
6970
}
7071
// todo: change type during implementation
71-
public customClass:any;
72-
// todo: change type during implementation
7372
@Input() public dateDisabled:any;
7473

7574
constructor(@Self() public cd:NgModel) {

components/datepicker/daypicker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ const TEMPLATE_OPTIONS:any = {
1212
`,
1313
WEEK_ROW: `
1414
<td [hidden]="!datePicker.showWeeks" class="text-xs-center h6"><em>{{ weekNumbers[index] }}</em></td>
15-
<!-- [ngClass]="dtz.customClass" -->
1615
<td *ngFor="#dtz of rowz" class="text-xs-center" role="gridcell" [id]="dtz.uid">
17-
<button type="button" style="min-width:100%;" class="btn btn-sm"
16+
<button type="button" style="min-width:100%;" class="btn btn-sm {{dtz.customClass}}"
1817
*ngIf="!(datePicker.onlyCurrentMonth && dtz.secondary)"
1918
[ngClass]="{'btn-secondary': !dtz.selected && !datePicker.isActive(dtz), 'btn-info': dtz.selected || !dtz.selected && datePicker.isActive(dtz), disabled: dtz.disabled}"
2019
[disabled]="dtz.disabled"
@@ -32,9 +31,8 @@ const TEMPLATE_OPTIONS:any = {
3231
`,
3332
WEEK_ROW: `
3433
<td [hidden]="!datePicker.showWeeks" class="text-center h6"><em>{{ weekNumbers[index] }}</em></td>
35-
<!-- [ngClass]="dtz.customClass" -->
3634
<td *ngFor="#dtz of rowz" class="text-center" role="gridcell" [id]="dtz.uid">
37-
<button type="button" style="min-width:100%;" class="btn btn-default btn-sm"
35+
<button type="button" style="min-width:100%;" class="btn btn-default btn-sm {{dtz.customClass}}"
3836
*ngIf="!(datePicker.onlyCurrentMonth && dtz.secondary)"
3937
[ngClass]="{'btn-info': dtz.selected, active: datePicker.isActive(dtz), disabled: dtz.disabled}"
4038
[disabled]="dtz.disabled"

components/datepicker/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const DATEPICKER_DIRECTIVES:Array<any> = [DatePicker];
3333
- `minDate` (`?Date=null`) - oldest selectable date
3434
- `maxDate` (`?Date=null`) - latest selectable date
3535
- `dateDisabled` (`?Array<{date:Date, mode:string}>`) - array of disabled dates if `mode` is `day`, or years, etc.
36-
- `customClass` (`?Array<{date:Date, mode:string, class: strung}>`) - array of custom classes to be applied to targeted dates
36+
- `customClass` (`?Array<{date:Date, mode:string, clazz:string}>`) - array of custom css classes to be applied to targeted dates
3737
- `showWeeks` (`?boolean=true`) - if `false` week numbers will be hidden
3838
- `startingDay` (`?number=0`) - starting day of the week from 0-6 (0=Sunday, ..., 6=Saturday).
3939
- `initDate` (`?Date`) - default date to show if `ng-model` value is not specified

0 commit comments

Comments
 (0)