Commit 38ab744203590cebd97e8c5efe1aa0510872934b

Authored by Sergio Crisostomo
1 parent 2bf3e047

Correct logic for selecting by keyboard

Showing 2 changed files with 40 additions and 16 deletions   Show diff stats
examples/routers/tabs.vue
@@ -176,18 +176,39 @@ @@ -176,18 +176,39 @@
176 <TabPane label="标签二" disabled>标签二的内容</TabPane> 176 <TabPane label="标签二" disabled>标签二的内容</TabPane>
177 <TabPane label="标签三">标签三的内容</TabPane> 177 <TabPane label="标签三">标签三的内容</TabPane>
178 </Tabs> 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 </div> 196 </div>
180 </template> 197 </template>
181 <script> 198 <script>
182 export default { 199 export default {
183 data () { 200 data () {
184 return { 201 return {
185 - tabs: 2 202 + tabs: 2,
  203 + name: 'b',
186 } 204 }
187 }, 205 },
188 methods: { 206 methods: {
189 handleTabsAdd () { 207 handleTabsAdd () {
190 this.tabs ++; 208 this.tabs ++;
  209 + },
  210 + handleClick (name) {
  211 + console.log(name);
191 } 212 }
192 } 213 }
193 } 214 }
src/components/tabs/tabs.vue
@@ -237,21 +237,9 @@ @@ -237,21 +237,9 @@
237 this.focusedKey = nextTab.name; 237 this.focusedKey = nextTab.name;
238 }, 238 },
239 handleTabKeyboardSelect(){ 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 - }); 240 + const focused = this.focusedKey || 0;
  241 + const index = this.navList.findIndex(({name}) => name === focused);
  242 + this.handleChange(index);
255 }, 243 },
256 handleRemove (index) { 244 handleRemove (index) {
257 const tabs = this.getTabs(); 245 const tabs = this.getTabs();
@@ -394,6 +382,21 @@ @@ -394,6 +382,21 @@
394 this.$nextTick(() => { 382 this.$nextTick(() => {
395 this.scrollToActiveTab(); 383 this.scrollToActiveTab();
396 }); 384 });
  385 +
  386 + // update visibility
  387 + const nextIndex = Math.max(this.navList.findIndex(tab => tab.name === this.focusedKey), 0);
  388 + [...this.$refs.panes.children].forEach((el, i) => {
  389 + if (nextIndex === i) {
  390 + [...el.children].forEach(child => child.style.display = 'block');
  391 + setTimeout(() => {
  392 + focusFirst(el, el);
  393 + }, transitionTime);
  394 + } else {
  395 + setTimeout(() => {
  396 + [...el.children].forEach(child => child.style.display = 'none');
  397 + }, transitionTime);
  398 + }
  399 + });
397 } 400 }
398 }, 401 },
399 mounted () { 402 mounted () {