Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Mark the radio group touched after losing focus from any input #257

Merged
merged 6 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@skyux/core": "4.5.0",
"@skyux/datetime": "4.10.2",
"@skyux/docs-tools": "4.8.1",
"@skyux/forms": "4.16.2",
"@skyux/forms": "4.16.3",
"@skyux/http": "4.2.0",
"@skyux/i18n": "4.1.0",
"@skyux/indicators": "4.9.2",
Expand Down
13 changes: 13 additions & 0 deletions src/app/public/modules/radio/radio-group.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ describe('Radio group component (reactive)', function () {
expect(componentInstance.radioForm.dirty).toEqual(true);
}));

it('should mark as touched after losing focus', fakeAsync(function () {
fixture.detectChanges();

expect(componentInstance.radioForm.touched).toEqual(false);

const debugElement = fixture.debugElement.query(By.css('input[type="radio"]'));
expect(debugElement.nativeElement).toExist();
debugElement.triggerEventHandler('focus', {});
debugElement.triggerEventHandler('blur', {});

expect(componentInstance.radioForm.touched).toEqual(true);
}));

it('should update the ngModel properly when radio button is changed', fakeAsync(function () {
fixture.detectChanges();

Expand Down
8 changes: 8 additions & 0 deletions src/app/public/modules/radio/radio-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export class SkyRadioGroupComponent implements AfterContentInit, AfterViewInit,
this.onTouched();
this.writeValue(change.value);
});
radio.blur
.pipe(
takeUntil(this.ngUnsubscribe)
)
.subscribe(() => {
this.onTouched();
this.changeDetector.markForCheck();
});
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/app/public/modules/radio/radio.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ export class SkyRadioComponent implements OnDestroy, ControlValueAccessor {
}
}

/**
* Fires when users focus off a radio button.
*/
public get blur(): Observable<undefined> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just make this a public property, instead of masking the EventEmitter with an Observable getter.

/**
 * @internal
 */
public blur = new Subject<void>();

return this._blur;
}

/**
* Fires when users select a radio button.
*/
Expand Down Expand Up @@ -246,6 +253,7 @@ export class SkyRadioComponent implements OnDestroy, ControlValueAccessor {
}

private _change = new EventEmitter<SkyRadioChange>();
private _blur = new EventEmitter<undefined>();
private _checked = false;
private _checkedChange = new BehaviorSubject<boolean>(this._checked);
private _disabled: boolean = false;
Expand Down Expand Up @@ -307,6 +315,7 @@ export class SkyRadioComponent implements OnDestroy, ControlValueAccessor {

public onInputFocusChange(event: Event): void {
this.onTouchedCallback();
this._blur.emit();
}

/* istanbul ignore next */
Expand Down