From 1a8154a34e57599d64c2d79b3bfb008d2e9b80b0 Mon Sep 17 00:00:00 2001 From: xiamiao Date: Fri, 21 Jun 2024 17:39:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(transfer):=20=E6=94=AF=E6=8C=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=AD=97=E6=AE=B5=E5=88=AB=E5=90=8D(#2909)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/transfer/src/Transfer.tsx | 11 +++++++++++ packages/ui/transfer/src/utils.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 packages/ui/transfer/src/utils.ts diff --git a/packages/ui/transfer/src/Transfer.tsx b/packages/ui/transfer/src/Transfer.tsx index 66d2187fd..ba35f6d8b 100644 --- a/packages/ui/transfer/src/Transfer.tsx +++ b/packages/ui/transfer/src/Transfer.tsx @@ -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) @@ -32,6 +33,7 @@ export const Transfer = forwardRef( targetSortType = 'default', pagination = false, data = NOOP_ARRAY, + fieldNames, defaultTargetIds = NOOP_ARRAY, targetIds: targetIdsProp, targetLimit, @@ -56,6 +58,11 @@ export const Transfer = forwardRef( 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) { @@ -376,6 +383,10 @@ export interface TransferProps * 穿梭框数据源 */ data: TransferDataItem[] + /** + * 设置data中各项值对应的key + **/ + fieldNames?: Record /** * 最大可穿梭上限 */ diff --git a/packages/ui/transfer/src/utils.ts b/packages/ui/transfer/src/utils.ts new file mode 100644 index 000000000..6f32edefc --- /dev/null +++ b/packages/ui/transfer/src/utils.ts @@ -0,0 +1,26 @@ +import { TransferDataItem } from "./types"; + + +export const transformData = (data: TransferDataItem[], + fieldNames: Record | 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[] + + }