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

fix(tree): 修复编辑模式下保存无效问题 (#3017) #3018

Merged
merged 2 commits into from
Oct 17, 2024
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
7 changes: 7 additions & 0 deletions .changeset/gorgeous-jars-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@hi-ui/tree": patch
"@hi-ui/hiui": patch
---

fix(tree): 修复编辑模式下保存无效问题 (#3017)
fix(tree): 修复拖拽和编辑模式下保存时异常问题 (#3022)
8 changes: 7 additions & 1 deletion packages/ui/tree/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const Tree = forwardRef<HTMLUListElement | null, TreeProps>(
iconRender,
onContextMenu,
expandOnSelect,
treeData,
}),
[
onNodeSelect,
Expand All @@ -211,6 +212,7 @@ export const Tree = forwardRef<HTMLUListElement | null, TreeProps>(
iconRender,
onContextMenu,
expandOnSelect,
treeData,
]
)

Expand Down Expand Up @@ -342,7 +344,11 @@ export interface TreeProps {
/**
* 节点结束拖拽时触发
*/
onDragEnd?: (evt: React.DragEvent, options: { dragNode: TreeNodeEventData }) => void
onDragEnd?: (
evt: React.DragEvent,
options: { dragNode: TreeNodeEventData },
treeData: TreeDataItem[]
) => void
/**
* 节点放开时触发,返回 true 表示允许更新拖拽后数据
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/tree/src/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const TreeNode = forwardRef<HTMLLIElement | null, TreeNodeProps>((props,
expandedIcon: expandIconContext,
leafIcon: leafIconContext,
expandOnSelect,
treeData,
} = useTreeContext()

const collapsedIcon = collapseIconContext || collapseIconProp
Expand Down Expand Up @@ -118,9 +119,9 @@ export const TreeNode = forwardRef<HTMLLIElement | null, TreeNodeProps>((props,
setDirection(null)
setIsDragging(false)

onDragEndContextLatest(evt, { dragNode: eventNodeRef.current })
onDragEndContextLatest(evt, { dragNode: eventNodeRef.current }, treeData)
},
[onDragEndContextLatest, eventNodeRef, dragNodeRef]
[dragNodeRef, onDragEndContextLatest, eventNodeRef, treeData]
)

const onDragLeaveContextLatest = useLatestCallback(onDragLeaveContext)
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/tree/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, useContext } from 'react'
import { TreeNodeEventData, TreeNodeDragDirection } from './types'
import { TreeNodeEventData, TreeNodeDragDirection, TreeDataItem } from './types'

interface TreeContext {
selectable?: boolean
Expand All @@ -8,7 +8,11 @@ interface TreeContext {
draggable?: boolean
dragNodeRef: React.MutableRefObject<TreeNodeEventData | null>
onDragStart?: (evt: React.DragEvent, options: { dragNode: TreeNodeEventData }) => void
onDragEnd?: (evt: React.DragEvent, options: { dragNode: TreeNodeEventData }) => void
onDragEnd?: (
evt: React.DragEvent,
options: { dragNode: TreeNodeEventData },
data: TreeDataItem[]
) => void
onDrop?: (
evt: React.DragEvent,
dragId: React.ReactText,
Expand Down Expand Up @@ -42,6 +46,7 @@ interface TreeContext {
onContextMenu?: (event: React.MouseEvent, node: TreeNodeEventData) => void
checkOnSelect: boolean
expandOnSelect?: boolean
treeData: TreeDataItem[]
}

const treeContext = createContext<TreeContext | null>(null)
Expand Down
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 @@ -89,7 +89,7 @@ export const useEdit = (
onDeleteRef.current?.(node, nextTreeData)
}
},
[treeData, setTreeData]
[treeData, onBeforeDeleteRef, setTreeData, onDeleteRef]
)

const onBeforeSaveRef = useLatestRef(onBeforeSave)
Expand Down Expand Up @@ -127,7 +127,7 @@ export const useEdit = (
onSaveRef.current?.(targetNode, nextTreeData)
}
},
[treeData, setTreeData]
[treeData, fieldNames, onBeforeSaveRef, setTreeData, onSaveRef]
)

return [saveEdit, cancelAddNode, deleteNode, addChildNode, addSiblingNode] as const
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/tree/src/utils/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TreeDataItem, FlattedTreeNodeData } from '../types'
const EMPTY_FIELD_NAMES = {} as any

export const getKey = (fieldNames: any, key: string): string => {
return fieldNames[key] || key
return fieldNames?.[key] || key
}

/**
Expand Down