Skip to content
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

fix(material/radio): input not focused when clicking on touch target #26618

Merged
merged 1 commit into from
Feb 16, 2023
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
2 changes: 1 addition & 1 deletion src/material/radio/radio.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[class.mdc-form-field--align-end]="labelPosition == 'before'">
<div class="mdc-radio" [class.mdc-radio--disabled]="disabled">
<!-- Render this element first so the input is on top. -->
<div class="mat-mdc-radio-touch-target" (click)="_onInputInteraction($event)"></div>
<div class="mat-mdc-radio-touch-target" (click)="_onTouchTargetClick($event)"></div>
<input #input class="mdc-radio__native-control" type="radio"
[id]="inputId"
[checked]="checked"
Expand Down
9 changes: 9 additions & 0 deletions src/material/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,15 @@ describe('MDC-based MatRadio', () => {
}
});

it('should focus on underlying input element when clicking on the touch target', () => {
const input = radioDebugElements[0].nativeElement.querySelector('input');
expect(document.activeElement).not.toBe(input);

radioDebugElements[0].nativeElement.querySelector('.mat-mdc-radio-touch-target').click();
fixture.detectChanges();
expect(document.activeElement).toBe(input);
});

it('should not change focus origin if origin not specified', () => {
fruitRadioInstances[0].focus(undefined, 'mouse');
fruitRadioInstances[1].focus();
Expand Down
11 changes: 11 additions & 0 deletions src/material/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,17 @@ export abstract class _MatRadioButtonBase
}
}

/** Triggered when the user clicks on the touch target. */
_onTouchTargetClick(event: Event) {
this._onInputInteraction(event);

if (!this.disabled) {
// Normally the input should be focused already, but if the click
// comes from the touch target, then we might have to focus it ourselves.
this._inputElement.nativeElement.focus();
}
}

/** Sets the disabled state and marks for check if a change occurred. */
protected _setDisabled(value: boolean) {
if (this._disabled !== value) {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
// (undocumented)
_onInputClick(event: Event): void;
_onInputInteraction(event: Event): void;
_onTouchTargetClick(event: Event): void;
radioGroup: _MatRadioGroupBase<_MatRadioButtonBase>;
get required(): boolean;
set required(value: BooleanInput);
Expand Down