Files
SupplierSystem/src/hooks/useRowClickOrDoubleClick.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

98 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useRef, type MouseEvent } from 'react';
/** 双击窗口:两次单击间隔小于该值即判为双击(与多数桌面端习惯一致) */
export const ROW_DOUBLE_CLICK_MS = 260;
type Options = {
/** 单击(延迟到双击窗口结束、确认没有第二次点击后才执行) */
onSingleClick: (event: MouseEvent<HTMLElement>) => void;
/** 双击(执行时单击已被取消,不会同时触发单击动作) */
onDoubleClick: (event: MouseEvent<HTMLElement>) => void;
/** 判定为拖拽/长按的位移阈值(px):超过则不视为点击 */
cancelMovePx?: number;
windowMs?: number;
};
/**
* 行内「单击 = 打开详情,双击 = 行内编辑」共存。
*
* 背景(2026-10-08 实测缺陷):行展开绑在 `<tr onClick>`,而双击的**第一次单击**会先冒泡到行,
* 于是「双击配送时间」会顺带把订单详情展开;只在子元素上 `stopPropagation` 拦 `dblclick` 拦不住
* 那两次 `click`。本 hook 把单击**推迟**到双击窗口之后执行:期间若来了第二次点击,就取消单击、
* 只跑双击。这样两条动作互斥且与浏览器事件顺序无关。
*/
export function useRowClickOrDoubleClick({
onSingleClick,
onDoubleClick,
cancelMovePx = 6,
windowMs = ROW_DOUBLE_CLICK_MS,
}: Options) {
const handlersRef = useRef({ onSingleClick, onDoubleClick });
handlersRef.current = { onSingleClick, onDoubleClick };
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const downPointRef = useRef<{ x: number; y: number } | null>(null);
const movedRef = useRef(false);
const clearPending = useCallback(() => {
if (timerRef.current != null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
}, []);
// 卸载时清掉挂起的单击,避免对已卸载页面调用 openOrderDetail
useEffect(() => clearPending, [clearPending]);
const onPointerDown = useCallback((event: MouseEvent<HTMLElement>) => {
downPointRef.current = { x: event.clientX, y: event.clientY };
movedRef.current = false;
}, []);
const onPointerMove = useCallback(
(event: MouseEvent<HTMLElement>) => {
const start = downPointRef.current;
if (!start) return;
if (Math.abs(event.clientX - start.x) > cancelMovePx || Math.abs(event.clientY - start.y) > cancelMovePx) {
movedRef.current = true;
}
},
[cancelMovePx]
);
const onClick = useCallback(
(event: MouseEvent<HTMLElement>) => {
// 交互元素(复选框/按钮/输入框/链接/下拉)与拖拽手势不参与本逻辑
if ((event.target as HTMLElement).closest('button, input, a, select, textarea')) return;
if (movedRef.current) return;
if (timerRef.current != null) {
// 第二次点击 → 判定双击:取消挂起的单击,只跑双击
clearPending();
handlersRef.current.onDoubleClick(event);
return;
}
// ⚠️ React 在事件派发结束后会把 currentTarget 置为 null。单击是**延迟**执行的,
// 这里必须同步把元素抓出来、到点再补回事件对象上;否则消费方读
// `event.currentTarget.dataset` 会抛 "Cannot read properties of null (reading 'dataset')"。
// (2026-10-08 实测缺陷:订单列表单击行报错、详情打不开;双击那条路是同步调用,不受影响)
const currentTarget = event.currentTarget;
timerRef.current = setTimeout(() => {
timerRef.current = null;
if (currentTarget && !event.currentTarget) {
try {
Object.defineProperty(event, 'currentTarget', {
value: currentTarget,
configurable: true,
});
} catch {
/* 只读事件对象:保持原样,消费方需自行兜底 */
}
}
handlersRef.current.onSingleClick(event);
}, windowMs);
},
[clearPending, windowMs]
);
return { onClick, onPointerDown, onPointerMove };
}