Skip to content

Commit 0270cf5

Browse files
devversionandrewseguin
authored andcommitted
fix(checkbox): support native tabindex attribute (#6793)
Currently the checkbox only allows changing the tabIndex using the tabIndex binding. Using the native tabindex attribute doesn't have any affect. With this change the native tabindex property will be respected. References #6465
1 parent f8ed442 commit 0270cf5

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('MatCheckbox', () => {
3333
CheckboxWithChangeEvent,
3434
CheckboxWithFormControl,
3535
CheckboxWithoutLabel,
36+
CheckboxWithTabindexAttr,
3637
],
3738
providers: [
3839
{provide: ViewportRuler, useClass: FakeViewportRuler}
@@ -677,6 +678,20 @@ describe('MatCheckbox', () => {
677678

678679
});
679680

681+
describe('with native tabindex attribute', () => {
682+
683+
it('should properly detect native tabindex attribute', async(() => {
684+
fixture = TestBed.createComponent(CheckboxWithTabindexAttr);
685+
fixture.detectChanges();
686+
687+
const checkbox = fixture.debugElement
688+
.query(By.directive(MatCheckbox)).componentInstance as MatCheckbox;
689+
690+
expect(checkbox.tabIndex)
691+
.toBe(5, 'Expected tabIndex property to have been set based on the native attribute');
692+
}));
693+
});
694+
680695
describe('with multiple checkboxes', () => {
681696
beforeEach(() => {
682697
fixture = TestBed.createComponent(MultipleCheckboxes);
@@ -1009,3 +1024,9 @@ class CheckboxWithFormControl {
10091024
class CheckboxWithoutLabel {
10101025
label: string;
10111026
}
1027+
1028+
/** Test component with the native tabindex attribute. */
1029+
@Component({
1030+
template: `<mat-checkbox tabindex="5"></mat-checkbox>`
1031+
})
1032+
class CheckboxWithTabindexAttr {}

src/lib/checkbox/checkbox.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {
1111
AfterViewInit,
12+
Attribute,
1213
ChangeDetectionStrategy,
1314
ChangeDetectorRef,
1415
Component,
@@ -27,10 +28,12 @@ import {
2728
CanColor,
2829
CanDisable,
2930
CanDisableRipple,
31+
HasTabIndex,
3032
MatRipple,
3133
mixinColor,
3234
mixinDisabled,
3335
mixinDisableRipple,
36+
mixinTabIndex,
3437
RippleRef,
3538
} from '@angular/material/core';
3639
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
@@ -79,7 +82,7 @@ export class MatCheckboxBase {
7982
constructor(public _renderer: Renderer2, public _elementRef: ElementRef) {}
8083
}
8184
export const _MatCheckboxMixinBase =
82-
mixinColor(mixinDisableRipple(mixinDisabled(MatCheckboxBase)), 'accent');
85+
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatCheckboxBase)), 'accent'));
8386

8487

8588
/**
@@ -104,13 +107,13 @@ export const _MatCheckboxMixinBase =
104107
'[class.mat-checkbox-label-before]': 'labelPosition == "before"',
105108
},
106109
providers: [MAT_CHECKBOX_CONTROL_VALUE_ACCESSOR],
107-
inputs: ['disabled', 'disableRipple', 'color'],
110+
inputs: ['disabled', 'disableRipple', 'color', 'tabIndex'],
108111
encapsulation: ViewEncapsulation.None,
109112
preserveWhitespaces: false,
110113
changeDetection: ChangeDetectionStrategy.OnPush
111114
})
112115
export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAccessor,
113-
AfterViewInit, OnDestroy, CanColor, CanDisable, CanDisableRipple {
116+
AfterViewInit, OnDestroy, CanColor, CanDisable, HasTabIndex, CanDisableRipple {
114117

115118
/**
116119
* Attached to the aria-label attribute of the host element. In most cases, arial-labelledby will
@@ -156,9 +159,6 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
156159
/** Whether the label should appear after or before the checkbox. Defaults to 'after' */
157160
@Input() labelPosition: 'before' | 'after' = 'after';
158161

159-
/** Tabindex value that is passed to the underlying input element. */
160-
@Input() tabIndex: number = 0;
161-
162162
/** Name value will be applied to the input element if present */
163163
@Input() name: string | null = null;
164164

@@ -199,8 +199,11 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
199199
constructor(renderer: Renderer2,
200200
elementRef: ElementRef,
201201
private _changeDetectorRef: ChangeDetectorRef,
202-
private _focusMonitor: FocusMonitor) {
202+
private _focusMonitor: FocusMonitor,
203+
@Attribute('tabindex') tabIndex: string) {
203204
super(renderer, elementRef);
205+
206+
this.tabIndex = parseInt(tabIndex) || 0;
204207
}
205208

206209
ngAfterViewInit() {

0 commit comments

Comments
 (0)