-
Notifications
You must be signed in to change notification settings - Fork 334
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
base: develop
Are you sure you want to change the base?
Conversation
解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题
@@ -9,36 +9,43 @@ const useDrag = ( | |||
onSizeDragEnd: TdDrawerProps['onSizeDragEnd'], | |||
) => { | |||
const [dragSizeValue, changeDragSizeValue] = useState<string>(null); | |||
// 使用 ref 来存储当前拖拽的宽度值 | |||
const dragSizeNumberRef = useRef<number>(0); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要改动点
There was a problem hiding this comment.
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);
const dragSizeRef = useRef<number>(0); 感觉变量名可以这个 |
|
||
if (!allowSizeDraggable) return; | ||
if (!allowSizeDraggable) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个可以顺便移动更上面判断 如果没有就不需要继续计算了
解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题
🤔 这个 PR 的性质是?
🔗 相关 Issue
💡 需求背景和解决方案
在
useDrag.ts
文件中, handleMouseup 函数中的 dragSizeValue 每次取值不正确的问题,是由于 dragSizeValue 的状态更新不及时导致的。 useState 的更新是异步的,因此在 handleMouseup 中直接使用 dragSizeValue 会导致获取到的值不是最新的。解决方案是使用 useRef 来存储最新的 dragSizeValue ,这样可以确保在 handleMouseup 中获取到的值是最新的。
📝 更新日志
fix(Drawer): 解决Drawer抽屉在拖拽改变大小的时候获取宽度可能不正确的问题
本条 PR 不需要纳入 Changelog
☑️ 请求合并前的自查清单