Skip to content

Commit add44ba

Browse files
committed
fix(datepicker): Fix validation for user template #7800
1 parent d802951 commit add44ba

File tree

3 files changed

+153
-70
lines changed

3 files changed

+153
-70
lines changed

projects/igniteui-angular/src/lib/date-picker/date-picker.component.spec.ts

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewChild, ElementRef, EventEmitter } from '@angular/core';
1+
import { Component, ViewChild, ElementRef, EventEmitter, QueryList } from '@angular/core';
22
import { async, fakeAsync, TestBed, tick, flush, ComponentFixture } from '@angular/core/testing';
33
import { FormsModule, FormGroup, FormBuilder, ReactiveFormsModule, Validators, NgControl } from '@angular/forms';
44
import { By } from '@angular/platform-browser';
@@ -14,6 +14,7 @@ import { IgxButtonModule } from '../directives/button/button.directive';
1414
import { IgxCalendarModule } from '../calendar/public_api';
1515
import { InteractionMode } from '../core/enums';
1616
import { DateRangeType } from '../core/dates/dateRange';
17+
import { IgxIconModule } from '../icon/public_api';
1718
import {
1819
OverlayCancelableEventArgs,
1920
OverlayClosingEventArgs,
@@ -40,7 +41,7 @@ describe('IgxDatePicker', () => {
4041
IgxDatePickerDropdownButtonsComponent
4142
],
4243
imports: [IgxDatePickerModule, FormsModule, ReactiveFormsModule, NoopAnimationsModule, IgxInputGroupModule, IgxCalendarModule,
43-
IgxButtonModule, IgxTextSelectionModule]
44+
IgxButtonModule, IgxTextSelectionModule, IgxIconModule]
4445
})
4546
.compileComponents();
4647
}));
@@ -1283,11 +1284,13 @@ describe('IgxDatePicker', () => {
12831284
let fixture: ComponentFixture<IgxDatePickerReactiveFormComponent>;
12841285
let datePickerOnChangeComponent: IgxDatePickerComponent;
12851286
let datePickerOnBlurComponent: IgxDatePickerComponent;
1287+
let datePickerTemplateIGComponent: IgxDatePickerComponent;
12861288

12871289
beforeEach(() => {
12881290
fixture = TestBed.createComponent(IgxDatePickerReactiveFormComponent);
12891291
datePickerOnChangeComponent = fixture.componentInstance.datePickerOnChangeComponent;
12901292
datePickerOnBlurComponent = fixture.componentInstance.datePickerOnBlurComponent;
1293+
datePickerTemplateIGComponent = fixture.componentInstance.datePickerTemplateIGComponent;
12911294
fixture.detectChanges();
12921295
});
12931296

@@ -1352,6 +1355,45 @@ describe('IgxDatePicker', () => {
13521355
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
13531356
}));
13541357

1358+
it('Should set date picker status to invalid if date is included in disabledDates range and user pass a template', fakeAsync(() => {
1359+
datePickerTemplateIGComponent.disabledDates = [{ type: DateRangeType.Before, dateRange: [new Date()] }];
1360+
const inputDirective = datePickerTemplateIGComponent.inputDirective;
1361+
1362+
const today = new Date();
1363+
datePickerTemplateIGComponent.value = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
1364+
fixture.detectChanges();
1365+
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
1366+
1367+
datePickerTemplateIGComponent.value = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
1368+
fixture.detectChanges();
1369+
expect(inputDirective.valid).toEqual(IgxInputState.INITIAL);
1370+
1371+
datePickerTemplateIGComponent.disabledDates = [{ type: DateRangeType.Before,
1372+
dateRange: [new Date(today.getFullYear(), today.getMonth(), today.getDate() + 2)] }];
1373+
fixture.detectChanges();
1374+
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
1375+
}));
1376+
1377+
it('Should set date picker status to invalid on blur when pass or change a template', fakeAsync(() => {
1378+
datePickerTemplateIGComponent.disabledDates = [{ type: DateRangeType.Before, dateRange: [new Date()] }];
1379+
const templateInputDirective = datePickerTemplateIGComponent.inputDirective;
1380+
const templateInput = templateInputDirective.nativeElement;
1381+
templateInput.dispatchEvent(new Event('blur'));
1382+
fixture.detectChanges();
1383+
expect(templateInputDirective.valid).toEqual(IgxInputState.INVALID);
1384+
1385+
fixture.componentInstance.useCustomTemplate = false;
1386+
fixture.detectChanges();
1387+
// obtain the default template input directive & input
1388+
const inputDirective = datePickerTemplateIGComponent.inputDirective;
1389+
const input = inputDirective.nativeElement;
1390+
expect(inputDirective.valid).toEqual(IgxInputState.INITIAL);
1391+
1392+
input.dispatchEvent(new Event('blur'));
1393+
fixture.detectChanges();
1394+
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
1395+
}));
1396+
13551397
// Bug #6025 Date picker does not disable in reactive form
13561398
it('Should disable when form is disabled', () => {
13571399
const formGroup: FormGroup = fixture.componentInstance.reactiveForm;
@@ -1401,7 +1443,21 @@ describe('IgxDatePicker', () => {
14011443

14021444
it('should initialize date picker with required correctly', () => {
14031445
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1404-
datePicker['inputGroup'] = inputGroup;
1446+
datePicker['_inputGroup'] = inputGroup;
1447+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
1448+
ngModel.control.validator = () => ({ required: true });
1449+
datePicker.ngOnInit();
1450+
datePicker.ngAfterViewInit();
1451+
datePicker.ngAfterViewChecked();
1452+
1453+
expect(datePicker).toBeDefined();
1454+
expect(inputGroup.isRequired).toBeTruthy();
1455+
});
1456+
1457+
it('should initialize date picker with required correctly with user template input-group', () => {
1458+
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1459+
datePicker['_inputGroupUserTemplate'] = inputGroup;
1460+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
14051461
ngModel.control.validator = () => ({ required: true });
14061462
datePicker.ngOnInit();
14071463
datePicker.ngAfterViewInit();
@@ -1413,7 +1469,8 @@ describe('IgxDatePicker', () => {
14131469

14141470
it('should update inputGroup isRequired correctly', () => {
14151471
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1416-
datePicker['inputGroup'] = inputGroup;
1472+
datePicker['_inputGroup'] = inputGroup;
1473+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
14171474
datePicker.ngOnInit();
14181475
datePicker.ngAfterViewInit();
14191476
datePicker.ngAfterViewChecked();
@@ -1581,6 +1638,18 @@ export class IgxDatePickerOpeningComponent {
15811638
<form [formGroup]="reactiveForm">
15821639
<igx-date-picker formControlName="datePickerOnChange" #datePickerOnChangeComponent></igx-date-picker>
15831640
<igx-date-picker formControlName="datePickerOnBlur" #datePickerOnBlurComponent></igx-date-picker>
1641+
<igx-date-picker formControlName="datePickerIGTemplate" #datePickerTemplateIGComponent>
1642+
<ng-template *ngIf="useCustomTemplate" igxDatePickerTemplate let-openDialog="openDialog" let-value="value"
1643+
let-displayData="displayData">
1644+
<igx-input-group>
1645+
<label igxLabel>Date</label>
1646+
<input igxInput [value]="displayData"/>
1647+
<igx-suffix>
1648+
<igx-icon>today</igx-icon>
1649+
</igx-suffix>
1650+
</igx-input-group>
1651+
</ng-template>
1652+
</igx-date-picker>
15841653
</form>
15851654
`
15861655
})
@@ -1591,13 +1660,17 @@ class IgxDatePickerReactiveFormComponent {
15911660
@ViewChild('datePickerOnBlurComponent', { read: IgxDatePickerComponent, static: true })
15921661
public datePickerOnBlurComponent: IgxDatePickerComponent;
15931662

1594-
reactiveForm: FormGroup;
1663+
@ViewChild('datePickerTemplateIGComponent', { read: IgxDatePickerComponent, static: true })
1664+
public datePickerTemplateIGComponent: IgxDatePickerComponent;
15951665

1666+
reactiveForm: FormGroup;
1667+
public useCustomTemplate = true;
15961668
constructor(fb: FormBuilder) {
15971669
const date = new Date(2000, 10, 15);
15981670
this.reactiveForm = fb.group({
15991671
datePickerOnChange: [date, Validators.required],
1600-
datePickerOnBlur: [date, { updateOn: 'blur', validators: Validators.required }]
1672+
datePickerOnBlur: [date, { updateOn: 'blur', validators: Validators.required }],
1673+
datePickerIGTemplate: [date, Validators.required]
16011674
});
16021675
}
16031676
}

0 commit comments

Comments
 (0)