Skip to content

Commit

Permalink
feat(transfer): 支持配置字段别名(#2909)
Browse files Browse the repository at this point in the history
* feat(transfer): 支持配置字段别名(#2909)

* chore: 生成变更记录文件

* feat(transfer): 代码规范

---------

Co-authored-by: xiamiao <[email protected]>
  • Loading branch information
xiamiao1121 and xiamiao authored Jun 28, 2024
1 parent 2d8fd92 commit ce9517d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-ligers-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/transfer": minor
---

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

feat(transfer): 支持配置字段别名
19 changes: 15 additions & 4 deletions packages/ui/transfer/src/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { TransferDataItem } from './types'
import { TransferProvider } from './context'
import { useCheck } from '@hi-ui/use-check'
import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state'
import { HiBaseHTMLProps, useLocaleContext } from '@hi-ui/core'
import { HiBaseFieldNames, HiBaseHTMLProps, useLocaleContext } from '@hi-ui/core'
import { transformData } from './utils'

const _role = 'transfer'
const _prefix = getPrefixCls(_role)
Expand All @@ -32,6 +33,7 @@ export const Transfer = forwardRef<HTMLDivElement | null, TransferProps>(
targetSortType = 'default',
pagination = false,
data = NOOP_ARRAY,
fieldNames,
defaultTargetIds = NOOP_ARRAY,
targetIds: targetIdsProp,
targetLimit,
Expand All @@ -56,6 +58,8 @@ export const Transfer = forwardRef<HTMLDivElement | null, TransferProps>(
onChange
)

const transformedData = useMemo(() => transformData(data, fieldNames), [data, fieldNames])

const pageSize = useMemo(() => {
if (pagination === true) return 10
if (typeof pagination === 'object' && 'pageSize' in pagination) {
Expand All @@ -79,7 +83,10 @@ export const Transfer = forwardRef<HTMLDivElement | null, TransferProps>(
allowCheck,
})

const [sourceList, targetList] = useMemo(() => splitData(data, targetIds), [data, targetIds])
const [sourceList, targetList] = useMemo(() => splitData(transformedData, targetIds), [
transformedData,
targetIds,
])

const isOverflowed = useMemo(() => {
if (targetLimit === undefined) return false
Expand Down Expand Up @@ -112,11 +119,11 @@ export const Transfer = forwardRef<HTMLDivElement | null, TransferProps>(
setTargetCheckedIds([])
}

const moveData = data.filter((item) => checkedIds.indexOf(item.id) !== -1)
const moveData = transformedData.filter((item) => checkedIds.indexOf(item.id) !== -1)
tryChangeTargetIds(nextTargetIds, direction, moveData)
},
[
data,
transformedData,
tryChangeTargetIds,
setSourceCheckedIds,
setTargetCheckedIds,
Expand Down Expand Up @@ -376,6 +383,10 @@ export interface TransferProps
* 穿梭框数据源
*/
data: TransferDataItem[]
/**
* 设置data中各项值对应的key
**/
fieldNames?: HiBaseFieldNames
/**
* 最大可穿梭上限
*/
Expand Down
27 changes: 27 additions & 0 deletions packages/ui/transfer/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HiBaseFieldNameKeys, HiBaseFieldNames } from '@hi-ui/core'
import { TransferDataItem } from './types'
import React from 'react'

export const transformData = (
data: TransferDataItem[],
fieldNames?: HiBaseFieldNames
): TransferDataItem[] => {
const getKeyFields = (node: TransferDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof TransferDataItem]
}
return node[key as keyof TransferDataItem]
}

const traverseNode = (node: TransferDataItem): TransferDataItem => {
const newNode = { ...node }

newNode.id = getKeyFields(newNode, 'id') as React.ReactText
newNode.title = getKeyFields(newNode, 'title') as React.ReactNode
newNode.disabled = (getKeyFields(newNode, 'disabled') ?? false) as boolean

return newNode
}

return data.map(traverseNode) as TransferDataItem[]
}

0 comments on commit ce9517d

Please sign in to comment.