Commit a459837191ef77cc181da7435ce3884e75436fab
Committed by
GitHub
Merge pull request #3642 from SergioCrisostomo/tabs-keyboard
Correct logic for selecting by keyboard
Showing
2 changed files
with
44 additions
and
19 deletions
Show diff stats
examples/routers/tabs.vue
| ... | ... | @@ -176,18 +176,39 @@ |
| 176 | 176 | <TabPane label="标签二" disabled>标签二的内容</TabPane> |
| 177 | 177 | <TabPane label="标签三">标签三的内容</TabPane> |
| 178 | 178 | </Tabs> |
| 179 | + <tabs v-model="name" type="card" @on-click="handleClick"> | |
| 180 | + <tab-pane name="a" label="标签一"> | |
| 181 | + <div>1</div> | |
| 182 | + </tab-pane> | |
| 183 | + <tab-pane name="b" label="标签二"> | |
| 184 | + <div>2</div> | |
| 185 | + </tab-pane> | |
| 186 | + <tab-pane name="c" label="标签三"> | |
| 187 | + <div>3</div> | |
| 188 | + </tab-pane> | |
| 189 | + <tab-pane name="d" label="标签四"> | |
| 190 | + <div>4</div> | |
| 191 | + </tab-pane> | |
| 192 | + <tab-pane name="e" label="标签五"> | |
| 193 | + <div>5</div> | |
| 194 | + </tab-pane> | |
| 195 | + </tabs> | |
| 179 | 196 | </div> |
| 180 | 197 | </template> |
| 181 | 198 | <script> |
| 182 | 199 | export default { |
| 183 | 200 | data () { |
| 184 | 201 | return { |
| 185 | - tabs: 2 | |
| 202 | + tabs: 2, | |
| 203 | + name: 'b', | |
| 186 | 204 | } |
| 187 | 205 | }, |
| 188 | 206 | methods: { |
| 189 | 207 | handleTabsAdd () { |
| 190 | 208 | this.tabs ++; |
| 209 | + }, | |
| 210 | + handleClick (name) { | |
| 211 | + console.log(name); | |
| 191 | 212 | } |
| 192 | 213 | } |
| 193 | 214 | } | ... | ... |
src/components/tabs/tabs.vue
| ... | ... | @@ -7,7 +7,7 @@ |
| 7 | 7 | tabindex="0" |
| 8 | 8 | ref="navContainer" |
| 9 | 9 | @keydown="handleTabKeyNavigation" |
| 10 | - @keydown.space.prevent="handleTabKeyboardSelect" | |
| 10 | + @keydown.space.prevent="handleTabKeyboardSelect(false)" | |
| 11 | 11 | > |
| 12 | 12 | <div ref="navWrap" :class="[prefixCls + '-nav-wrap', scrollable ? prefixCls + '-nav-scrollable' : '']"> |
| 13 | 13 | <span :class="[prefixCls + '-nav-prev', scrollable ? '' : prefixCls + '-nav-scroll-disabled']" @click="scrollPrev"><Icon type="chevron-left"></Icon></span> |
| ... | ... | @@ -236,22 +236,11 @@ |
| 236 | 236 | const nextTab = getNextTab(this.navList, this.focusedKey, direction); |
| 237 | 237 | this.focusedKey = nextTab.name; |
| 238 | 238 | }, |
| 239 | - handleTabKeyboardSelect(){ | |
| 240 | - this.activeKey = this.focusedKey || 0; | |
| 241 | - const nextIndex = Math.max(this.navList.findIndex(tab => tab.name === this.focusedKey), 0); | |
| 242 | - | |
| 243 | - [...this.$refs.panes.children].forEach((el, i) => { | |
| 244 | - if (nextIndex === i) { | |
| 245 | - [...el.children].forEach(child => child.style.display = 'block'); | |
| 246 | - setTimeout(() => { | |
| 247 | - focusFirst(el, el); | |
| 248 | - }, transitionTime); | |
| 249 | - } else { | |
| 250 | - setTimeout(() => { | |
| 251 | - [...el.children].forEach(child => child.style.display = 'none'); | |
| 252 | - }, transitionTime); | |
| 253 | - } | |
| 254 | - }); | |
| 239 | + handleTabKeyboardSelect(init = false){ | |
| 240 | + if (init) return; | |
| 241 | + const focused = this.focusedKey || 0; | |
| 242 | + const index = this.navList.findIndex(({name}) => name === focused); | |
| 243 | + this.handleChange(index); | |
| 255 | 244 | }, |
| 256 | 245 | handleRemove (index) { |
| 257 | 246 | const tabs = this.getTabs(); |
| ... | ... | @@ -394,6 +383,21 @@ |
| 394 | 383 | this.$nextTick(() => { |
| 395 | 384 | this.scrollToActiveTab(); |
| 396 | 385 | }); |
| 386 | + | |
| 387 | + // update visibility | |
| 388 | + const nextIndex = Math.max(this.navList.findIndex(tab => tab.name === this.focusedKey), 0); | |
| 389 | + [...this.$refs.panes.children].forEach((el, i) => { | |
| 390 | + if (nextIndex === i) { | |
| 391 | + [...el.children].forEach(child => child.style.display = 'block'); | |
| 392 | + setTimeout(() => { | |
| 393 | + focusFirst(el, el); | |
| 394 | + }, transitionTime); | |
| 395 | + } else { | |
| 396 | + setTimeout(() => { | |
| 397 | + [...el.children].forEach(child => child.style.display = 'none'); | |
| 398 | + }, transitionTime); | |
| 399 | + } | |
| 400 | + }); | |
| 397 | 401 | } |
| 398 | 402 | }, |
| 399 | 403 | mounted () { |
| ... | ... | @@ -413,7 +417,7 @@ |
| 413 | 417 | this.mutationObserver.observe(hiddenParentNode, { attributes: true, childList: true, characterData: true, attributeFilter: ['style'] }); |
| 414 | 418 | } |
| 415 | 419 | |
| 416 | - this.handleTabKeyboardSelect(); | |
| 420 | + this.handleTabKeyboardSelect(true); | |
| 417 | 421 | }, |
| 418 | 422 | beforeDestroy() { |
| 419 | 423 | this.observer.removeListener(this.$refs.navWrap, this.handleResize); | ... | ... |