From 4cda67d1e101a2ebd235b70a1c3c0da003e93fbb Mon Sep 17 00:00:00 2001 From: xiamiao Date: Wed, 26 Jun 2024 14:17:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(filter):=20=E5=A2=9E=E5=8A=A0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/filter/src/use-filter.ts | 6 +++--- packages/ui/filter/src/utils.ts | 31 ++++++++++++++++------------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/ui/filter/src/use-filter.ts b/packages/ui/filter/src/use-filter.ts index 18dd0dc3b..2469c146b 100644 --- a/packages/ui/filter/src/use-filter.ts +++ b/packages/ui/filter/src/use-filter.ts @@ -18,7 +18,7 @@ export const useFilter = ({ onChange, ...rest }: UseFilterProps) => { - dataProp = useMemo(() => transformTreeData(dataProp, fieldNames), [dataProp, fieldNames]) + const transformedData = useMemo(() => transformTreeData(dataProp, fieldNames), [dataProp, fieldNames]) // 选中的级联路径 id 列表 const [value, tryChangeValue] = useUncontrolledState(defaultValue, valueProp, onChange) @@ -26,7 +26,7 @@ export const useFilter = ({ * 根据级联路径生成面板数据 */ const menusData = useMemo(() => { - let lastMenu = dataProp + let lastMenu = transformedData const menus = [lastMenu] const menuPathLength = value.length @@ -45,7 +45,7 @@ export const useFilter = ({ } return menus - }, [dataProp, value]) + }, [transformedData, value]) const menusWithLabel = useMemo(() => { return labels.map((label, depth) => { diff --git a/packages/ui/filter/src/utils.ts b/packages/ui/filter/src/utils.ts index f2de9a7c0..9b0f50947 100644 --- a/packages/ui/filter/src/utils.ts +++ b/packages/ui/filter/src/utils.ts @@ -1,21 +1,26 @@ -import { HiBaseFieldNames } from '@hi-ui/core' -import { FilterDataItem } from './types' +import { HiBaseFieldNameKeys, HiBaseFieldNames } from "@hi-ui/core" +import { FilterDataItem } from "./types" -export const transformTreeData = (data: FilterDataItem[] , fieldNames?: HiBaseFieldNames) : FilterDataItem[] => { - const getKeyFields = (node: any, key: any) => { - if(fieldNames ){ - return node[(fieldNames as any)[key] || key] +export const transformTreeData = ( + data: FilterDataItem[], + fieldNames?: HiBaseFieldNames +): FilterDataItem[] => { + const getKeyFields = (node: FilterDataItem, key: HiBaseFieldNameKeys) => { + if (fieldNames) { + return node[(fieldNames[key] || key) as keyof FilterDataItem] } return node[key] } - const traverseNode = (node: FilterDataItem) : FilterDataItem => { - const newNode = { ...node } - newNode.id = getKeyFields(newNode, 'id') - newNode.title = getKeyFields(newNode, 'title') - newNode.disabled = getKeyFields(newNode, 'disabled')?? false - newNode.children = getKeyFields(newNode, 'children') - if(newNode.children){ + const traverseNode = (node: FilterDataItem): FilterDataItem => { + const newNode: FilterDataItem = { ...node } + + newNode.id = getKeyFields(newNode, "id") as React.ReactText + newNode.title = getKeyFields(newNode, "title") as React.ReactText + newNode.disabled = (getKeyFields(newNode, "disabled") ?? false) as boolean + newNode.children = getKeyFields(newNode, "children") as FilterDataItem[] + + if (newNode.children) { newNode.children = newNode.children.map(traverseNode) } return newNode