Skip to content

Commit

Permalink
feat(list): 代码规范
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamiao committed Jun 26, 2024
1 parent b99972b commit ae9e791
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
10 changes: 6 additions & 4 deletions packages/ui/list/src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ export const List = forwardRef<HTMLDivElement | null, ListProps>(
},
ref
) => {

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

const cls = cx(prefixCls, className, {
[`${prefixCls}--bordered`]: bordered,
Expand All @@ -78,9 +80,9 @@ export const List = forwardRef<HTMLDivElement | null, ListProps>(

return (
<div ref={ref} role={role} className={cls} {...rest}>
{data && data.length > 0 ? (
{transformedData && transformedData.length > 0 ? (
<ul className={cx(`${prefixCls}__wrapper`)}>
{data.map((item, index) => {
{transformedData.map((item, index) => {
return renderListItem(item, index)
})}
</ul>
Expand Down
45 changes: 22 additions & 23 deletions packages/ui/list/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { HiBaseFieldNames } from "@hi-ui/core"
import { ListDataItem } from "./types"
import { HiBaseFieldNameKeys, HiBaseFieldNames } from '@hi-ui/core'
import { ListDataItem, ListActionPlacementEnum } from './types'

export const transformData = (
export const transformData = (
data: ListDataItem[],
fieldNames?: HiBaseFieldNames
) : ListDataItem[] => {

const getKeyFields = (node: any, key: any)=> {
if(fieldNames){
return node[(fieldNames as any)[key] || key]
}
return node[key]
): 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}
const traverseNode = (node: ListDataItem): ListDataItem => {
const newNode: ListDataItem = { ...node }

newNode.title = getKeyFields(newNode, 'title')
newNode.description = getKeyFields(newNode, 'description')
newNode.extra = getKeyFields(newNode, 'extra')
newNode.avatar = getKeyFields(newNode, 'avatar')
newNode.action = getKeyFields(newNode, 'action')
newNode.actionPlacement = getKeyFields(newNode, 'actionPlacement')
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 newNode
}

return data.map(node => traverseNode(node))


return data.map((node) => traverseNode(node))
}

0 comments on commit ae9e791

Please sign in to comment.