Skip to content

Commit abc4894

Browse files
committed
feat: tree add activeKey
1 parent 5dd7a57 commit abc4894

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

components/vc-tree/Tree.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
shallowRef,
3232
watch,
3333
watchEffect,
34+
nextTick,
3435
} from 'vue';
3536
import initDefaultProps from '../_util/props-util/initDefaultProps';
3637
import type { CheckInfo, DraggableFn } from './props';
@@ -246,6 +247,26 @@ export default defineComponent({
246247
const scrollTo: ScrollTo = scroll => {
247248
listRef.value.scrollTo(scroll);
248249
};
250+
watch(
251+
() => props.activeKey,
252+
() => {
253+
if (props.activeKey !== undefined) {
254+
activeKey.value = props.activeKey;
255+
}
256+
},
257+
{ immediate: true },
258+
);
259+
watch(
260+
activeKey,
261+
val => {
262+
nextTick(() => {
263+
if (val !== null) {
264+
scrollTo({ key: val });
265+
}
266+
});
267+
},
268+
{ immediate: true, flush: 'post' },
269+
);
249270
// =========================== Expanded ===========================
250271
/** Set uncontrolled `expandedKeys`. This will also auto update `flattenNodes`. */
251272
const setExpandedKeys = (keys: Key[]) => {
@@ -270,16 +291,14 @@ export default defineComponent({
270291
currentMouseOverDroppableNodeKey = null;
271292
};
272293
// if onNodeDragEnd is called, onWindowDragEnd won't be called since stopPropagation() is called
273-
const onNodeDragEnd: NodeDragEventHandler = (event, node, outsideTree = false) => {
294+
const onNodeDragEnd: NodeDragEventHandler = (event, node) => {
274295
const { onDragend } = props;
275296

276297
dragState.dragOverNodeKey = null;
277298

278299
cleanDragState();
279300

280-
if (onDragend && !outsideTree) {
281-
onDragend({ event, node: node.eventData });
282-
}
301+
onDragend?.({ event, node: node.eventData });
283302

284303
dragNode = null;
285304
};
@@ -552,8 +571,8 @@ export default defineComponent({
552571
dropPosition: dropPosition + Number(posArr[posArr.length - 1]),
553572
};
554573

555-
if (onDrop && !outsideTree) {
556-
onDrop(dropResult);
574+
if (!outsideTree) {
575+
onDrop?.(dropResult);
557576
}
558577

559578
dragNode = null;
@@ -865,8 +884,9 @@ export default defineComponent({
865884
if (activeKey.value === newActiveKey) {
866885
return;
867886
}
868-
869-
activeKey.value = newActiveKey;
887+
if (props.activeKey !== undefined) {
888+
activeKey.value = newActiveKey;
889+
}
870890
if (newActiveKey !== null) {
871891
scrollTo({ key: newActiveKey });
872892
}

components/vc-tree/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// base rc-tree 5.3.7
1+
// base rc-tree 5.4.4
22
import type { TreeProps, TreeNodeProps } from './props';
33
import Tree from './Tree';
44
import TreeNode from './TreeNode';

components/vc-tree/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export type DraggableFn = (node: DataNode) => boolean;
121121
export const treeProps = () => ({
122122
prefixCls: String,
123123
focusable: { type: Boolean, default: undefined },
124+
activeKey: [Number, String] as PropType<Key>,
124125
tabindex: Number,
125126
children: PropTypes.any,
126127
treeData: { type: Array as PropType<DataNode[]> }, // Generate treeNode by children

components/vc-tree/util.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ export function convertDataToTree(
287287
const list = Array.isArray(treeData) ? treeData : [treeData];
288288
return list.map(({ children, ...props }): NodeElement => {
289289
const childrenNodes = convertDataToTree(children, processor);
290-
return <TreeNode {...processProps(props)}>{childrenNodes}</TreeNode>;
290+
return (
291+
<TreeNode key={props.key} {...processProps(props)}>
292+
{childrenNodes}
293+
</TreeNode>
294+
);
291295
});
292296
}
293297

0 commit comments

Comments
 (0)