Skip to content

fix(checkbox): redirect focus to underlying input element #13959

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
Dec 7, 2018
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
1 change: 1 addition & 0 deletions src/lib/checkbox/checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ $_mat-checkbox-mark-stroke-size: 2 / 15 * $mat-checkbox-size !default;

cursor: pointer;
-webkit-tap-highlight-color: transparent;
outline: 0;

.mat-ripple-element:not(.mat-checkbox-persistent-ripple) {
opacity: 0.16;
Expand Down
11 changes: 10 additions & 1 deletion src/lib/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ describe('MatCheckbox', () => {
expect(document.activeElement).toBe(inputElement);
});

it('should focus on underlying input element when the host is focused', () => {
expect(document.activeElement).not.toBe(inputElement);

checkboxNativeElement.focus();
fixture.detectChanges();

expect(document.activeElement).toBe(inputElement);
});

it('should forward the value to input element', () => {
testComponent.checkboxValue = 'basic_checkbox';
fixture.detectChanges();
Expand Down Expand Up @@ -790,7 +799,7 @@ describe('MatCheckbox', () => {
fixture.detectChanges();

const checkbox = fixture.debugElement.query(By.directive(MatCheckbox)).nativeElement;
expect(checkbox.getAttribute('tabindex')).toBeFalsy();
expect(checkbox.getAttribute('tabindex')).toBe('-1');
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/lib/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ export const _MatCheckboxMixinBase:
host: {
'class': 'mat-checkbox',
'[id]': 'id',
'[attr.tabindex]': 'null',
'[attr.tabindex]': '-1', // Reset back to -1 so that the `focus` event still works.
'[class.mat-checkbox-indeterminate]': 'indeterminate',
'[class.mat-checkbox-checked]': 'checked',
'[class.mat-checkbox-disabled]': 'disabled',
'[class.mat-checkbox-label-before]': 'labelPosition == "before"',
'[class._mat-animation-noopable]': `_animationMode === 'NoopAnimations'`,
'(focus)': '_inputElement.nativeElement.focus()',
},
providers: [MAT_CHECKBOX_CONTROL_VALUE_ACCESSOR],
inputs: ['disableRipple', 'color', 'tabIndex'],
Expand Down