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): addChildNode增加position参数 #2588

Merged
merged 1 commit into from
Sep 14, 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
5 changes: 5 additions & 0 deletions .changeset/grumpy-cherries-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat: 支持在孩子节点前面或后面添加节点
5 changes: 5 additions & 0 deletions .changeset/tame-bags-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/tree": patch
---

feat: 支持在孩子节点前面或后面添加节点
4 changes: 2 additions & 2 deletions packages/ui/tree/src/hooks/use-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export const useEdit = (
)

const addChildNode = useCallback(
(node: FlattedTreeNodeData) => {
(node: FlattedTreeNodeData, position: 0 | 1 = 0) => {
setTreeData((prev) => {
const nextTreeData = cloneTree(prev)
const nodeToAdd = genTreeNode()
addChildNodeById(nextTreeData, node.id, nodeToAdd, 0)
addChildNodeById(nextTreeData, node.id, nodeToAdd, position)
return nextTreeData
})
},
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/tree/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export type TreeEditActions = {
editNode: () => void
/**
* 执行添加子节点操作
*
* @param position 0 表示插入到孩子节点之前,1 表示之后
*/
addChildNode: () => void
addChildNode: (position?: 0 | 1) => void
/**
* 执行添加兄弟节点操作
*/
Expand Down
10 changes: 4 additions & 6 deletions packages/ui/tree/src/use-tree-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ interface EditableTreeNodeTitleProps {
onCancel: (node: FlattedTreeNodeData) => void
onSave: (savedNode: FlattedTreeNodeData) => Promise<void>
onDelete: (deletedNode: FlattedTreeNodeData) => Promise<void>
addChildNode: (node: FlattedTreeNodeData) => void
addChildNode: (node: FlattedTreeNodeData, position?: 0 | 1) => void
addSiblingNode: (node: FlattedTreeNodeData) => void
onExpand: (ids: React.ReactText[]) => void
placeholder?: string
Expand Down Expand Up @@ -267,9 +267,9 @@ const EditableNodeMenu = (props: EditableNodeMenuProps) => {
menuVisibleAction.off()
})
},
addChildNode: () => {
addChildNode: (position: 0 | 1 = 0) => {
menuVisibleAction.off()
addChildNode(node)
addChildNode(node, position)
// 展开子节点列表
onExpand(expandedIds.concat(node.id))
},
Expand Down Expand Up @@ -305,8 +305,6 @@ const EditableNodeMenu = (props: EditableNodeMenuProps) => {
return menuOptions
}, [menuOptionsProp, node])

if (!isArrayNonEmpty(menuOptions)) return null

return (
<div
className={cx(`${prefixCls}-action-wrap`, menuVisible && `${prefixCls}-action-wrap--visible`)}
Expand All @@ -317,7 +315,7 @@ const EditableNodeMenu = (props: EditableNodeMenuProps) => {
>
{isFunction(actionRender) ? (
actionRender(node, menuActionsRef.current)
) : (
) : !isArrayNonEmpty(menuOptions) ? null : (
<>
<IconButton
tabIndex={-1}
Expand Down
38 changes: 1 addition & 37 deletions packages/ui/tree/stories/action-render.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ActionRender = () => {

return id === 11 ? (
<Space>
<PlusOutlined onClick={() => editActions.addChildNode()} />
<PlusOutlined onClick={() => editActions.addChildNode(1)} />
<DuplicateOutlined onClick={() => editActions.addSiblingNode()} />
<EditOutlined onClick={() => editActions.editNode()} />
<PopConfirm
Expand All @@ -38,42 +38,6 @@ export const ActionRender = () => {
</Space>
) : null
}}
menuOptions={[
{
type: 'addChildNode',
title: '新建子节点',
},
{
type: 'addSiblingNode',
title: '新建兄弟节点',
},
{
// type: 'deleteNode',
title: '删除当前菜单',
onClick(node, action) {
action.closeMenu()

Modal.confirm({
title: '提示',
content: '确定删除吗?',
onConfirm: () => {
action.deleteNode()
},
})
},
},
{
type: 'editNode',
title: '编辑当前菜单',
},
{
title: 'Hello,自定义的菜单',
onClick(node, action) {
console.log(node)
action.closeMenu()
},
},
]}
data={[
{
id: 1,
Expand Down