Skip to content

Commit

Permalink
✨ feat(sortable-tree): 支持禁用拖拽功能
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 21, 2023
1 parent 6934467 commit 9a00235
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/SortableTree/components/TreeItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,26 @@ const TreeItem: FC<TreeItemProps> = memo(
const prefix = `${prefixCls}-node`;
const { styles, cx } = useStyles({ prefix, collapsed });

const [indentationWidth, selected, Content, Extra, withKeyboardSelectNode, deselectedNode] =
useStore(
(s) => [
s.indentationWidth,
s.selectedIds.includes(id),
s.renderContent,
s.renderExtra,
s.withKeyboardSelectNode,
s.deselectedAll,
],
shallow,
);
const [
indentationWidth,
selected,
Content,
Extra,
withKeyboardSelectNode,
deselectedNode,
disableDrag,
] = useStore(
(s) => [
s.indentationWidth,
s.selectedIds.includes(id),
s.renderContent,
s.renderExtra,
s.withKeyboardSelectNode,
s.deselectedAll,
s.disableDrag,
],
shallow,
);

const extraPanelVisible = showExtra && !clone;

Expand Down Expand Up @@ -199,12 +207,14 @@ const TreeItem: FC<TreeItemProps> = memo(
{...props}
>
<div className={`${prefix}-body`} ref={setDraggableNodeRef} style={style}>
<HandleAction
{...listeners}
{...attributes}
className={cx(`${prefix}-handle`, clone ? undefined : styles.handle)}
style={{ width: 12 }}
/>
{disableDrag ? null : (
<HandleAction
{...listeners}
{...attributes}
className={cx(`${prefix}-handle`, clone ? undefined : styles.handle)}
style={{ width: 12 }}
/>
)}
{onCollapse && (
<CollapseAction
onClick={(e) => {
Expand Down
2 changes: 2 additions & 0 deletions src/SortableTree/container/StoreUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const StoreUpdater = ({
hideRemove,
hideAdd,
indentationWidth,
disableDrag,
}: StoreUpdaterProps) => {
const storeApi = useStoreApi();

Expand All @@ -65,6 +66,7 @@ const StoreUpdater = ({
useStoreUpdater('indentationWidth', indentationWidth);
useStoreUpdater('hideAdd', hideAdd);
useStoreUpdater('hideRemove', hideRemove);
useStoreUpdater('disableDrag', disableDrag);

useStoreUpdater('onSelectedIdsChange', onSelectedIdsChange);

Expand Down
12 changes: 12 additions & 0 deletions src/SortableTree/demos/disableDrag.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* title: 禁用拖拽
*/
import { SortableTree } from '@ant-design/pro-editor';

import { initialData } from './data';

export default () => (
<div style={{ width: 340 }}>
<SortableTree defaultTreeData={initialData} disableDrag />
</div>
);
1 change: 1 addition & 0 deletions src/SortableTree/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ atomId: SortableTree
### 使用 renderContent 自定义渲染

<code src="./demos/renderContent.tsx" ></code>
<code src="./demos/disableDrag.tsx" ></code>

[//]: # '### 多选方案'
[//]: #
Expand Down
5 changes: 5 additions & 0 deletions src/SortableTree/store/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface ControlledState {
* 隐藏默认的删除按钮
*/
hideRemove?: boolean;
/**
* 禁用拖拽
*/
disableDrag?: boolean;
/**
* 缩进宽度
*/
Expand Down Expand Up @@ -96,6 +100,7 @@ export const initialState: State = {
renderContent: undefined,
renderExtra: undefined,
hideAdd: false,
disableDrag: false,
hideRemove: false,
...initialDragState,
};

0 comments on commit 9a00235

Please sign in to comment.