From 41a1f85dd5a38da86efad1139c47be03b7543768 Mon Sep 17 00:00:00 2001 From: tjz <415800467@qq.com> Date: Thu, 12 Apr 2018 22:04:55 +0800 Subject: [PATCH] add vc-tree demo --- components/tabs/tabs.jsx | 2 +- components/vc-tree/demo/contextmenu.less | 18 ++++ components/vc-tree/demo/contextmenu.vue | 75 +++++++++++++++ components/vc-tree/demo/draggable.less | 11 +++ components/vc-tree/demo/draggable.vue | 111 +++++++++++++++++++++++ components/vc-tree/src/Tree.jsx | 50 +++------- components/vc-tree/src/TreeNode.jsx | 8 +- components/vc-tree/src/util.js | 2 +- site/routes.js | 2 +- 9 files changed, 232 insertions(+), 47 deletions(-) create mode 100644 components/vc-tree/demo/contextmenu.less create mode 100644 components/vc-tree/demo/contextmenu.vue create mode 100644 components/vc-tree/demo/draggable.less create mode 100644 components/vc-tree/demo/draggable.vue diff --git a/components/tabs/tabs.jsx b/components/tabs/tabs.jsx index 99bbf0f9d..e905c6895 100644 --- a/components/tabs/tabs.jsx +++ b/components/tabs/tabs.jsx @@ -1,7 +1,7 @@ import Tabs from '../vc-tabs/src/Tabs' import isFlexSupported from '../_util/isFlexSupported' -import { hasProp, getComponentFromProp, getComponentName, isEmptyElement, getSlotOptions } from '../_util/props-util' +import { hasProp, getComponentFromProp, isEmptyElement, getSlotOptions } from '../_util/props-util' import warning from '../_util/warning' export default { name: 'ATabs', diff --git a/components/vc-tree/demo/contextmenu.less b/components/vc-tree/demo/contextmenu.less new file mode 100644 index 000000000..63295d04f --- /dev/null +++ b/components/vc-tree/demo/contextmenu.less @@ -0,0 +1,18 @@ +@contextmenuPrefixCls: rc-tree-contextmenu; +.@{contextmenuPrefixCls} { + position: absolute; + left: -9999px; + top: -9999px; + z-index: 1070; + display: block; + background-color: #fff; + + &-hidden { + display: none; + } + + &-inner { + border: 1px solid #ddd; + padding: 10px 20px; + } +} diff --git a/components/vc-tree/demo/contextmenu.vue b/components/vc-tree/demo/contextmenu.vue new file mode 100644 index 000000000..6d536ef7e --- /dev/null +++ b/components/vc-tree/demo/contextmenu.vue @@ -0,0 +1,75 @@ + + diff --git a/components/vc-tree/demo/draggable.less b/components/vc-tree/demo/draggable.less new file mode 100644 index 000000000..dfe188788 --- /dev/null +++ b/components/vc-tree/demo/draggable.less @@ -0,0 +1,11 @@ + +.draggable-demo{ + padding: 0 20px; + .draggable-container { + margin: 10px 30px; + width: 200px; + height: 200px; + overflow: auto; + border: 1px solid #ccc; + } +} diff --git a/components/vc-tree/demo/draggable.vue b/components/vc-tree/demo/draggable.vue new file mode 100644 index 000000000..14337392f --- /dev/null +++ b/components/vc-tree/demo/draggable.vue @@ -0,0 +1,111 @@ + \ No newline at end of file diff --git a/components/vc-tree/src/Tree.jsx b/components/vc-tree/src/Tree.jsx index 23cbd1e90..5204380db 100644 --- a/components/vc-tree/src/Tree.jsx +++ b/components/vc-tree/src/Tree.jsx @@ -1,7 +1,7 @@ import PropTypes from '../../_util/vue-types' import classNames from 'classnames' import warning from 'warning' -import { initDefaultProps, getOptionProps } from '../../_util/props-util' +import { initDefaultProps, getOptionProps, getSlots } from '../../_util/props-util' import { cloneElement } from '../../_util/vnode' import BaseMixin from '../../_util/BaseMixin' import { @@ -162,9 +162,10 @@ const Tree = { } return { ...state, - ...(this.getSyncProps(props) || {}), + ...this.getSyncProps(props), dragOverNodeKey: '', dropPosition: null, + dragNodesKeys: [], } }, provide () { @@ -203,8 +204,8 @@ const Tree = { methods: { onNodeDragStart (event, node) { const { sExpandedKeys } = this - const { eventKey, children } = node.props - + const { eventKey } = node + const children = getSlots(node).default this.dragNode = node this.setState({ @@ -223,13 +224,13 @@ const Tree = { */ onNodeDragEnter (event, node) { const { sExpandedKeys } = this - const { pos, eventKey } = node.props + const { pos, eventKey } = node const dropPosition = calcDropPosition(event, node) // Skip if drag node is self if ( - this.dragNode.props.eventKey === eventKey && + this.dragNode.eventKey === eventKey && dropPosition === 0 ) { this.setState({ @@ -268,7 +269,7 @@ const Tree = { }, 0) }, onNodeDragOver (event, node) { - const { eventKey } = node.props + const { eventKey } = node // Update drag position if (this.dragNode && eventKey === this.dragOverNodeKey) { @@ -297,7 +298,7 @@ const Tree = { onNodeDrop (event, node) { const { dragNodesKeys, dropPosition } = this - const { eventKey, pos } = node.props + const { eventKey, pos } = node this.setState({ dragOverNodeKey: '', @@ -509,46 +510,21 @@ const Tree = { /** * Sync state with props if needed */ - getSyncProps (props = {}, prevProps) { - let needSync = false + getSyncProps (props = {}) { const newState = {} - const myPrevProps = prevProps || {} const children = this.$slots.default - function checkSync (name) { - if (props[name] !== myPrevProps[name]) { - needSync = true - return true - } - return false - } - - // Children change will affect check box status. - // And no need to check when prev props not provided - if (prevProps && checkSync('children')) { - const { checkedKeys = [], halfCheckedKeys = [] } = - calcCheckedKeys(props.checkedKeys || this.sCheckedKeys, props, children) || {} - newState.sCheckedKeys = checkedKeys - newState.sHalfCheckedKeys = halfCheckedKeys - } - - // Re-calculate when autoExpandParent or expandedKeys changed - if (prevProps && (checkSync('autoExpandParent') || checkSync('expandedKeys'))) { - newState.sExpandedKeys = props.autoExpandParent - ? calcExpandedKeys(props.expandedKeys, props, children) : props.expandedKeys - } - - if (checkSync('selectedKeys')) { + if (props.selectedKeys !== undefined) { newState.sSelectedKeys = calcSelectedKeys(props.selectedKeys, props, children) } - if (checkSync('checkedKeys')) { + if (props.checkedKeys !== undefined) { const { checkedKeys = [], halfCheckedKeys = [] } = calcCheckedKeys(props.checkedKeys, props, children) || {} newState.sCheckedKeys = checkedKeys newState.sHalfCheckedKeys = halfCheckedKeys } - return needSync ? newState : null + return newState }, /** diff --git a/components/vc-tree/src/TreeNode.jsx b/components/vc-tree/src/TreeNode.jsx index ce8d02025..5154308bf 100644 --- a/components/vc-tree/src/TreeNode.jsx +++ b/components/vc-tree/src/TreeNode.jsx @@ -291,11 +291,6 @@ const TreeNode = { } }, - // Drag usage - setSelectHandle (node) { - this.selectHandle = node - }, - getNodeChildren () { const { $slots: { default: children }} = this const originList = filterEmpty(children) @@ -497,10 +492,9 @@ const TreeNode = { )} draggable={(!disabled && draggable) || undefined} aria-grabbed={(!disabled && draggable) || undefined} - onMouseenter={this.onMouseEnter} onMouseleave={this.onMouseLeave} - onContexmenu={this.onContextMenu} + onContextmenu={this.onContextMenu} onClick={this.onSelectorClick} onDragstart={this.onDragStart} > diff --git a/components/vc-tree/src/util.js b/components/vc-tree/src/util.js index c55fbc446..11fd3bd13 100644 --- a/components/vc-tree/src/util.js +++ b/components/vc-tree/src/util.js @@ -176,7 +176,7 @@ export function getDragNodesKeys (treeNodes, node) { export function calcDropPosition (event, treeNode) { const { clientY } = event - const { top, bottom, height } = treeNode.selectHandle.getBoundingClientRect() + const { top, bottom, height } = treeNode.$refs.selectHandle.getBoundingClientRect() const des = Math.max(height * DRAG_SIDE_RANGE, DRAG_MIN_GAP) if (clientY <= top + des) { diff --git a/site/routes.js b/site/routes.js index 2b214efe2..6360a0fc8 100644 --- a/site/routes.js +++ b/site/routes.js @@ -3,7 +3,7 @@ import Layout from './components/layout.vue' const AsyncTestComp = () => { const d = window.location.hash.replace('#', '') return { - component: import(`../components/vc-upload/demo/${d}`), + component: import(`../components/vc-tree/demo/${d}`), } }