快捷键动态获取parentEle元素

This commit is contained in:
wanghaoo 2018-12-26 10:09:30 +08:00
parent b2ce3ba707
commit fcc27437e1
7 changed files with 41 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import Tree from '../../src';
const x = 6;
const y = 5;
const z = 1;
const z = 2;
const gData = [];
const generateData = (_level, _preKey, _tns) => {

File diff suppressed because one or more lines are too long

26
dist/demo.js vendored

File diff suppressed because one or more lines are too long

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -11,6 +11,7 @@ import {
getCheck,
getStrictlyValue,
arraysEqual,
closest
} from './util';
import PropTypes from 'prop-types';
import { KeyCode } from 'tinper-bee-core';
@ -425,7 +426,8 @@ onExpand(treeNode,keyType) {
//查询的下一个节点不为空的话,则选中
if(nextTreeNode){
const queryInfo = `a[pos="${nextPos}"]`;
const focusEle = e.target.parentElement.parentElement.parentElement.parentElement.querySelector(queryInfo);
const parentEle = closest(e.target,".u-tree")
const focusEle = parentEle?parentEle.querySelector(queryInfo):null;
focusEle && focusEle.focus()
this.onSelect(nextTreeNode);
}

View File

@ -168,7 +168,7 @@ class TreeNode extends React.Component {
// keyboard event support
onKeyDown(e) {
this.props.root.onKeyDown(e,this);
if(e.keyCode !== KeyCode.TAB){
if(e.keyCode == KeyCode.SPACE || e.keyCode == KeyCode.DOWN || e.keyCode == KeyCode.LEFT || e.keyCode == KeyCode.RIGHT ||e.keyCode == KeyCode.UP){
e.preventDefault();
}

View File

@ -284,3 +284,17 @@ export function arraysEqual(a, b) {
}
return true;
}
export function closest(el, selector) {
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
while (el) {
if (matchesSelector.call(el, selector)) {
return el;
} else {
el = el.parentElement;
}
}
return null;
}