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

fix: Fixed single-select Cascader, under React18, the expansion behav… #2435

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

YyumeiZhang
Copy link
Collaborator

@YyumeiZhang YyumeiZhang commented Aug 21, 2024

…ior is abnormal when loading data asynchronously

中文模板 / Chinese Template

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Test Case
  • TypeScript definition update
  • Document improve
  • CI/CD improve
  • Branch sync
  • Other, please describe:

PR description

Fixes #
单选的 Cascader 在 React 18 中会出现此问题。

Cascader 的展开项和 state 中 activeKeys 相关,点击会触发 activeKeys 变化。

在点击项目时,会调用 foundation 中的 handleItemClick -> handleSingleSelect->正确更新 activeKeys,由于需要异步加载数据,因此调用 notifyIfLoadData ->用户设置的 loadData 。

loadData 返回一个 promise,执行会触发treeData 更新, 并 resolve。

  1. treeData 更新会触发 componentDidUpdate->foundation 中的 collectOptions -> updateSelectedKey。
  2. resolve 后的 then 函数会更新 loading 状态和 loadedKeys(在 callback 函数的调用中)。

在 react 18 之前,treeData 的更新比 then 函数中的 loading 和 loadedKeys 更新快,因此updateSelectedKey函数中,可以借助没更新的 loading 和 loadedKeys 决定不改变 activeKeys。
在 react 18 之后,猜测对 promise 和 then 中的状态更新逻辑发生变化,导致updateSelectedKey执行时,loading 和 loadedKeys 已经变化,因此执行结果不符合预期。activeKeys 被更改。

修改方式为 在 loadData 的 then 函数中,借助 setTimeout 保证执行顺序。保证updateSelectedKey执行时,loading 和 loadedKeys 未变化。

Changelog

🇨🇳 Chinese


🇺🇸 English

Checklist

  • Test or no need
  • Document or no need
  • Changelog or no need

Other

  • Skip Changelog

Additional information

…ior is abnormal when loading data asynchronously
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6e312a1:

Sandbox Source
pr-story Configuration

Copy link

cypress bot commented Aug 21, 2024



Test summary

268 0 11 0


Run details

Project semi-design
Status Passed
Commit 643519c ℹ️
Started Aug 21, 2024 12:31 PM
Ended Aug 21, 2024 12:41 PM
Duration 10:28 💡
OS Linux Debian -
Browser Chrome 103

View run in Cypress Cloud ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Cloud

@YyumeiZhang YyumeiZhang requested a review from pointhalo August 21, 2024 12:44
@YyumeiZhang YyumeiZhang merged commit 4a1dd75 into main Sep 3, 2024
9 checks passed
@YyumeiZhang YyumeiZhang deleted the fix/cascader-react18 branch September 3, 2024 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant