Skip to content

Commit bcaba87

Browse files
committed
fix(date-picker): move isReaquired to ngAfterViewChecked, #6471
1 parent 21ccad2 commit bcaba87

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

-4
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

+6-1
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

+18-2
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
* Gets/Sets the `IgxDatePickerComponent` label.
149151
* @remarks
@@ -895,6 +897,18 @@ export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor
895897
}
896898
}
897899

900+
public ngAfterViewChecked() {
901+
// if one sets mode at run time this forces initialization of new igxInputGroup
902+
// As a result a new igxInputDirective is initialized too. In ngAfterViewInit of
903+
// the new directive isRequired of the igxInputGroup is set again. However
904+
// ngAfterViewInit of date picker is not called again and we may finish with wrong
905+
// isRequired in igxInputGroup. This is why we should set it her, only when needed
906+
if (this.inputGroup && this.inputGroup.isRequired !== this.required) {
907+
this.inputGroup.isRequired = this.required;
908+
this._cdr.detectChanges();
909+
}
910+
}
911+
898912
protected onStatusChanged() {
899913
if ((this._ngControl.control.touched || this._ngControl.control.dirty) &&
900914
(this._ngControl.control.validator || this._ngControl.control.asyncValidator)) {
@@ -905,6 +919,8 @@ export class IgxDatePickerComponent implements IDatePicker, ControlValueAccessor
905919
input.valid = this._ngControl.valid ? IgxInputState.INITIAL : IgxInputState.INVALID;
906920
}
907921
}
922+
923+
this.inputGroup.isRequired = this.required;
908924
}
909925

910926
/**

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,14 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
134134
if (typeof value === 'boolean') {
135135
this.nativeElement.required = this.inputGroup.isRequired = value;
136136

137-
if (value && !this.nativeElement.checkValidity() && this.shouldCheckValidity) {
137+
if (value && !this.nativeElement.checkValidity()) {
138138
this.valid = IgxInputState.INVALID;
139139
} else {
140140
this.valid = IgxInputState.INITIAL;
141141
}
142142
}
143143
}
144144

145-
/** @hidden @internal */
146-
@Input()
147-
public shouldCheckValidity = true;
148-
149145
/**
150146
* Gets whether the igxInput is required.
151147
*
@@ -212,7 +208,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
212208
if (!this.ngControl.valid) {
213209
this.valid = IgxInputState.INVALID;
214210
}
215-
} else if (this._hasValidators() && !this.nativeElement.checkValidity() && this.shouldCheckValidity) {
211+
} else if (this._hasValidators() && !this.nativeElement.checkValidity()) {
216212
this.valid = IgxInputState.INVALID;
217213
}
218214
}
@@ -403,7 +399,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
403399
* @internal
404400
*/
405401
private checkValidity() {
406-
if (!this.ngControl && this._hasValidators() && this.shouldCheckValidity) {
402+
if (!this.ngControl && this._hasValidators()) {
407403
this.valid = this.nativeElement.checkValidity() ? IgxInputState.VALID : IgxInputState.INVALID;
408404
}
409405
}

0 commit comments

Comments
 (0)