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(drawer): 解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题 #3420

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

wonkzhang
Copy link
Contributor

解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 Issue

💡 需求背景和解决方案

useDrag.ts 文件中, handleMouseup 函数中的 dragSizeValue 每次取值不正确的问题,是由于 dragSizeValue 的状态更新不及时导致的。 useState 的更新是异步的,因此在 handleMouseup 中直接使用 dragSizeValue 会导致获取到的值不是最新的。

解决方案是使用 useRef 来存储最新的 dragSizeValue ,这样可以确保在 handleMouseup 中获取到的值是最新的。

📝 更新日志

  • fix(Drawer): 解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题

  • 本条 PR 不需要纳入 Changelog

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题
@@ -9,36 +9,43 @@ const useDrag = (
onSizeDragEnd: TdDrawerProps['onSizeDragEnd'],
) => {
const [dragSizeValue, changeDragSizeValue] = useState<string>(null);
// 使用 ref 来存储当前拖拽的宽度值
const dragSizeNumberRef = useRef<number>(0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要改动点


if (typeof moveSize === 'undefined') return;
changeDragSizeValue(`${moveSize}px`);
dragSizeNumberRef.current = moveSize;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要改动点

onSizeDragEnd?.({
e,
// 此处不要使用 dragSizeValue,useState 的更新是异步的,在鼠标拖拽的同步操作中取不到最新的值
size: dragSizeNumberRef.current,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要改动点

Copy link
Contributor

github-actions bot commented Mar 7, 2025

完成

@HaixingOoO HaixingOoO requested a review from Copilot March 8, 2025 05:01

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR fixes an issue where the Drawer component was not obtaining the correct width during drag-resize operations by switching from state to a ref for tracking the latest drag size.

  • Changed handleMousemove and handleMouseup to use useCallback for improved performance and stability.
  • Introduced a ref (dragSizeNumberRef) to store the current drag size, ensuring the handleMouseup callback accesses the latest value.

Reviewed Changes

File Description
packages/components/drawer/hooks/useDrag.ts Updated drag handling logic to use useCallback and a ref for the drag size to fix stale state issues

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

packages/components/drawer/hooks/useDrag.ts:13

  • [nitpick] The variable name 'dragSizeNumberRef' is a bit verbose. Consider renaming it to 'dragSizeRef' for improved readability.
const dragSizeNumberRef = useRef<number>(0);
@HaixingOoO
Copy link
Collaborator

HaixingOoO commented Mar 8, 2025

const dragSizeRef = useRef<number>(0); 

感觉变量名可以这个

… 后续不必要的计算

调整了变量dragSizeRef的命名;将allowSizeDraggable的判断逻辑提升到了函数的顶部,避免 后续不必要的计算
@wonkzhang wonkzhang closed this Mar 10, 2025
@wonkzhang
Copy link
Contributor Author

const dragSizeRef = useRef<number>(0); 

感觉变量名可以这个

已经调整了变量名

@wonkzhang wonkzhang reopened this Mar 10, 2025
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.

3 participants