Skip to content

Commit a9a169c

Browse files
authored
fix(material/radio): input not focused when clicking on touch target (#26618)
Fixes that the radio button's internal `input` wasn't receiving focus when the touch target is clicked. Fixes #26487.
1 parent 3959fb9 commit a9a169c

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/material/radio/radio.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[class.mdc-form-field--align-end]="labelPosition == 'before'">
33
<div class="mdc-radio" [class.mdc-radio--disabled]="disabled">
44
<!-- Render this element first so the input is on top. -->
5-
<div class="mat-mdc-radio-touch-target" (click)="_onInputInteraction($event)"></div>
5+
<div class="mat-mdc-radio-touch-target" (click)="_onTouchTargetClick($event)"></div>
66
<input #input class="mdc-radio__native-control" type="radio"
77
[id]="inputId"
88
[checked]="checked"

src/material/radio/radio.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,15 @@ describe('MDC-based MatRadio', () => {
807807
}
808808
});
809809

810+
it('should focus on underlying input element when clicking on the touch target', () => {
811+
const input = radioDebugElements[0].nativeElement.querySelector('input');
812+
expect(document.activeElement).not.toBe(input);
813+
814+
radioDebugElements[0].nativeElement.querySelector('.mat-mdc-radio-touch-target').click();
815+
fixture.detectChanges();
816+
expect(document.activeElement).toBe(input);
817+
});
818+
810819
it('should not change focus origin if origin not specified', () => {
811820
fruitRadioInstances[0].focus(undefined, 'mouse');
812821
fruitRadioInstances[1].focus();

src/material/radio/radio.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,17 @@ export abstract class _MatRadioButtonBase
611611
}
612612
}
613613

614+
/** Triggered when the user clicks on the touch target. */
615+
_onTouchTargetClick(event: Event) {
616+
this._onInputInteraction(event);
617+
618+
if (!this.disabled) {
619+
// Normally the input should be focused already, but if the click
620+
// comes from the touch target, then we might have to focus it ourselves.
621+
this._inputElement.nativeElement.focus();
622+
}
623+
}
624+
614625
/** Sets the disabled state and marks for check if a change occurred. */
615626
protected _setDisabled(value: boolean) {
616627
if (this._disabled !== value) {

tools/public_api_guard/material/radio.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
8686
// (undocumented)
8787
_onInputClick(event: Event): void;
8888
_onInputInteraction(event: Event): void;
89+
_onTouchTargetClick(event: Event): void;
8990
radioGroup: _MatRadioGroupBase<_MatRadioButtonBase>;
9091
get required(): boolean;
9192
set required(value: BooleanInput);

0 commit comments

Comments
 (0)