Skip to content

Commit

Permalink
perf(check-cascader): 优化大数据下勾选根节点时卡顿 (#2998) (#3001)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare authored Sep 14, 2024
1 parent 908d6cd commit 872c903
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-rings-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/check-cascader": patch
---

perf: 优化大数据下勾选根节点时卡顿
5 changes: 5 additions & 0 deletions .changeset/tame-rats-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

perf(check-cascader): 优化大数据下勾选根节点时卡顿
18 changes: 9 additions & 9 deletions packages/ui/check-cascader/src/CheckCascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp

const [cascaderData, setCascaderData] = useCache(data)

const flattedData = useMemo(() => flattenTreeData(cascaderData, fieldNames), [
cascaderData,
fieldNames,
])
const [flattedData, flattedDataMap] = useMemo(() => {
const flattedData = flattenTreeData(cascaderData, fieldNames)
const flattedDataMap = new Map(flattedData.map((node: any, index) => [node.id, node]))

return [flattedData, flattedDataMap]
}, [cascaderData, fieldNames])

const [_value, tryChangeValue] = useUncontrolledState(defaultValue, valueProp, onChange)
// 内部实现使用尾部 id
const value = _value.map((path) => path[path.length - 1])

const proxyOnChange = useLatestCallback(
(value: React.ReactText[], item: any, shouldChecked: boolean) => {
const flattedItems = flattedData

const itemsPaths = value.map((lastId) => {
const item = flattedItems.find((item) => item.id === lastId)
const item = flattedDataMap.get(lastId)
if (item) {
return getTopDownAncestors(item).map(({ id }) => id)
}
Expand Down Expand Up @@ -223,9 +223,9 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp

const selectedItems = useMemo(() => {
return value.map((selectedId) => {
return flattedData.find((item) => item.id === selectedId)
return flattedDataMap.get(selectedId)
})
}, [value, flattedData])
}, [flattedDataMap, value])

return (
<Picker
Expand Down

0 comments on commit 872c903

Please sign in to comment.