Commit fff80ecb012b72b6bdc6761820ba7c729b04407e

Authored by Aresn
Committed by GitHub
2 parents 87ec8082 e45d1bce

Merge pull request #3665 from SergioCrisostomo/tabs-keyboard

Tabs small fixes
Showing 1 changed file with 21 additions and 12 deletions   Show diff stats
src/components/tabs/tabs.vue
... ... @@ -133,7 +133,7 @@
133 133 ];
134 134 },
135 135 contentStyle () {
136   - const x = this.navList.findIndex((nav) => nav.name === this.activeKey);
  136 + const x = this.getTabIndex(this.activeKey);
137 137 const p = x === 0 ? '0%' : `-${x}00%`;
138 138  
139 139 let style = {};
... ... @@ -184,7 +184,7 @@
184 184 },
185 185 updateBar () {
186 186 this.$nextTick(() => {
187   - const index = this.navList.findIndex((nav) => nav.name === this.activeKey);
  187 + const index = this.getTabIndex(this.activeKey);
188 188 if (!this.$refs.nav) return; // 页面销毁时,这里会报错,为了解决 #2100
189 189 const prevTabs = this.$refs.nav.querySelectorAll(`.${prefixCls}-tab`);
190 190 const tab = prevTabs[index];
... ... @@ -239,7 +239,7 @@
239 239 handleTabKeyboardSelect(init = false){
240 240 if (init) return;
241 241 const focused = this.focusedKey || 0;
242   - const index = this.navList.findIndex(({name}) => name === focused);
  242 + const index = this.getTabIndex(focused);
243 243 this.handleChange(index);
244 244 },
245 245 handleRemove (index) {
... ... @@ -310,6 +310,9 @@
310 310 ? Number(navStyle.transform.match(/translateX\(-(\d+(\.\d+)*)px\)/)[1])
311 311 : 0;
312 312 },
  313 + getTabIndex(name){
  314 + return this.navList.findIndex(nav => nav.name === name);
  315 + },
313 316 setOffset(value) {
314 317 this.navStyle.transform = `translateX(-${value}px)`;
315 318 },
... ... @@ -368,6 +371,18 @@
368 371 parentNode = parentNode.parentNode;
369 372 }
370 373 return false;
  374 + },
  375 + updateVisibility(index){
  376 + [...this.$refs.panes.children].forEach((el, i) => {
  377 + if (index === i) {
  378 + [...el.children].forEach(child => child.style.visibility = 'visible');
  379 + setTimeout(() => focusFirst(el, el), transitionTime);
  380 + } else {
  381 + setTimeout(() => {
  382 + [...el.children].forEach(child => child.style.visibility = 'hidden');
  383 + }, transitionTime);
  384 + }
  385 + });
371 386 }
372 387 },
373 388 watch: {
... ... @@ -385,15 +400,8 @@
385 400 });
386 401  
387 402 // 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.visibility = 'visible');
392   - setTimeout(() => focusFirst(el, el), transitionTime);
393   - } else {
394   - [...el.children].forEach(child => child.style.visibility = 'hidden');
395   - }
396   - });
  403 + const nextIndex = Math.max(this.getTabIndex(this.focusedKey), 0);
  404 + this.updateVisibility(nextIndex);
397 405 }
398 406 },
399 407 mounted () {
... ... @@ -414,6 +422,7 @@
414 422 }
415 423  
416 424 this.handleTabKeyboardSelect(true);
  425 + this.updateVisibility(this.getTabIndex(this.activeKey));
417 426 },
418 427 beforeDestroy() {
419 428 this.observer.removeListener(this.$refs.navWrap, this.handleResize);
... ...