Skip to content

Commit

Permalink
fix(timepicker): add ng-touched class on blur (#3956)
Browse files Browse the repository at this point in the history
Fixes #3852
  • Loading branch information
rmirville committed May 12, 2021
1 parent ae2e6b1 commit 21f8f05
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 3 deletions.
231 changes: 228 additions & 3 deletions src/timepicker/timepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ describe('ngb-timepicker', () => {

describe('forms', () => {

it('should work with template-driven form validation', fakeAsync(() => {
it('should work with template-driven form validation - valid status', fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control" required></ngb-timepicker>
Expand All @@ -723,7 +723,85 @@ describe('ngb-timepicker', () => {
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-invalid');
}));

it('should work with template-driven form validation when meridian is true', fakeAsync(() => {
it('should work with template-driven form validation - visited status - hours', fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const hourInput = fixture.debugElement.query(By.css('.ngb-tp-hour input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

hourInput.triggerEventHandler('focus', {});
fixture.detectChanges();
hourInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with template-driven form validation - visited status - minutes', fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const minuteInput = fixture.debugElement.query(By.css('.ngb-tp-minute input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

minuteInput.triggerEventHandler('focus', {});
fixture.detectChanges();
minuteInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with template-driven form validation - visited status - seconds', fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control" seconds="true" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const secondInput = fixture.debugElement.query(By.css('.ngb-tp-second input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

secondInput.triggerEventHandler('focus', {});
fixture.detectChanges();
secondInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with template-driven form validation when meridian is true - valid status', fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control"></ngb-timepicker>
Expand All @@ -745,7 +823,88 @@ describe('ngb-timepicker', () => {
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-invalid');
}));

it('should work with model-driven form validation', () => {
it('should work with template-driven form validation when meridian is true - visited status - hours',
fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control"></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const hourInput = fixture.debugElement.query(By.css('.ngb-tp-hour input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

hourInput.triggerEventHandler('focus', {});
fixture.detectChanges();
hourInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with template-driven form validation when meridian is true - visited status - minutes',
fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control"></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const minuteInput = fixture.debugElement.query(By.css('.ngb-tp-minute input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

minuteInput.triggerEventHandler('focus', {});
fixture.detectChanges();
minuteInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with template-driven form validation when meridian is true - visited status - seconds',
fakeAsync(() => {
const html = `
<form>
<ngb-timepicker [(ngModel)]="model" name="control" seconds="true"></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
const secondInput = fixture.debugElement.query(By.css('.ngb-tp-second input'));

fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

secondInput.triggerEventHandler('focus', {});
fixture.detectChanges();
secondInput.triggerEventHandler('blur', {});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
}));

it('should work with model-driven form validation - valid status', () => {
const html = `
<form [formGroup]="form">
<ngb-timepicker formControlName="control" required></ngb-timepicker>
Expand All @@ -766,6 +925,72 @@ describe('ngb-timepicker', () => {
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-invalid');
});

it('should work with model-driven form validation - visited status - hours', () => {
const html = `
<form [formGroup]="form">
<ngb-timepicker formControlName="control" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
fixture.detectChanges();
const hourInput = fixture.debugElement.query(By.css('.ngb-tp-hour input'));

expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

hourInput.triggerEventHandler('focus', {});
fixture.detectChanges();
hourInput.triggerEventHandler('blur', {});
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
});

it('should work with model-driven form validation - visited status - minutes', () => {
const html = `
<form [formGroup]="form">
<ngb-timepicker formControlName="control" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
fixture.detectChanges();
const minuteInput = fixture.debugElement.query(By.css('.ngb-tp-minute input'));

expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

minuteInput.triggerEventHandler('focus', {});
fixture.detectChanges();
minuteInput.triggerEventHandler('blur', {});
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
});

it('should work with model-driven form validation - visited status - seconds', () => {
const html = `
<form [formGroup]="form">
<ngb-timepicker formControlName="control" seconds="true" required></ngb-timepicker>
</form>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
fixture.detectChanges();
const secondInput = fixture.debugElement.query(By.css('.ngb-tp-second input'));

expect(getTimepicker(compiled)).toHaveCssClass('ng-untouched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-touched');

secondInput.triggerEventHandler('focus', {});
fixture.detectChanges();
secondInput.triggerEventHandler('blur', {});
fixture.detectChanges();
expect(getTimepicker(compiled)).toHaveCssClass('ng-touched');
expect(getTimepicker(compiled)).not.toHaveCssClass('ng-untouched');
});

it('should propagate model changes only if valid - no seconds', () => {
const html = `<ngb-timepicker [(ngModel)]="model"></ngb-timepicker>`;

Expand Down
5 changes: 5 additions & 0 deletions src/timepicker/timepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const FILTER_REGEX = /[^0-9]/g;
maxlength="2" inputmode="numeric" placeholder="HH" i18n-placeholder="@@ngb.timepicker.HH"
[value]="formatHour(model?.hour)" (change)="updateHour($any($event).target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Hours" i18n-aria-label="@@ngb.timepicker.hours"
(blur)="handleBlur()"
(input)="formatInput($any($event).target)"
(keydown.ArrowUp)="changeHour(hourStep); $event.preventDefault()"
(keydown.ArrowDown)="changeHour(-hourStep); $event.preventDefault()">
Expand All @@ -61,6 +62,7 @@ const FILTER_REGEX = /[^0-9]/g;
maxlength="2" inputmode="numeric" placeholder="MM" i18n-placeholder="@@ngb.timepicker.MM"
[value]="formatMinSec(model?.minute)" (change)="updateMinute($any($event).target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Minutes" i18n-aria-label="@@ngb.timepicker.minutes"
(blur)="handleBlur()"
(input)="formatInput($any($event).target)"
(keydown.ArrowUp)="changeMinute(minuteStep); $event.preventDefault()"
(keydown.ArrowDown)="changeMinute(-minuteStep); $event.preventDefault()">
Expand All @@ -83,6 +85,7 @@ const FILTER_REGEX = /[^0-9]/g;
maxlength="2" inputmode="numeric" placeholder="SS" i18n-placeholder="@@ngb.timepicker.SS"
[value]="formatMinSec(model?.second)" (change)="updateSecond($any($event).target.value)"
[readOnly]="readonlyInputs" [disabled]="disabled" aria-label="Seconds" i18n-aria-label="@@ngb.timepicker.seconds"
(blur)="handleBlur()"
(input)="formatInput($any($event).target)"
(keydown.ArrowUp)="changeSecond(secondStep); $event.preventDefault()"
(keydown.ArrowDown)="changeSecond(-secondStep); $event.preventDefault()">
Expand Down Expand Up @@ -264,6 +267,8 @@ export class NgbTimepicker implements ControlValueAccessor,

formatMinSec(value?: number) { return padNumber(isNumber(value) ? value : NaN); }

handleBlur() { this.onTouched(); }

get isSmallSize(): boolean { return this.size === 'small'; }

get isLargeSize(): boolean { return this.size === 'large'; }
Expand Down

0 comments on commit 21f8f05

Please sign in to comment.