Skip to content

Commit

Permalink
feat(transfer): 支持配置字段别名(#2909)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamiao committed Jun 21, 2024
1 parent e9e1a99 commit 1a8154a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/ui/transfer/src/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 { 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,11 @@ export const Transfer = forwardRef<HTMLDivElement | null, TransferProps>(
onChange
)

data = useMemo(() => {
if(data) return transformData(data, fieldNames)
else return data
}, [data,fieldNames])

const pageSize = useMemo(() => {
if (pagination === true) return 10
if (typeof pagination === 'object' && 'pageSize' in pagination) {
Expand Down Expand Up @@ -376,6 +383,10 @@ export interface TransferProps
* 穿梭框数据源
*/
data: TransferDataItem[]
/**
* 设置data中各项值对应的key
**/
fieldNames?: Record<string, string>
/**
* 最大可穿梭上限
*/
Expand Down
26 changes: 26 additions & 0 deletions packages/ui/transfer/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TransferDataItem } from "./types";


export const transformData = (data: TransferDataItem[],
fieldNames: Record<string, string> | undefined
): TransferDataItem[] => {
const getKeyFields = (node: any, key: any) => {
if(fieldNames ){
return node[(fieldNames as any)[key] || key]
}
return node[key]
}

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

newNode.id = getKeyFields(newNode, 'id')
newNode.title = getKeyFields(newNode, 'title')
newNode.disabled = getKeyFields(newNode, 'disabled')?? false

return newNode
}

return data.map(traverseNode) as TransferDataItem[]

}

0 comments on commit 1a8154a

Please sign in to comment.