Skip to content

Commit 6111743

Browse files
committed
fix(checkbox): support native tabindex attribute
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 8436f8c commit 6111743

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('MdCheckbox', () => {
3333
CheckboxWithChangeEvent,
3434
CheckboxWithFormControl,
3535
CheckboxWithoutLabel,
36+
CheckboxWithTabindexAttr,
3637
],
3738
providers: [
3839
{provide: ViewportRuler, useClass: FakeViewportRuler}
@@ -677,6 +678,20 @@ describe('MdCheckbox', () => {
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(MdCheckbox)).componentInstance as MdCheckbox;
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);
@@ -998,3 +1013,9 @@ class CheckboxWithFormControl {
9981013
class CheckboxWithoutLabel {
9991014
label: string;
10001015
}
1016+
1017+
/** Test component with the native tabindex attribute. */
1018+
@Component({
1019+
template: `<md-checkbox tabindex="5"></md-checkbox>`
1020+
})
1021+
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
MdRipple,
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 MdCheckboxBase {
7982
constructor(public _renderer: Renderer2, public _elementRef: ElementRef) {}
8083
}
8184
export const _MdCheckboxMixinBase =
82-
mixinColor(mixinDisableRipple(mixinDisabled(MdCheckboxBase)), 'accent');
85+
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MdCheckboxBase)), 'accent'));
8386

8487

8588
/**
@@ -104,13 +107,13 @@ export const _MdCheckboxMixinBase =
104107
'[class.mat-checkbox-label-before]': 'labelPosition == "before"',
105108
},
106109
providers: [MD_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 MdCheckbox extends _MdCheckboxMixinBase implements ControlValueAccessor, AfterViewInit,
113-
OnDestroy, CanColor, CanDisable, CanDisableRipple {
116+
OnDestroy, CanColor, CanDisable, CanDisableRipple, HasTabIndex {
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 MdCheckbox extends _MdCheckboxMixinBase implements ControlValueAcce
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 MdCheckbox extends _MdCheckboxMixinBase implements ControlValueAcce
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)