You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

useDraggable.ts 829 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { useState } from 'react';
  2. /**
  3. * 处理 react-draggable 组件拖动结束时,响应了点击事件的
  4. */
  5. export const useDraggable = (onClick: () => void) => {
  6. const [isDragging, setIsDragging] = useState(false);
  7. const handleStart = () => {
  8. setIsDragging(false);
  9. };
  10. const handleDrag = () => {
  11. if (!isDragging) {
  12. setIsDragging(true);
  13. }
  14. };
  15. const handleStop = () => {
  16. // 延迟设置 isDragging 为 false 是为了确保在点击事件触发之前它仍然为 true
  17. setTimeout(() => setIsDragging(false), 0);
  18. };
  19. const handleClick = (e: React.MouseEvent<HTMLElement>) => {
  20. if (isDragging) {
  21. e.preventDefault();
  22. e.stopPropagation();
  23. } else {
  24. onClick();
  25. }
  26. };
  27. return {
  28. handleStart,
  29. handleDrag,
  30. handleStop,
  31. handleClick,
  32. };
  33. };