Skip to content

Commit

Permalink
Merge pull request #2880 from xiamiao1121/feature/menu/#2872
Browse files Browse the repository at this point in the history
feat(menu): 新增支持配置字段别名(#2872)
  • Loading branch information
solarjoker authored Jun 28, 2024
2 parents 53b9321 + 5f664df commit bdca50e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-impalas-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/menu": minor
---

feat: 新增支持配置字段别名
5 changes: 5 additions & 0 deletions .changeset/fifty-teachers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(menu): 新增支持配置字段别名
31 changes: 20 additions & 11 deletions packages/ui/menu/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { cx, getPrefixCls } from '@hi-ui/classname'
import { MenuFoldOutlined, MenuUnfoldOutlined } from '@hi-ui/icons'
import { __DEV__ } from '@hi-ui/env'
import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state'
import { HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core'
import { HiBaseFieldNames, HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core'
import Tooltip from '@hi-ui/tooltip'
import { useUncontrolledToggle } from '@hi-ui/use-toggle'
import { getTreeNodesWithChildren } from '@hi-ui/tree-utils'
Expand All @@ -21,7 +21,7 @@ import { uuid } from '@hi-ui/use-id'
import { MenuDataItem, MenuFooterRenderProps } from './types'
import { MenuItem } from './MenuItem'
import MenuContext from './context'
import { getAncestorIds } from './util'
import { getAncestorIds, transformTreeData } from './util'

const MENU_PREFIX = getPrefixCls('menu')

Expand All @@ -41,6 +41,7 @@ export const Menu = forwardRef<HTMLDivElement | null, MenuProps>(
role = 'menu',
className,
data = NOOP_ARRAY,
fieldNames,
placement = 'vertical',
showCollapse = false,
// 仅对垂直模式有效
Expand Down Expand Up @@ -70,14 +71,18 @@ export const Menu = forwardRef<HTMLDivElement | null, MenuProps>(

const [activeParents, updateActiveParents] = useState(() => getAncestorIds(activeId, data))

const transformedData = useMemo(() => {
return transformTreeData(data, fieldNames)
}, [data, fieldNames])

useEffect(() => {
updateActiveParents(getAncestorIds(activeId, data))
}, [activeId, data])
updateActiveParents(getAncestorIds(activeId, transformedData))
}, [activeId, transformedData])

const [expandedIds, updateExpandedIds] = useUncontrolledState(
() => {
return defaultExpandAll
? getTreeNodesWithChildren(data).map((node) => node.id)
? getTreeNodesWithChildren(transformedData).map((node) => node.id)
: defaultExpandedIds
},
expandedIdsProp,
Expand Down Expand Up @@ -143,15 +148,15 @@ export const Menu = forwardRef<HTMLDivElement | null, MenuProps>(
const [tagMaxCount, setTagMaxCount] = useState(0)

const mergedTagList = useMemo(() => {
if (showVertical) return data
if (containerWidth < MIN_WIDTH) return data
return data.slice(0, Math.min(data.length, containerWidth / MIN_WIDTH))
}, [showVertical, data, containerWidth])
if (showVertical) return transformedData
if (containerWidth < MIN_WIDTH) return transformedData
return transformedData.slice(0, Math.min(transformedData.length, containerWidth / MIN_WIDTH))
}, [showVertical, transformedData, containerWidth])

const restTagList = useMemo(() => {
if (tagMaxCount > 0) return data.slice(tagMaxCount)
if (tagMaxCount > 0) return transformedData.slice(tagMaxCount)
return []
}, [data, tagMaxCount])
}, [transformedData, tagMaxCount])

const getTagWidth = useCallback(
(index: number) => {
Expand Down Expand Up @@ -303,6 +308,10 @@ export interface MenuProps extends Omit<HiBaseHTMLProps<'div'>, 'onClick'> {
* 菜单项数据列表
*/
data: MenuDataItem[]
/**
* 设置 data 中 id, title, disabled, children 对应的 key
*/
fieldNames?: HiBaseFieldNames
/**
* 默认激活的菜单项 id
*/
Expand Down
32 changes: 32 additions & 0 deletions packages/ui/menu/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { filterTree, cloneTree, getTreeNodesWithChildren } from '@hi-ui/tree-utils'
import { MenuDataItem } from './types'
import { HiBaseFieldNameKeys, HiBaseFieldNames } from '@hi-ui/core'
import React from 'react'

// 寻找某一节点的父节点
export const getParentId = (id: string | number, data: Record<string, any>[]): string | number => {
Expand Down Expand Up @@ -55,3 +57,33 @@ export const filterTreeData = (
}) ?? []
)
}

export const transformTreeData = (data: MenuDataItem[], fieldNames?: HiBaseFieldNames) => {
/**
* 转换对象
*/
const getKeyFields = (node: MenuDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof MenuDataItem]
}
return node[key]
}

/**
* 递归处理树形数组
*/
const traverseTreeNode = (node: MenuDataItem): MenuDataItem => {
const newNode: MenuDataItem = { ...node }
newNode.id = getKeyFields(newNode, 'id') as React.ReactText
newNode.title = getKeyFields(newNode, 'title')
newNode.icon = getKeyFields(newNode, 'icon' as HiBaseFieldNameKeys)
newNode.disabled = (getKeyFields(newNode, 'disabled') ?? false) as boolean
newNode.children = getKeyFields(newNode, 'children') as MenuDataItem[]
if (newNode.children) {
newNode.children = newNode.children.map(traverseTreeNode)
}
return newNode
}

return data.map(traverseTreeNode)
}

0 comments on commit bdca50e

Please sign in to comment.