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

feat(tab): Give focus to tab when activated #3164

Merged
merged 3 commits into from
Jul 23, 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 packages/mdc-tab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Method Signature | Description
`getContentOffsetWidth() => number` | Returns the `offsetWidth` value of the content element.
`notifyInteracted() => void` | Emits the `MDCTab:interacted` event.
`notifyActivated() => void` | Emits the `MDCTab:activated` event.
`focus() => void` | Applies focus to the root element.

### `MDCTabFoundation`

Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-tab/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class MDCTabAdapter {
* @return {number}
*/
getContentOffsetWidth() {}

/**
* Applies focus to the root element
*/
focus() {}
}

export {MDCTabDimensions, MDCTabAdapter};
2 changes: 2 additions & 0 deletions packages/mdc-tab/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MDCTabFoundation extends MDCFoundation {
getOffsetWidth: () => {},
getContentOffsetLeft: () => {},
getContentOffsetWidth: () => {},
focus: () => {},
});
}

Expand Down Expand Up @@ -127,6 +128,7 @@ class MDCTabFoundation extends MDCFoundation {
this.adapter_.setAttr(strings.ARIA_SELECTED, 'true');
this.adapter_.setAttr(strings.TABINDEX, '0');
this.adapter_.activateIndicator(previousIndicatorClientRect);
this.adapter_.focus();
this.adapter_.notifyActivated();
}

Expand Down
1 change: 1 addition & 0 deletions packages/mdc-tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class MDCTab extends MDCComponent {
getOffsetWidth: () => this.root_.offsetWidth,
getContentOffsetLeft: () => this.content_.offsetLeft,
getContentOffsetWidth: () => this.content_.offsetWidth,
focus: () => this.root_.focus(),
}));
}

Expand Down
7 changes: 7 additions & 0 deletions test/unit/mdc-tab/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ test('defaultAdapter returns a complete adapter implementation', () => {
'activateIndicator', 'deactivateIndicator', 'computeIndicatorClientRect',
'getOffsetLeft', 'getOffsetWidth', 'getContentOffsetLeft', 'getContentOffsetWidth',
'notifyInteracted', 'notifyActivated',
'focus',
]);
});

Expand Down Expand Up @@ -88,6 +89,12 @@ test('#activate activates the indicator', () => {
td.verify(mockAdapter.activateIndicator({width: 100, left: 200}));
});

test('#activate focuses the root node', () => {
const {foundation, mockAdapter} = setupTest();
foundation.activate({width: 100, left: 200});
td.verify(mockAdapter.focus());
});

test(`#activate emits the ${MDCTabFoundation.strings.ACTIVATED_EVENT} event`, () => {
const {foundation, mockAdapter} = setupTest();
foundation.activate();
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mdc-tab/mdc-tab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ test('#adapter.getContentOffsetLeft() returns the offsetLeft of the content elem
assert.strictEqual(component.getDefaultFoundation().adapter_.getContentOffsetLeft(), content.offsetLeft);
});

test('#adapter.focus() gives focus to the root element', () => {
const {root, component} = setupTest();
document.body.appendChild(root);
component.getDefaultFoundation().adapter_.focus();
assert.strictEqual(document.activeElement, root);
document.body.removeChild(root);
});

test(`#adapter.notifyInteracted() emits the ${MDCTabFoundation.strings.INTERACTED_EVENT} event`, () => {
const {component} = setupTest();
const handler = td.func('interaction handler');
Expand Down