ant-design/components/tree/demo/draggable.md

139 lines
3.4 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 2
title:
2016-07-21 09:52:39 +08:00
zh-CN: 拖动示例
en-US: draggable
2016-03-31 09:40:55 +08:00
---
2016-01-07 19:05:55 +08:00
2016-07-21 09:52:39 +08:00
## zh-CN
2016-01-28 15:13:17 +08:00
将节点拖拽到其他节点内部或前后。
2016-01-07 19:05:55 +08:00
2016-07-21 09:52:39 +08:00
## en-US
Drag treeNode to insert after the other treeNode or insert into the other parent TreeNode.
```tsx
2016-01-07 19:05:55 +08:00
import { Tree } from 'antd';
import type { DataNode, TreeProps } from 'antd/lib/tree';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-06-27 15:55:04 +08:00
2016-01-07 19:05:55 +08:00
const x = 3;
const y = 2;
const z = 1;
const defaultData: DataNode[] = [];
2016-01-07 19:05:55 +08:00
const generateData = (_level: number, _preKey?: React.Key, _tns?: DataNode[]) => {
2016-01-07 19:05:55 +08:00
const preKey = _preKey || '0';
const tns = _tns || defaultData;
2016-01-07 19:05:55 +08:00
const children = [];
for (let i = 0; i < x; i++) {
const key = `${preKey}-${i}`;
tns.push({ title: key, key });
2016-01-07 19:05:55 +08:00
if (i < y) {
children.push(key);
}
}
if (_level < 0) {
return tns;
}
const level = _level - 1;
2016-01-07 19:05:55 +08:00
children.forEach((key, index) => {
tns[index].children = [];
return generateData(level, key, tns[index].children);
2016-01-07 19:05:55 +08:00
});
};
generateData(z);
const App: React.FC = () => {
const [gData, setGData] = useState(defaultData);
const [expandedKeys] = useState(['0-0', '0-0-0', '0-0-0-0']);
2018-06-27 15:55:04 +08:00
const onDragEnter: TreeProps['onDragEnter'] = info => {
2016-01-29 11:31:10 +08:00
console.log(info);
// expandedKeys 需要受控时设置
// setExpandedKeys(info.expandedKeys)
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const onDrop: TreeProps['onDrop'] = info => {
2016-01-07 19:05:55 +08:00
console.log(info);
chore: merge master into feature (#28751) * fix: Transfer dataSource cannot be immutable (#28675) close #28662 * docs: fix errors in example code (#28677) * ci: expand ie check (#28673) * ci: expand ie check * Update issue-open-check.yml * perf(📦): reduce @babel/runtime package size (#28678) * perf(📦): reduce @babel/runtime package size https://github.com/ant-design/antd-tools/commit/04cd73dea1206d1119847ffd7342b28b26d0babc * chore(:up:): upgrade @ant-design/react-slick to esm support version * upgrade @ant-design/tools * ci: add open condition (#28682) * fix(Slider): forcePopupAlign null when unmounted (#28699) * docs: Update overview.zh-CN.md (#28703) * docs: Update resources.en-US.md (#28701) * chore: bump rc-select to 12.1.0 (#28715) * fix: stylelint plugin (#28730) * Update package.json * perf(📦): upgrade rc-image to 5.x (#28727) * refactor: upgrade rc-image to 5.x reduce bundle size * upgrade rc-image * upgrade @ant-design/tools https://github.com/ant-design/antd-tools/pull/226 * rc-image 5.0.0 * fix image preview icon missing * refactor code * docs: example of synchronous rc-tree (#28648) * ci: fix outputs type 过程中的使用 string。奇怪啊,之前测试过的,今天点的时候发现不行了 * Update package.json * fix: site overflow cause sticky invalid (#28741) * fix: site overflow cause sticky invalid * disable auto scroll * chore: upgrade rc-dialog and rc-drawer (#28749) * chore: upgrade rc-dialog and rc-drawer (#28687) * chore: upgrade rc-dialog and rc-drawer * upgrade rc-util * update snapshots * upgrade rc-util * upgrade rc-util * update snapshots * upgrade rc-dialog * perf: remove duplicated rc-dialog Co-authored-by: 骗你是小猫咪 <darryshaw@gmail.com> Co-authored-by: bigbigbo <zxb141242@163.com> Co-authored-by: xrkffgg <xrkffgg@gmail.com> Co-authored-by: Yann Pringault <yann.pringault@gmail.com> Co-authored-by: godfather <greenday.wj@foxmail.com> Co-authored-by: Mateusz Wierzbicki <22788841+mateusz-wierzbicki@users.noreply.github.com> Co-authored-by: Kermit <kermitlx@outlook.com> Co-authored-by: AkiJoey <akijoey1010635951@gmail.com> Co-authored-by: qqabcv520 <605655316@qq.com> Co-authored-by: 骗你是小猫咪 <darryshaw@gmail.com>
2021-01-08 10:14:01 +08:00
const dropKey = info.node.key;
const dragKey = info.dragNode.key;
const dropPos = info.node.pos.split('-');
2017-05-02 16:45:32 +08:00
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
2018-10-25 11:19:17 +08:00
const loop = (
data: DataNode[],
key: React.Key,
callback: (node: DataNode, i: number, data: DataNode[]) => void,
) => {
for (let i = 0; i < data.length; i++) {
if (data[i].key === key) {
return callback(data[i], i, data);
2016-01-07 19:05:55 +08:00
}
if (data[i].children) {
loop(data[i].children!, key, callback);
2016-01-07 19:05:55 +08:00
}
}
2016-01-07 19:05:55 +08:00
};
const data = [...gData];
2018-10-25 11:19:17 +08:00
// Find dragObject
let dragObj: DataNode;
2016-01-07 19:05:55 +08:00
loop(data, dragKey, (item, index, arr) => {
arr.splice(index, 1);
dragObj = item;
});
2018-10-25 11:19:17 +08:00
if (!info.dropToGap) {
// Drop on the content
2019-05-07 14:57:32 +08:00
loop(data, dropKey, item => {
2018-10-25 11:19:17 +08:00
item.children = item.children || [];
// where to insert 示例添加到头部,可以是随意位置
item.children.unshift(dragObj);
2018-10-25 11:19:17 +08:00
});
} else if (
((info.node as any).props.children || []).length > 0 && // Has children
(info.node as any).props.expanded && // Is expanded
2019-05-07 14:57:32 +08:00
dropPosition === 1 // On the bottom gap
2018-10-25 11:19:17 +08:00
) {
2019-05-07 14:57:32 +08:00
loop(data, dropKey, item => {
2018-10-25 11:19:17 +08:00
item.children = item.children || [];
// where to insert 示例添加到头部,可以是随意位置
2018-10-25 11:19:17 +08:00
item.children.unshift(dragObj);
// in previous version, we use item.children.push(dragObj) to insert the
// item to the tail of the children
2018-10-25 11:19:17 +08:00
});
} else {
let ar: DataNode[] = [];
let i: number;
loop(data, dropKey, (_item, index, arr) => {
2016-01-07 19:05:55 +08:00
ar = arr;
i = index;
});
2017-05-02 16:45:32 +08:00
if (dropPosition === -1) {
ar.splice(i!, 0, dragObj!);
2017-05-02 16:45:32 +08:00
} else {
ar.splice(i! + 1, 0, dragObj!);
2017-05-02 16:45:32 +08:00
}
2016-01-07 19:05:55 +08:00
}
setGData(data);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
return (
<Tree
className="draggable-tree"
defaultExpandedKeys={expandedKeys}
draggable
blockNode
onDragEnter={onDragEnter}
onDrop={onDrop}
treeData={gData}
/>
);
};
2016-01-07 19:05:55 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```