Skip to content

feat(draggable): Support dynamic rendering draggable handler icon #844

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type AllowDrop<TreeDataType extends BasicDataNode = DataNode> = (

export type DraggableFn = (node: DataNode) => boolean;
export type DraggableConfig = {
icon?: React.ReactNode | false;
icon?: ((node: DataNode) => React.ReactNode) | React.ReactNode | false;
nodeDraggable?: DraggableFn;
};

Expand Down
17 changes: 7 additions & 10 deletions src/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
// @ts-ignore
import { TreeContext, TreeContextProps } from './contextTypes';
import Indent from './Indent';
import { TreeNodeProps } from './interface';
import { DataNode, TreeNodeProps } from './interface';
import getEntity from './utils/keyUtil';
import { convertNodePropsToEventData } from './utils/treeUtil';

Expand Down Expand Up @@ -293,13 +293,15 @@ class InternalTreeNode extends React.Component<InternalTreeNodeProps, TreeNodeSt
};

// ==================== Render: Drag Handler ====================
renderDragHandler = () => {
renderDragHandler = (data: DataNode) => {
const {
context: { draggable, prefixCls },
} = this.props;

return draggable?.icon ? (
<span className={`${prefixCls}-draggable-icon`}>{draggable.icon}</span>
<span className={`${prefixCls}-draggable-icon`}>
{typeof draggable.icon === 'function' ? draggable.icon(data) : draggable.icon}
</span>
) : null;
};

Expand Down Expand Up @@ -579,13 +581,8 @@ class InternalTreeNode extends React.Component<InternalTreeNodeProps, TreeNodeSt
{...ariaSelected}
{...dataOrAriaAttributeProps}
>
<Indent
prefixCls={prefixCls}
level={level}
isStart={isStart}
isEnd={isEnd}
/>
{this.renderDragHandler()}
<Indent prefixCls={prefixCls} level={level} isStart={isStart} isEnd={isEnd} />
{this.renderDragHandler(data)}
{this.renderSwitcher()}
{this.renderCheckbox()}
{this.renderSelector()}
Expand Down
29 changes: 29 additions & 0 deletions tests/TreeDraggable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,35 @@ describe('Tree Draggable', () => {
expect(container.querySelectorAll('.handler')).toHaveLength(2);
});

it('render handler icon render function', () => {
const { container } = render(
<Tree
draggable={{
icon: node => !node?.children && <span className="handler" />,
}}
defaultExpandAll
treeData={[
{
title: 'Parent',
key: 'parent',
children: [
{
title: 'Child',
key: 'child',
},
{
title: 'Child1',
key: 'child1',
},
],
},
]}
/>,
);

expect(container.querySelectorAll('.handler')).toHaveLength(2);
});

it('not break with fieldNames', () => {
const onDrop = jest.fn();

Expand Down
Loading