Skip to content

Commit bc3b82b

Browse files
emanuelmutschlechnerpi0
authored andcommitted
feat(tabs): add key nav prop like button toolbar has (bootstrap-vue#1733)
1 parent 89eff3c commit bc3b82b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/components/tabs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ export default {
295295
<!-- with-classes.vue -->
296296
```
297297

298+
## Keyboard Navigation
299+
Enable optional keyboard navigation by setting the prop `key-nav`.
300+
301+
| Keypress | Action
302+
| -------- | ------
303+
| <kbd>LEFT</kbd> or <kbd>UP</kbd> | Move to the previous non-disabled tab
304+
| <kbd>RIGHT</kbd> or <kbd>DOWN</kbd> | Move to the next non-disabled tab
305+
| <kbd>SHIFT</kbd>+<kbd>LEFT</kbd> or <kbd>SHIFT</kbd>+<kbd>UP</kbd> | Move to the first non-disabled tab
306+
| <kbd>SHIFT</kbd>+<kbd>RIGHT</kbd> or <kbd>SHIFT</kbd>+<kbd>DOWN</kbd> | Move to the last non-disabled tab
307+
| <kbd>TAB</kbd> | Move to the next control on the page
308+
| <kbd>SHIFT</kbd>+<kbd>TAB</kbd> | Move to the previous control on the page
309+
310+
**Caution:** If you have text or text-like inputs in your tabs, leave keyboard navigation off,
311+
as it is not possble to use key presses to jump out of a text (or test-like) inputs.
312+
298313

299314
## Advanced Examples
300315

src/components/tabs/tabs.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const bTabButtonHelper = {
1515
active: { type: Boolean, default: false },
1616
disabled: { type: Boolean, default: false },
1717
linkClass: { default: null },
18-
itemClass: { default: null }
18+
itemClass: { default: null },
19+
keyNav: { type: Boolean, default: false }
1920
},
2021
render (h) {
2122
const link = h('a', {
@@ -26,7 +27,7 @@ const bTabButtonHelper = {
2627
],
2728
attrs: {
2829
role: 'tab',
29-
tabindex: '-1',
30+
tabindex: this.keyNav ? '-1' : null,
3031
href: this.href,
3132
id: this.id,
3233
disabled: this.disabled,
@@ -52,6 +53,9 @@ const bTabButtonHelper = {
5253
evt.preventDefault()
5354
evt.stopPropagation()
5455
}
56+
if (evt.type !== 'click' && !this.keyNav) {
57+
return
58+
}
5559
if (this.disabled) {
5660
stop()
5761
return
@@ -86,7 +90,8 @@ export default {
8690
posInSet: index + 1,
8791
controls: this.safeId('_BV_tab_container_'),
8892
linkClass: tab.titleLinkClass,
89-
itemClass: tab.titleItemClass
93+
itemClass: tab.titleItemClass,
94+
keyNav: this.keyNav
9095
},
9196
on: {
9297
click: evt => {
@@ -116,7 +121,7 @@ export default {
116121
],
117122
attrs: {
118123
role: 'tablist',
119-
tabindex: '0',
124+
tabindex: this.keyNav ? '0' : null,
120125
id: this.safeId('_BV_tab_controls_')
121126
},
122127
on: { keydown: this.onKeynav }
@@ -237,6 +242,10 @@ export default {
237242
navWrapperClass: {
238243
type: [String, Array, Object],
239244
default: null
245+
},
246+
keyNav: {
247+
type: Boolean,
248+
default: false
240249
}
241250
},
242251
watch: {
@@ -280,6 +289,9 @@ export default {
280289
* handle keyboard navigation
281290
*/
282291
onKeynav (evt) {
292+
if (!this.keyNav) {
293+
return
294+
}
283295
const key = evt.keyCode
284296
const shift = evt.shiftKey
285297
function stop () {

0 commit comments

Comments
 (0)