Accessibility for Tree (#8049)

* tree

* Update yarn.lock
pull/8100/head
maranran 2017-11-08 21:19:52 -06:00 committed by 杨奕
parent 81011d1c48
commit 6c77cd9716
3 changed files with 84 additions and 8 deletions

View File

@ -25,7 +25,12 @@
@include b(tree-node) { @include b(tree-node) {
white-space: nowrap; white-space: nowrap;
outline: none;
&:focus { /* focus */
> .el-tree-node__content {
background-color: $--tree-node-hover-color;
}
}
@include e(content) { @include e(content) {
display: flex; display: flex;
align-items: center; align-items: center;

View File

@ -1,12 +1,21 @@
<template> <template>
<div class="el-tree-node" <div
class="el-tree-node"
@click.stop="handleClick" @click.stop="handleClick"
v-show="node.visible" v-show="node.visible"
:class="{ :class="{
'is-expanded': expanded, 'is-expanded': expanded,
'is-current': tree.store.currentNode === node, 'is-current': tree.store.currentNode === node,
'is-hidden': !node.visible 'is-hidden': !node.visible,
}"> 'is-focusable': !node.disabled,
'is-checked': !node.disabled && node.checked
}"
role="treeitem"
tabindex="-1"
:aria-expanded="expanded"
:aria-disabled="node.disabled"
:aria-checked="node.checked"
>
<div class="el-tree-node__content" <div class="el-tree-node__content"
:style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }"> :style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }">
<span <span
@ -20,7 +29,8 @@
:indeterminate="node.indeterminate" :indeterminate="node.indeterminate"
:disabled="!!node.disabled" :disabled="!!node.disabled"
@click.native.stop @click.native.stop
@change="handleCheckChange"> @change="handleCheckChange"
>
</el-checkbox> </el-checkbox>
<span <span
v-if="node.loading" v-if="node.loading"
@ -32,7 +42,10 @@
<div <div
class="el-tree-node__children" class="el-tree-node__children"
v-if="childNodeRendered" v-if="childNodeRendered"
v-show="expanded"> v-show="expanded"
role="group"
:aria-expanded="expanded"
>
<el-tree-node <el-tree-node
:render-content="renderContent" :render-content="renderContent"
v-for="child in node.childNodes" v-for="child in node.childNodes"

View File

@ -1,5 +1,9 @@
<template> <template>
<div class="el-tree" :class="{ 'el-tree--highlight-current': highlightCurrent }"> <div
class="el-tree"
:class="{ 'el-tree--highlight-current': highlightCurrent }"
role="tree"
>
<el-tree-node <el-tree-node
v-for="child in root.childNodes" v-for="child in root.childNodes"
:node="child" :node="child"
@ -33,7 +37,9 @@
return { return {
store: null, store: null,
root: null, root: null,
currentNode: null currentNode: null,
treeItems: null,
checkboxItems: []
}; };
}, },
@ -101,6 +107,9 @@
get() { get() {
return this.data; return this.data;
} }
},
treeItemArray() {
return Array.prototype.slice.call(this.treeItems);
} }
}, },
@ -115,6 +124,11 @@
}, },
data(newVal) { data(newVal) {
this.store.setData(newVal); this.store.setData(newVal);
},
checkboxItems(val) {
Array.prototype.forEach.call(val, (checkbox) => {
checkbox.setAttribute('tabindex', -1);
});
} }
}, },
@ -171,6 +185,42 @@
updateKeyChildren(key, data) { updateKeyChildren(key, data) {
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild'); if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
this.store.updateChildren(key, data); this.store.updateChildren(key, data);
},
initTabindex() {
this.treeItems = this.$el.querySelectorAll('.is-focusable[role=treeitem]');
this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
const checkedItem = this.$el.querySelectorAll('.is-checked[role=treeitem]');
if (checkedItem.length) {
checkedItem[0].setAttribute('tabindex', 0);
return;
}
this.treeItems[0].setAttribute('tabindex', 0);
},
handelKeydown(ev) {
const currentItem = ev.target;
const keyCode = ev.keyCode;
this.treeItems = this.$el.querySelectorAll('.is-focusable[role=treeitem]');
const currentIndex = this.treeItemArray.indexOf(currentItem);
let nextIndex;
if ([38, 40].includes(keyCode)) { // updown
if (keyCode === 38) { // up
nextIndex = currentIndex !== 0 ? currentIndex - 1 : 0;
} else {
nextIndex = (currentIndex < this.treeItemArray.length - 1) ? currentIndex + 1 : 0;
}
this.treeItemArray[nextIndex].focus(); //
}
const hasInput = currentItem.querySelector('[type="checkbox"]');
if ([37, 39].includes(keyCode)) { // leftright
currentItem.click(); //
}
if ([13, 32].includes(keyCode)) { // space entercheckbox
if (hasInput) {
hasInput.click();
}
ev.stopPropagation();
ev.preventDefault();
}
} }
}, },
@ -194,6 +244,14 @@
}); });
this.root = this.store.root; this.root = this.store.root;
},
mounted() {
this.initTabindex();
this.$el.addEventListener('keydown', this.handelKeydown);
},
updated() {
this.treeItems = this.$el.querySelectorAll('[role=treeitem]');
this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
} }
}; };
</script> </script>