Skip to content

Commit

Permalink
feat(filter): 增加类型约束
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamiao committed Jun 26, 2024
1 parent 57b578a commit 4cda67d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/ui/filter/src/use-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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)

/**
* 根据级联路径生成面板数据
*/
const menusData = useMemo(() => {
let lastMenu = dataProp
let lastMenu = transformedData
const menus = [lastMenu]

const menuPathLength = value.length
Expand All @@ -45,7 +45,7 @@ export const useFilter = ({
}

return menus
}, [dataProp, value])
}, [transformedData, value])

const menusWithLabel = useMemo(() => {
return labels.map((label, depth) => {
Expand Down
31 changes: 18 additions & 13 deletions packages/ui/filter/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4cda67d

Please sign in to comment.