Skip to content

Commit 7f61ed4

Browse files
committed
fix(date-picker): move isReaquired to ngAfterViewChecked, #6471
1 parent 18a543f commit 7f61ed4

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

projects/igniteui-angular/src/lib/date-picker/date-picker.component.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
[disabled]="disabled"
1212
(blur)="onBlur($event)"
1313
readonly
14-
required="required"
15-
[shouldCheckValidity]="false"
1614
/>
1715
</igx-input-group>
1816
</ng-template>
@@ -38,8 +36,6 @@
3836
(wheel)="onWheel($event)"
3937
(input)="onInput($event)"
4038
(focus)="onFocus()"
41-
required="required"
42-
[shouldCheckValidity]="false"
4339
/>
4440
<igx-suffix *ngIf="!isEmpty" (click)="clear()">
4541
<igx-icon>clear</igx-icon>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,12 @@ describe('IgxDatePicker', () => {
13761376
onClosing: new EventEmitter<OverlayClosingEventArgs>()
13771377
};
13781378
element = {};
1379-
cdr = { markForCheck: () => {}, detach: () => {}, reattach: () => {}};
1379+
cdr = {
1380+
markForCheck: () => {},
1381+
detectChanges: () => {},
1382+
detach: () => {},
1383+
reattach: () => {}
1384+
};
13801385
moduleRef = {};
13811386
injector = { get: () => ngModel };
13821387
inputGroup = new IgxInputGroupComponent(null, null);

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
NgModuleRef,
1818
OnInit,
1919
AfterViewInit,
20-
Injector
20+
Injector,
21+
AfterViewChecked
2122
} from '@angular/core';
2223
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl, AbstractControl } from '@angular/forms';
2324
import {
@@ -143,7 +144,8 @@ const noop = () => { };
143144
}
144145
`]
145146
})
146-
export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor, EditorProvider, OnInit, AfterViewInit, OnDestroy {
147+
export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor,
148+
EditorProvider, OnInit, AfterViewInit, OnDestroy, AfterViewChecked {
147149
/**
148150
* An @Input property that sets the `IgxDatePickerComponent` label.
149151
* The default label is 'Date'.
@@ -945,6 +947,18 @@ export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor
945947
}
946948
}
947949

950+
public ngAfterViewChecked() {
951+
// if one sets mode at run time this forces initialization of new igxInputGroup
952+
// As a result a new igxInputDirective is initialized too. In ngAfterViewInit of
953+
// the new directive isRequired of the igxInputGroup is set again. However
954+
// ngAfterViewInit of date picker is not called again and we may finish with wrong
955+
// isRequired in igxInputGroup. This is why we should set it her, only when needed
956+
if (this.inputGroup && this.inputGroup.isRequired !== this.required) {
957+
this.inputGroup.isRequired = this.required;
958+
this._cdr.detectChanges();
959+
}
960+
}
961+
948962
protected onStatusChanged() {
949963
if ((this._ngControl.control.touched || this._ngControl.control.dirty) &&
950964
(this._ngControl.control.validator || this._ngControl.control.asyncValidator)) {
@@ -955,6 +969,8 @@ export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor
955969
input.valid = this._ngControl.valid ? IgxInputState.INITIAL : IgxInputState.INVALID;
956970
}
957971
}
972+
973+
this.inputGroup.isRequired = this.required;
958974
}
959975

960976
/**

projects/igniteui-angular/src/lib/directives/input/input.directive.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,14 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
108108
if (typeof value === 'boolean') {
109109
this.nativeElement.required = this.inputGroup.isRequired = value;
110110

111-
if (value && !this.nativeElement.checkValidity() && this.shouldCheckValidity) {
111+
if (value && !this.nativeElement.checkValidity()) {
112112
this.valid = IgxInputState.INVALID;
113113
} else {
114114
this.valid = IgxInputState.INITIAL;
115115
}
116116
}
117117
}
118118

119-
/** @hidden @internal */
120-
@Input()
121-
public shouldCheckValidity = true;
122-
123119
/**
124120
* Gets whether the igxInput is required.
125121
* ```typescript
@@ -175,7 +171,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
175171
if (!this.ngControl.valid) {
176172
this.valid = IgxInputState.INVALID;
177173
}
178-
} else if (this._hasValidators() && !this.nativeElement.checkValidity() && this.shouldCheckValidity) {
174+
} else if (this._hasValidators() && !this.nativeElement.checkValidity()) {
179175
this.valid = IgxInputState.INVALID;
180176
}
181177
}
@@ -341,7 +337,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
341337
}
342338

343339
private checkValidity() {
344-
if (!this.ngControl && this._hasValidators() && this.shouldCheckValidity) {
340+
if (!this.ngControl && this._hasValidators()) {
345341
this.valid = this.nativeElement.checkValidity() ? IgxInputState.VALID : IgxInputState.INVALID;
346342
}
347343
}

0 commit comments

Comments
 (0)