Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(check-cascader): 优化大数据下勾选根节点时卡顿 (#2998) #3001

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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