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): 修复组件指定 fieldNames={{ title: 'name' }} 后,menuOptions 里 type 为 editNode 时会出现改不了节点的情况 #2966

Merged
merged 3 commits into from
Aug 15, 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
5 changes: 5 additions & 0 deletions .changeset/dull-birds-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/tree": patch
---

fix: 修复组件指定 fieldNames={{ title: 'name' }} 后,menuOptions 里 type 为 editNode 时会出现改不了节点的情况
5 changes: 5 additions & 0 deletions .changeset/odd-ladybugs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(tree): 修复组件指定 fieldNames={{ title: 'name' }} 后,menuOptions 里 type 为 editNode 时会出现改不了节点的情况
24 changes: 17 additions & 7 deletions packages/ui/tree/src/hooks/use-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TreeDataItem, FlattedTreeNodeData, TreeNodeType, TreeDataStatus } from
import { cloneTree } from '@hi-ui/tree-utils'
import { addChildNodeById, deleteNodeById, insertNodeById, uuid } from '../utils'
import { useLatestRef } from '@hi-ui/use-latest'
import { HiBaseFieldNames } from 'packages/core/core/lib/types'
import { getKey } from '../utils/tree'

const genTreeNode = () => ({ id: uuid(), title: '', type: 'add' } as FlattedTreeNodeData)

Expand All @@ -20,7 +22,8 @@ export const useEdit = (
level: number
) => boolean | Promise<boolean>,
onSave?: (savedNode: FlattedTreeNodeData, data: TreeDataItem[]) => void,
onDelete?: (deletedNode: FlattedTreeNodeData, data: TreeDataItem[]) => void
onDelete?: (deletedNode: FlattedTreeNodeData, data: TreeDataItem[]) => void,
fieldNames?: HiBaseFieldNames
) => {
const addSiblingNode = useCallback(
(node: FlattedTreeNodeData) => {
Expand Down Expand Up @@ -101,7 +104,7 @@ export const useEdit = (
}

const nextTreeData = cloneTree(treeData)
_saveEdit(targetNode, nextTreeData)
_saveEdit(targetNode, nextTreeData, fieldNames)

if (onBeforeSaveRef.current) {
const result = await onBeforeSaveRef.current(
Expand Down Expand Up @@ -131,16 +134,23 @@ export const useEdit = (
}

// 修改指定的 id 的 node 内容
const _saveEdit = (targetNode: TreeDataItem, treeData: TreeDataItem[]) => {
const _saveEdit = (
targetNode: TreeDataItem,
treeData: TreeDataItem[],
fieldNames?: HiBaseFieldNames
) => {
const { id, title } = targetNode

treeData.forEach((node) => {
if (node.id === id) {
node.title = title
if (
// @ts-ignore
node[getKey(fieldNames, 'id')] === id
) {
// @ts-ignore
node[getKey(fieldNames, 'title')] = title
delete node.type
} else {
if (node.children) {
_saveEdit(targetNode, node.children)
_saveEdit(targetNode, node.children, fieldNames)
}
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/tree/src/use-tree-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export const useTreeEditProps = <T extends EditableTreeProps>(
onBeforeSave,
onBeforeDelete,
onSave,
onDelete
onDelete,
fieldNames
)

const [editing, setEditing] = React.useState<boolean>(false)
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/tree/src/utils/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { baseFlattenTree } from '@hi-ui/tree-utils'
import { TreeDataItem, FlattedTreeNodeData } from '../types'

const EMPTY_FIELD_NAMES = {} as any

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

/**
* 扁平化树数据结构,基于前序遍历
*
Expand Down