feat: tree add activeKey
parent
5dd7a57734
commit
abc4894605
|
@ -31,6 +31,7 @@ import {
|
||||||
shallowRef,
|
shallowRef,
|
||||||
watch,
|
watch,
|
||||||
watchEffect,
|
watchEffect,
|
||||||
|
nextTick,
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
||||||
import type { CheckInfo, DraggableFn } from './props';
|
import type { CheckInfo, DraggableFn } from './props';
|
||||||
|
@ -246,6 +247,26 @@ export default defineComponent({
|
||||||
const scrollTo: ScrollTo = scroll => {
|
const scrollTo: ScrollTo = scroll => {
|
||||||
listRef.value.scrollTo(scroll);
|
listRef.value.scrollTo(scroll);
|
||||||
};
|
};
|
||||||
|
watch(
|
||||||
|
() => props.activeKey,
|
||||||
|
() => {
|
||||||
|
if (props.activeKey !== undefined) {
|
||||||
|
activeKey.value = props.activeKey;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true },
|
||||||
|
);
|
||||||
|
watch(
|
||||||
|
activeKey,
|
||||||
|
val => {
|
||||||
|
nextTick(() => {
|
||||||
|
if (val !== null) {
|
||||||
|
scrollTo({ key: val });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ immediate: true, flush: 'post' },
|
||||||
|
);
|
||||||
// =========================== Expanded ===========================
|
// =========================== Expanded ===========================
|
||||||
/** Set uncontrolled `expandedKeys`. This will also auto update `flattenNodes`. */
|
/** Set uncontrolled `expandedKeys`. This will also auto update `flattenNodes`. */
|
||||||
const setExpandedKeys = (keys: Key[]) => {
|
const setExpandedKeys = (keys: Key[]) => {
|
||||||
|
@ -270,16 +291,14 @@ export default defineComponent({
|
||||||
currentMouseOverDroppableNodeKey = null;
|
currentMouseOverDroppableNodeKey = null;
|
||||||
};
|
};
|
||||||
// if onNodeDragEnd is called, onWindowDragEnd won't be called since stopPropagation() is called
|
// if onNodeDragEnd is called, onWindowDragEnd won't be called since stopPropagation() is called
|
||||||
const onNodeDragEnd: NodeDragEventHandler = (event, node, outsideTree = false) => {
|
const onNodeDragEnd: NodeDragEventHandler = (event, node) => {
|
||||||
const { onDragend } = props;
|
const { onDragend } = props;
|
||||||
|
|
||||||
dragState.dragOverNodeKey = null;
|
dragState.dragOverNodeKey = null;
|
||||||
|
|
||||||
cleanDragState();
|
cleanDragState();
|
||||||
|
|
||||||
if (onDragend && !outsideTree) {
|
onDragend?.({ event, node: node.eventData });
|
||||||
onDragend({ event, node: node.eventData });
|
|
||||||
}
|
|
||||||
|
|
||||||
dragNode = null;
|
dragNode = null;
|
||||||
};
|
};
|
||||||
|
@ -552,8 +571,8 @@ export default defineComponent({
|
||||||
dropPosition: dropPosition + Number(posArr[posArr.length - 1]),
|
dropPosition: dropPosition + Number(posArr[posArr.length - 1]),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (onDrop && !outsideTree) {
|
if (!outsideTree) {
|
||||||
onDrop(dropResult);
|
onDrop?.(dropResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
dragNode = null;
|
dragNode = null;
|
||||||
|
@ -865,8 +884,9 @@ export default defineComponent({
|
||||||
if (activeKey.value === newActiveKey) {
|
if (activeKey.value === newActiveKey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (props.activeKey !== undefined) {
|
||||||
activeKey.value = newActiveKey;
|
activeKey.value = newActiveKey;
|
||||||
|
}
|
||||||
if (newActiveKey !== null) {
|
if (newActiveKey !== null) {
|
||||||
scrollTo({ key: newActiveKey });
|
scrollTo({ key: newActiveKey });
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// base rc-tree 5.3.7
|
// base rc-tree 5.4.4
|
||||||
import type { TreeProps, TreeNodeProps } from './props';
|
import type { TreeProps, TreeNodeProps } from './props';
|
||||||
import Tree from './Tree';
|
import Tree from './Tree';
|
||||||
import TreeNode from './TreeNode';
|
import TreeNode from './TreeNode';
|
||||||
|
|
|
@ -121,6 +121,7 @@ export type DraggableFn = (node: DataNode) => boolean;
|
||||||
export const treeProps = () => ({
|
export const treeProps = () => ({
|
||||||
prefixCls: String,
|
prefixCls: String,
|
||||||
focusable: { type: Boolean, default: undefined },
|
focusable: { type: Boolean, default: undefined },
|
||||||
|
activeKey: [Number, String] as PropType<Key>,
|
||||||
tabindex: Number,
|
tabindex: Number,
|
||||||
children: PropTypes.any,
|
children: PropTypes.any,
|
||||||
treeData: { type: Array as PropType<DataNode[]> }, // Generate treeNode by children
|
treeData: { type: Array as PropType<DataNode[]> }, // Generate treeNode by children
|
||||||
|
|
|
@ -287,7 +287,11 @@ export function convertDataToTree(
|
||||||
const list = Array.isArray(treeData) ? treeData : [treeData];
|
const list = Array.isArray(treeData) ? treeData : [treeData];
|
||||||
return list.map(({ children, ...props }): NodeElement => {
|
return list.map(({ children, ...props }): NodeElement => {
|
||||||
const childrenNodes = convertDataToTree(children, processor);
|
const childrenNodes = convertDataToTree(children, processor);
|
||||||
return <TreeNode {...processProps(props)}>{childrenNodes}</TreeNode>;
|
return (
|
||||||
|
<TreeNode key={props.key} {...processProps(props)}>
|
||||||
|
{childrenNodes}
|
||||||
|
</TreeNode>
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue