Skip to content

Commit 8b0a5b3

Browse files
committed
fix(datepicker): Fix validation for user template #7800
1 parent 0964234 commit 8b0a5b3

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
}));
@@ -1290,11 +1291,13 @@ describe('IgxDatePicker', () => {
12901291
let fixture: ComponentFixture<IgxDatePickerReactiveFormComponent>;
12911292
let datePickerOnChangeComponent: IgxDatePickerComponent;
12921293
let datePickerOnBlurComponent: IgxDatePickerComponent;
1294+
let datePickerTemplateIGComponent: IgxDatePickerComponent;
12931295

12941296
beforeEach(() => {
12951297
fixture = TestBed.createComponent(IgxDatePickerReactiveFormComponent);
12961298
datePickerOnChangeComponent = fixture.componentInstance.datePickerOnChangeComponent;
12971299
datePickerOnBlurComponent = fixture.componentInstance.datePickerOnBlurComponent;
1300+
datePickerTemplateIGComponent = fixture.componentInstance.datePickerTemplateIGComponent;
12981301
fixture.detectChanges();
12991302
});
13001303

@@ -1359,6 +1362,45 @@ describe('IgxDatePicker', () => {
13591362
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
13601363
}));
13611364

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

14091451
it('should initialize date picker with required correctly', () => {
14101452
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1411-
datePicker['inputGroup'] = inputGroup;
1453+
datePicker['_inputGroup'] = inputGroup;
1454+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
1455+
ngModel.control.validator = () => ({ required: true });
1456+
datePicker.ngOnInit();
1457+
datePicker.ngAfterViewInit();
1458+
datePicker.ngAfterViewChecked();
1459+
1460+
expect(datePicker).toBeDefined();
1461+
expect(inputGroup.isRequired).toBeTruthy();
1462+
});
1463+
1464+
it('should initialize date picker with required correctly with user template input-group', () => {
1465+
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1466+
datePicker['_inputGroupUserTemplate'] = inputGroup;
1467+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
14121468
ngModel.control.validator = () => ({ required: true });
14131469
datePicker.ngOnInit();
14141470
datePicker.ngAfterViewInit();
@@ -1420,7 +1476,8 @@ describe('IgxDatePicker', () => {
14201476

14211477
it('should update inputGroup isRequired correctly', () => {
14221478
const datePicker = new IgxDatePickerComponent(overlay, element, cdr, moduleRef, injector);
1423-
datePicker['inputGroup'] = inputGroup;
1479+
datePicker['_inputGroup'] = inputGroup;
1480+
datePicker['_inputDirectiveUserTemplates'] = new QueryList();
14241481
datePicker.ngOnInit();
14251482
datePicker.ngAfterViewInit();
14261483
datePicker.ngAfterViewChecked();
@@ -1588,6 +1645,18 @@ export class IgxDatePickerOpeningComponent {
15881645
<form [formGroup]="reactiveForm">
15891646
<igx-date-picker formControlName="datePickerOnChange" #datePickerOnChangeComponent></igx-date-picker>
15901647
<igx-date-picker formControlName="datePickerOnBlur" #datePickerOnBlurComponent></igx-date-picker>
1648+
<igx-date-picker formControlName="datePickerIGTemplate" #datePickerTemplateIGComponent>
1649+
<ng-template *ngIf="useCustomTemplate" igxDatePickerTemplate let-openDialog="openDialog" let-value="value"
1650+
let-displayData="displayData">
1651+
<igx-input-group>
1652+
<label igxLabel>Date</label>
1653+
<input igxInput [value]="displayData"/>
1654+
<igx-suffix>
1655+
<igx-icon>today</igx-icon>
1656+
</igx-suffix>
1657+
</igx-input-group>
1658+
</ng-template>
1659+
</igx-date-picker>
15911660
</form>
15921661
`
15931662
})
@@ -1598,13 +1667,17 @@ class IgxDatePickerReactiveFormComponent {
15981667
@ViewChild('datePickerOnBlurComponent', { read: IgxDatePickerComponent, static: true })
15991668
public datePickerOnBlurComponent: IgxDatePickerComponent;
16001669

1601-
reactiveForm: FormGroup;
1670+
@ViewChild('datePickerTemplateIGComponent', { read: IgxDatePickerComponent, static: true })
1671+
public datePickerTemplateIGComponent: IgxDatePickerComponent;
16021672

1673+
reactiveForm: FormGroup;
1674+
public useCustomTemplate = true;
16031675
constructor(fb: FormBuilder) {
16041676
const date = new Date(2000, 10, 15);
16051677
this.reactiveForm = fb.group({
16061678
datePickerOnChange: [date, Validators.required],
1607-
datePickerOnBlur: [date, { updateOn: 'blur', validators: Validators.required }]
1679+
datePickerOnBlur: [date, { updateOn: 'blur', validators: Validators.required }],
1680+
datePickerIGTemplate: [date, Validators.required]
16081681
});
16091682
}
16101683
}

0 commit comments

Comments
 (0)