Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tree组件选项支持配置是否可拖拽 #331

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/tree/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface TreeOption {
disabled?: boolean;
selectable?: boolean;
checkable?: boolean;
draggable?: boolean;
isLeaf?: boolean;
prefix?: string | (() => VNodeChild);
suffix?: string | (() => VNodeChild);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个代码补丁似乎是在 TreeOption 接口中添加了一个新属性 draggable。从代码上看,它似乎没有明显的潜在风险或问题。

由于缺少完整的上下文和详细的说明,我无法提供更深入的建议或改进。如果您有相关问题或其他具体需求,请随时提交。

Expand Down
4 changes: 2 additions & 2 deletions components/tree/props.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExtractPropTypes, PropType, InjectionKey, Ref } from 'vue';
import { CHECK_STRATEGY } from './const';
import { extractPropsDefaultValue } from '../_util/utils';
import { CHECK_STRATEGY } from './const';
import type { ExtractPropTypes, PropType, InjectionKey, Ref } from 'vue';

import type {
TreeOption,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 code patch 主要对代码的 import 顺序进行了调整,把 import { CHECK_STRATEGY } from './const'; 挪到了前面。这样做可以避免潜在的命名冲突,并提高代码可读性。

除此之外,我没有发现其他的错误风险和需要改进的地方。

Expand Down
5 changes: 3 additions & 2 deletions components/tree/useData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ref, watch } from 'vue';
import { isNil } from 'lodash-es';
import { isNil, isUndefined } from 'lodash-es';
import { concat } from '../_util/utils';
import type { InnerTreeOption, TreeNodeKey } from './interface';
import type { TreeProps } from './props';
import { concat } from '../_util/utils';

let uid = 1;
const getUid = () => {
Expand Down Expand Up @@ -51,6 +51,7 @@ export default ({ props, emit }: { props: TreeProps; emit: any }) => {
disabled: item.disabled,
selectable: item.selectable,
checkable: item.checkable,
draggable: isUndefined(item.draggable) ? true : item.draggable,
value,
label,
isLeaf,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此代码补丁对导入的依赖库lodash-es进行了修改,添加了isUndefined方法,并对树节点对象中的draggable属性做了判断,默认设置为true。从代码上来看,没有明显的bug风险。但是如果可拖拽功能并不是所有情况下都需要开启,那么可以根据实际需求,通过配置或者其他方式动态设置draggable属性值。

Expand Down
11 changes: 10 additions & 1 deletion components/tree/useDrag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { onUnmounted, ref } from 'vue';
import type { InnerTreeOption, TreeNodeKey, DropPosition } from './interface';
import getPrefixCls from '../_util/getPrefixCls';
import type { InnerTreeOption, TreeNodeKey, DropPosition } from './interface';

const prefixCls = getPrefixCls('tree-node');

Expand Down Expand Up @@ -35,13 +35,22 @@ export default ({

const handleDragstart = (value: TreeNodeKey, event: DragEvent) => {
const node = nodeList.get(value);
if (!node.draggable) {
// 阻止默认事件,默认事件会有拖拽效果
event.preventDefault();
return;
}
dragNode = node;
emit('dragstart', { node, event });
};

const handleDragend = (value: TreeNodeKey, event: DragEvent) => {
resetDragState();
const node = nodeList.get(value);
if (!node.draggable) {
event.preventDefault();
return;
}
emit('dragend', { node, event });
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码是Vue.js组件库中“TreeNode”的一部分。它从文件接口中导入InnerTreeOption、TreeNodeKey和DropPosition三个类型,并定义了一个名为prefixCls的常量。
代码中包含了两个处理拖放事件的函数handleDragstart和handleDragend,根据节点是否可拖拽来设置dragNode变量,确定是否触发'dragend'或'dragstart'事件,并在执行完成后重置状态。

在代码方面,可以看到差异性非常小,只有顺序上的改变,交换导入模块和获取样式类名的位置。因此如果没问题,并且符合规范,请忽略这个提交。

Expand Down
1 change: 0 additions & 1 deletion components/tree/useFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ref, Ref } from 'vue';
import { isFunction } from 'lodash-es';

import type { TreeNodeKey, InnerTreeOption } from './interface';
import type { TreeProps } from './props';

Expand Down
8 changes: 6 additions & 2 deletions components/tree/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export default ({
const index = values.indexOf(val);
if (props.multiple) {
if (index !== -1) {
props.cancelable && values.splice(index, 1);
if (props.cancelable) {
values.splice(index, 1);
}
} else {
values.push(val);
}
} else if (index !== -1) {
props.cancelable && values.splice(index, 1);
if (props.cancelable) {
values.splice(index, 1);
}
} else {
values[0] = val;
}
Expand Down
2 changes: 2 additions & 0 deletions docs/.vitepress/components/tree/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ app.use(FTree);
通过关键字过滤树节点。

--FILTER

### 前缀与后缀

放一些操作。
Expand Down Expand Up @@ -138,6 +139,7 @@ app.use(FTree);
| disabled? | 是否禁用节点 | boolean | `-` |
| selectable? | 是否禁用选中节点,默认为`Tree`组件的`selectable` | boolean | `-` |
| checkable? | 是否禁用勾选节点,默认为`Tree`组件的`checkable` | boolean | `-` |
| draggable? | 节点是否能被拖拽,默认为true | boolean | `true` |
| isLeaf? | 节点是否是叶节点,在 remote 模式下是必须的 | boolean | `false` |
| prefix? | 节点的前缀 | string / (() => VNodeChild) | `null` |
| suffix? | 节点的后缀 | string / (() => VNodeChild) | `null` |