Skip to content

Commit 5cf57f4

Browse files
committed
feat: tree add blockNode selectable
1 parent a078105 commit 5cf57f4

File tree

14 files changed

+259
-151
lines changed

14 files changed

+259
-151
lines changed

build/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
dev: {
3-
componentName: 'tree-select', // dev components
3+
componentName: 'tree', // dev components
44
},
55
};

components/tree/DirectoryTree.jsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import PropTypes from '../_util/vue-types';
44
import warning from '../_util/warning';
55
import { conductExpandParent, convertTreeToEntities } from '../vc-tree/src/util';
66
import Tree, { TreeProps } from './Tree';
7-
import { calcRangeKeys, getFullKeyList } from './util';
7+
import {
8+
calcRangeKeys,
9+
getFullKeyList,
10+
convertDirectoryKeysToNodes,
11+
getFullKeyListByTreeData,
12+
} from './util';
813
import Icon from '../icon';
914
import BaseMixin from '../_util/BaseMixin';
1015
import {
@@ -64,7 +69,11 @@ export default {
6469

6570
// Expanded keys
6671
if (defaultExpandAll) {
67-
state._expandedKeys = getFullKeyList(this.$slots.default);
72+
if (props.treeData) {
73+
state._expandedKeys = getFullKeyListByTreeData(props.treeData);
74+
} else {
75+
state._expandedKeys = getFullKeyList(this.$slots.default);
76+
}
6877
} else if (defaultExpandParent) {
6978
state._expandedKeys = conductExpandParent(expandedKeys || defaultExpandedKeys, keyEntities);
7079
} else {
@@ -125,6 +134,13 @@ export default {
125134
const { eventKey = '' } = node;
126135

127136
const newState = {};
137+
138+
// We need wrap this event since some value is not same
139+
const newEvent = {
140+
...event,
141+
selected: true, // Directory selected always true
142+
};
143+
128144
// Windows / Mac single pick
129145
const ctrlPick = nativeEvent.ctrlKey || nativeEvent.metaKey;
130146
const shiftPick = nativeEvent.shiftKey;
@@ -136,6 +152,7 @@ export default {
136152
newSelectedKeys = keys;
137153
this.lastSelectedKey = eventKey;
138154
this.cachedSelectedKeys = newSelectedKeys;
155+
newEvent.selectedNodes = convertDirectoryKeysToNodes(children, newSelectedKeys);
139156
} else if (multiple && shiftPick) {
140157
// Shift click
141158
newSelectedKeys = Array.from(
@@ -144,16 +161,18 @@ export default {
144161
...calcRangeKeys(children, expandedKeys, eventKey, this.lastSelectedKey),
145162
]),
146163
);
164+
newEvent.selectedNodes = convertDirectoryKeysToNodes(children, newSelectedKeys);
147165
} else {
148166
// Single click
149167
newSelectedKeys = [eventKey];
150168
this.lastSelectedKey = eventKey;
151169
this.cachedSelectedKeys = newSelectedKeys;
170+
newEvent.selectedNodes = [event.node];
152171
}
153172
newState._selectedKeys = newSelectedKeys;
154173

155174
this.$emit('update:selectedKeys', newSelectedKeys);
156-
this.$emit('select', newSelectedKeys, event);
175+
this.$emit('select', newSelectedKeys, newEvent);
157176

158177
this.setUncontrolledState(newState);
159178
},

components/tree/Tree.jsx

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function TreeProps() {
9393
* 替换treeNode中 title,key,children字段为treeData中对应的字段
9494
*/
9595
replaceFields: PropTypes.object,
96+
blockNode: PropTypes.bool,
9697
};
9798
}
9899

@@ -111,6 +112,7 @@ export default {
111112
on: animation,
112113
props: { appear: null },
113114
},
115+
blockNode: false,
114116
}),
115117
inject: {
116118
configProvider: { default: () => ConfigConsumerProps },
@@ -128,32 +130,27 @@ export default {
128130
if (loading) {
129131
return <Icon type="loading" class={`${prefixCls}-switcher-loading-icon`} />;
130132
}
131-
if (showLine) {
132-
if (isLeaf) {
133-
return <Icon type="file" class={`${prefixCls}-switcher-line-icon`} />;
134-
}
135-
return (
136-
<Icon
137-
type={expanded ? 'minus-square' : 'plus-square'}
138-
class={`${prefixCls}-switcher-line-icon`}
139-
theme="outlined"
140-
/>
141-
);
142-
} else {
143-
const switcherCls = `${prefixCls}-switcher-icon`;
144-
if (isLeaf) {
145-
return null;
146-
} else if (switcherIcon) {
147-
const switcherOriginCls = getClass(switcherIcon[0]);
148-
return cloneElement(switcherIcon, {
149-
class: {
150-
[switcherCls]: true,
151-
},
152-
});
153-
} else {
154-
return <Icon type="caret-down" class={`${prefixCls}-switcher-icon`} theme="filled" />;
155-
}
133+
134+
if (isLeaf) {
135+
return showLine ? <Icon type="file" class={`${prefixCls}-switcher-line-icon`} /> : null;
156136
}
137+
const switcherCls = `${prefixCls}-switcher-icon`;
138+
if (switcherIcon) {
139+
return cloneElement(switcherIcon, {
140+
class: {
141+
[switcherCls]: true,
142+
},
143+
});
144+
}
145+
return showLine ? (
146+
<Icon
147+
type={expanded ? 'minus-square' : 'plus-square'}
148+
class={`${prefixCls}-switcher-line-icon`}
149+
theme="outlined"
150+
/>
151+
) : (
152+
<Icon type="caret-down" class={switcherCls} theme="filled" />
153+
);
157154
},
158155
updateTreeData(treeData) {
159156
const { $slots, $scopedSlots } = this;
@@ -166,12 +163,17 @@ export default {
166163
const treeNodeProps = {
167164
...restProps,
168165
icon:
169-
$slots[slots.icon] ||
170166
($scopedSlots[scopedSlots.icon] && $scopedSlots[scopedSlots.icon](item)) ||
167+
$slots[slots.icon] ||
171168
restProps.icon,
169+
switcherIcon:
170+
($scopedSlots[scopedSlots.switcherIcon] &&
171+
$scopedSlots[scopedSlots.switcherIcon](item)) ||
172+
$slots[slots.switcherIcon] ||
173+
restProps.switcherIcon,
172174
title:
173-
$slots[slots.title] ||
174175
($scopedSlots[scopedSlots.title] && $scopedSlots[scopedSlots.title](item)) ||
176+
$slots[slots.title] ||
175177
restProps[replaceFields.title],
176178
dataRef: item,
177179
on,
@@ -188,7 +190,8 @@ export default {
188190
},
189191
render() {
190192
const props = getOptionProps(this);
191-
const { prefixCls: customizePrefixCls, showIcon, treeNodes } = props;
193+
const { $slots, $scopedSlots } = this;
194+
const { prefixCls: customizePrefixCls, showIcon, treeNodes, blockNode } = props;
192195
const getPrefixCls = this.configProvider.getPrefixCls;
193196
const prefixCls = getPrefixCls('tree', customizePrefixCls);
194197
const switcherIcon = getComponentFromProp(this, 'switcherIcon');
@@ -202,13 +205,16 @@ export default {
202205
...props,
203206
prefixCls,
204207
checkable: checkable ? <span class={`${prefixCls}-checkbox-inner`} /> : checkable,
205-
children: filterEmpty(this.$slots.default || []),
208+
children: filterEmpty($scopedSlots.default ? $scopedSlots.default() : $slots.default),
206209
__propsSymbol__: Symbol(),
207210
switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, switcherIcon, nodeProps),
208211
},
209212
on: getListeners(this),
210213
ref: 'tree',
211-
class: !showIcon && `${prefixCls}-icon-hide`,
214+
class: {
215+
[`${prefixCls}-icon-hide`]: !showIcon,
216+
[`${prefixCls}-block-node`]: blockNode,
217+
},
212218
};
213219
if (treeData) {
214220
vcTreeProps.props.treeData = treeData;

0 commit comments

Comments
 (0)