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(dropdown): 支持配置字段别名(#2883) #2889

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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/fuzzy-buttons-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(dropdown): 支持配置字段别名
5 changes: 5 additions & 0 deletions .changeset/wise-walls-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/dropdown": minor
---

feat: 支持配置字段别名
19 changes: 15 additions & 4 deletions packages/ui/dropdown/src/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { cloneElement, forwardRef } from 'react'
import React, { cloneElement, forwardRef, useMemo } from 'react'
import { cx, getPrefixCls } from '@hi-ui/classname'
import { __DEV__ } from '@hi-ui/env'
import { HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core'
import { HiBaseFieldNames, HiBaseHTMLProps, HiBaseSizeEnum } from '@hi-ui/core'
import { PopperOverlayProps, Popper, PopperProps } from '@hi-ui/popper'
import { DropDownProvider, useDropDownContext } from './context'
import { useDropdown, UseDropdownProps } from './use-dropdown'
import { isArray, isArrayNonEmpty } from '@hi-ui/type-assertion'
import Button, { ButtonGroup } from '@hi-ui/button'
import { DownOutlined } from '@hi-ui/icons'
import { DropdownDataItem } from './types'
import { transformData } from './utils'

const _role = 'dropdown'
const _prefix = getPrefixCls(_role)
Expand All @@ -25,6 +26,7 @@ export const Dropdown = forwardRef<HTMLDivElement | null, DropdownProps>(
className,
children: triggerButton,
data = DEFAULT_DATA,
fieldNames,
title,
type = 'text',
onClick,
Expand All @@ -35,6 +37,11 @@ export const Dropdown = forwardRef<HTMLDivElement | null, DropdownProps>(
},
ref
) => {
const transformedData = useMemo(() => {
if (data) return transformData(data, fieldNames)
else return data
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
}, [data, fieldNames])

const { rootProps, ...providedValue } = useDropdown(rest)

const { getMenuProps, getTriggerProps, disabled, menuVisibleAction } = providedValue
Expand Down Expand Up @@ -111,7 +118,7 @@ export const Dropdown = forwardRef<HTMLDivElement | null, DropdownProps>(
<div ref={ref} role={role} className={cls} {...rootProps}>
{renderButton()}

{isArrayNonEmpty(data) ? (
{isArrayNonEmpty(transformedData) ? (
<DropdownMenu
{...getMenuProps({
overlay: {
Expand All @@ -121,7 +128,7 @@ export const Dropdown = forwardRef<HTMLDivElement | null, DropdownProps>(
})}
size={size}
>
{dig(data)}
{dig(transformedData)}
</DropdownMenu>
) : null}
</div>
Expand All @@ -139,6 +146,10 @@ export interface DropdownProps extends Omit<HiBaseHTMLProps<'div'>, 'onClick'>,
* 下拉菜单数据项
*/
data?: DropdownDataItem[]
/**
* 设置data 中id, title, href, target, disabled, split 等属性对应的 key
*/
fieldNames?: HiBaseFieldNames
/**
* 设置下拉面板宽度
*/
Expand Down
38 changes: 36 additions & 2 deletions packages/ui/dropdown/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isArray } from '@hi-ui/type-assertion'
import { DropdownTriggerActionEnum } from '../types'

import { DropdownTriggerActionEnum, DropdownDataItem } from '../types'
import { HiBaseFieldNameKeys, HiBaseFieldNames } from '@hi-ui/core'
import React from 'react'
/**
* 抹平 trigger 结构为数组
*
Expand All @@ -10,3 +11,36 @@ import { DropdownTriggerActionEnum } from '../types'
export const normalizeTrigger = (
trigger: DropdownTriggerActionEnum | DropdownTriggerActionEnum[]
) => (isArray(trigger) ? Array.from(new Set(trigger)) : [trigger])

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

const traverseNode = (node: DropdownDataItem): DropdownDataItem => {
const newNode: DropdownDataItem = { ...node }
newNode.id = getKeyFields(newNode, 'id') as React.ReactText
newNode.title = getKeyFields(newNode, 'title')
newNode.href = getKeyFields(newNode, 'href' as HiBaseFieldNameKeys) as string
newNode.disabled = (getKeyFields(newNode, 'disabled') ?? false) as boolean
newNode.split = (getKeyFields(newNode, 'split' as HiBaseFieldNameKeys) ?? false) as boolean
newNode.target = getKeyFields(newNode, 'target' as HiBaseFieldNameKeys) as
| '_self'
| '_blank'
| '_parent'
| '_top'

return newNode
}

return data.map(traverseNode)
}