Skip to content

Commit

Permalink
fix: Move duble click to syntheic events
Browse files Browse the repository at this point in the history
  • Loading branch information
Domino987 committed Jul 7, 2021
1 parent 101d290 commit fcf963d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
17 changes: 8 additions & 9 deletions src/components/m-table-body-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function MTableBodyRow(props) {
}
onToggleDetailPanel(path, panel);
});
const [doubleClickRef] = useDoubleClick(
const onRowClickListener = useDoubleClick(
onRowClick ? (e) => onClick(e, onRowClick) : undefined,
onDoubleRowClick ? (e) => onClick(e, onDoubleRowClick) : undefined,
onClick
Expand Down Expand Up @@ -155,10 +155,8 @@ export default function MTableBodyRow(props) {
}

const size = CommonValues.elementSize(props);
const selectionWidth = CommonValues.selectionMaxWidth(
props,
props.treeDataMaxLevel
) || 0;
const selectionWidth =
CommonValues.selectionMaxWidth(props, props.treeDataMaxLevel) || 0;

const styles =
size === 'medium'
Expand All @@ -182,9 +180,10 @@ export default function MTableBodyRow(props) {
checked={props.data.tableData.checked === true}
onClick={(e) => e.stopPropagation()}
value={props.data.tableData.id.toString()}
onChange={(event) =>
props.onRowSelected(event, props.path, props.data)
}
onChange={(event) => {
console.log('call');
props.onRowSelected(event, props.path, props.data);
}}
style={styles}
{...checkboxProps}
/>
Expand Down Expand Up @@ -445,7 +444,7 @@ export default function MTableBodyRow(props) {
<TableRow
selected={hasAnyEditingRow}
{...rowProps}
ref={doubleClickRef}
onClick={onRowClickListener}
hover={!!onRowClick || !!onDoubleRowClick}
style={getStyle(props.index, props.level)}
>
Expand Down
57 changes: 20 additions & 37 deletions src/utils/hooks/useDoubleClick.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import React from 'react';

function useDoubleClick(singleCallback, dbCallback) {
/** callback ref Pattern **/
const [elem, setElem] = React.useState(null);
const callbackRef = React.useCallback((node) => {
setElem(node);
callbackRef.current = node;
}, []);

const countRef = React.useRef(0);
/** Refs for the timer **/
const timerRef = React.useRef(null);
Expand All @@ -18,41 +11,31 @@ function useDoubleClick(singleCallback, dbCallback) {
inputDoubleCallbackRef.current = dbCallback;
inputSingleCallbackRef.current = singleCallback;
});
React.useEffect(() => {
function handler(e) {
const isDoubleClick = countRef.current + 1 === 2;
const timerIsPresent = timerRef.current;
if (timerIsPresent && isDoubleClick) {
const onClick = React.useCallback((e) => {
const isDoubleClick = countRef.current + 1 === 2;
const timerIsPresent = timerRef.current;
if (timerIsPresent && isDoubleClick) {
clearTimeout(timerRef.current);
timerRef.current = null;
countRef.current = 0;
if (inputDoubleCallbackRef.current) {
inputDoubleCallbackRef.current(e);
}
}
if (!timerIsPresent) {
countRef.current = countRef.current + 1;
const timer = setTimeout(() => {
clearTimeout(timerRef.current);
timerRef.current = null;
countRef.current = 0;
if (inputDoubleCallbackRef.current) {
inputDoubleCallbackRef.current(e);
if (inputSingleCallbackRef.current) {
inputSingleCallbackRef.current(e);
}
}
if (!timerIsPresent) {
countRef.current = countRef.current + 1;
const timer = setTimeout(() => {
clearTimeout(timerRef.current);
timerRef.current = null;
countRef.current = 0;
if (inputSingleCallbackRef.current) {
inputSingleCallbackRef.current(e);
}
}, 200);
timerRef.current = timer;
}
}
if (elem) {
elem.addEventListener('click', handler);
}, 200);
timerRef.current = timer;
}
return () => {
if (elem) {
elem.removeEventListener('click', handler);
}
};
}, [elem]);
return [callbackRef, elem];
}, []);
return onClick;
}

export { useDoubleClick };

0 comments on commit fcf963d

Please sign in to comment.