Skip to content

feat(material/radio): allow focus origin to be optional input in focus method #20911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/material/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ describe('MatRadio', () => {
let seasonRadioInstances: MatRadioButton[];
let weatherRadioInstances: MatRadioButton[];
let fruitRadioInstances: MatRadioButton[];
let fruitRadioNativeElements: HTMLElement[];
let fruitRadioNativeInputs: HTMLElement[];
let testComponent: StandaloneRadioButtons;

Expand All @@ -618,7 +619,7 @@ describe('MatRadio', () => {
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
.map(debugEl => debugEl.componentInstance);

const fruitRadioNativeElements = radioDebugElements
fruitRadioNativeElements = radioDebugElements
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
.map(debugEl => debugEl.nativeElement);

Expand Down Expand Up @@ -732,6 +733,14 @@ describe('MatRadio', () => {
}
});

it('should not change focus origin if origin not specified', () => {
fruitRadioInstances[0].focus(undefined, 'mouse');
fruitRadioInstances[1].focus();

expect(fruitRadioNativeElements[1].classList).toContain('cdk-focused');
expect(fruitRadioNativeElements[1].classList).toContain('cdk-mouse-focused');
});

it('should not add the "name" attribute if it is not passed in', () => {
const radio = fixture.debugElement.nativeElement.querySelector('#nameless input');
expect(radio.hasAttribute('name')).toBe(false);
Expand Down
10 changes: 7 additions & 3 deletions src/material/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FocusMonitor} from '@angular/cdk/a11y';
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {
BooleanInput,
coerceBooleanProperty,
Expand Down Expand Up @@ -516,8 +516,12 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
}

/** Focuses the radio button. */
focus(options?: FocusOptions): void {
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
focus(options?: FocusOptions, origin?: FocusOrigin): void {
if (origin) {
this._focusMonitor.focusVia(this._inputElement, origin, options);
} else {
this._inputElement.nativeElement.focus(options);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/radio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBa
_onInputChange(event: Event): void;
_onInputClick(event: Event): void;
protected _setDisabled(value: boolean): void;
focus(options?: FocusOptions): void;
focus(options?: FocusOptions, origin?: FocusOrigin): void;
ngAfterViewInit(): void;
ngOnDestroy(): void;
ngOnInit(): void;
Expand Down