forked from XiaoMi/hiui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(list): 支持配置字段别名(XiaoMi#2908) * chore: 生成变更记录文件 * chore: 修改代码 * feat(list): 修改fieldNames类型 * feat(list): 代码规范 --------- Co-authored-by: xiamiao <[email protected]>
- Loading branch information
1 parent
ce9517d
commit 280dfc9
Showing
4 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@hi-ui/hiui": patch | ||
--- | ||
|
||
feat(list): 支持配置字段别名 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@hi-ui/list": minor | ||
--- | ||
|
||
feat: 支持配置字段别名 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { HiBaseFieldNameKeys, HiBaseFieldNames } from '@hi-ui/core' | ||
import { ListDataItem, ListActionPlacementEnum } from './types' | ||
|
||
export const transformData = ( | ||
data: ListDataItem[], | ||
fieldNames?: HiBaseFieldNames | ||
): ListDataItem[] => { | ||
const getKeyFields = (node: ListDataItem, key: HiBaseFieldNameKeys) => { | ||
if (fieldNames) { | ||
return node[(fieldNames[key] || key) as keyof ListDataItem] | ||
} | ||
return node[key as keyof ListDataItem] | ||
} | ||
|
||
const traverseNode = (node: ListDataItem): ListDataItem => { | ||
const newNode: ListDataItem = { ...node } | ||
|
||
newNode.title = getKeyFields(newNode, 'title' as HiBaseFieldNameKeys) | ||
newNode.description = getKeyFields(newNode, 'description' as HiBaseFieldNameKeys) | ||
newNode.extra = getKeyFields(newNode, 'extra' as HiBaseFieldNameKeys) | ||
newNode.avatar = getKeyFields(newNode, 'avatar' as HiBaseFieldNameKeys) | ||
newNode.action = getKeyFields(newNode, 'action' as HiBaseFieldNameKeys) | ||
newNode.actionPlacement = getKeyFields( | ||
newNode, | ||
'actionPlacement' as HiBaseFieldNameKeys | ||
) as ListActionPlacementEnum | ||
|
||
return newNode | ||
} | ||
|
||
return data.map((node) => traverseNode(node)) | ||
} |