Skip to content

Commit 7ea2df4

Browse files
committed
fix(Cell/Row): stopPropagation should work for cellClick and not reach rowClick #50
1 parent 8dece67 commit 7ea2df4

File tree

5 files changed

+48
-51
lines changed

5 files changed

+48
-51
lines changed

package-lock.json

Lines changed: 3 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"dependencies": {
1414
"clsx": "1.1.1",
1515
"react-virtualized-auto-sizer": "1.0.6",
16-
"react-window": "1.8.6",
17-
"use-double-click": "1.0.5"
16+
"react-window": "1.8.6"
1817
},
1918
"devDependencies": {
2019
"@babel/core": "7.12.17",
@@ -54,7 +53,6 @@
5453
"@types/react-dom": "17.0.11",
5554
"@types/react-virtualized-auto-sizer": "1.0.1",
5655
"@types/react-window": "1.8.5",
57-
"@types/use-double-click": "1.0.1",
5856
"@typescript-eslint/eslint-plugin": "5.10.2",
5957
"@typescript-eslint/parser": "5.10.2",
6058
"axios": "0.21.1",

src/table/Row/Row.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useConsumeRowLayout } from '@table-library/react-table-library/resize/u
1515
import { Nullish } from '@table-library/react-table-library/types/common';
1616
import {
1717
OnClick,
18+
Event,
1819
Features,
1920
RowProps,
2021
FeatureProps,
@@ -103,6 +104,7 @@ export const Row: React.FC<RowProps> = (props: RowProps) => {
103104
<RowContainer
104105
role="row"
105106
data-table-library_tr-body=""
107+
onClick={onDoubleClick ? () => {} : (event: Event) => onClickByFeature(item, event)}
106108
css={css`
107109
${themeByFeature}
108110
${theme?.BaseRow}

src/table/Row/useDoubleClick.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
import * as React from 'react';
2-
import useDoubleClickBase from 'use-double-click';
32

4-
import { TableNode, OnClick } from '@table-library/react-table-library/types/table';
3+
import { TableNode, OnClick, Event } from '@table-library/react-table-library/types/table';
54
import { Nullish } from '@table-library/react-table-library/types/common';
65

76
const NOOP = () => {};
87

8+
const useDoubleClickBase = ({
9+
onSingleClick,
10+
onDoubleClick,
11+
ref,
12+
}: {
13+
ref: React.MutableRefObject<HTMLDivElement | null>;
14+
onSingleClick: (e: Event) => void;
15+
onDoubleClick: ((e: Event) => void) | null;
16+
}) => {
17+
React.useEffect(() => {
18+
const { current } = ref;
19+
20+
let clickCount = 0;
21+
22+
const handleDoubleClick = (event: any) => {
23+
if (onDoubleClick) {
24+
clickCount += 1;
25+
26+
setTimeout(() => {
27+
if (clickCount === 1) onSingleClick(event);
28+
else if (clickCount === 2) onDoubleClick(event);
29+
30+
clickCount = 0;
31+
}, 300);
32+
}
33+
};
34+
35+
current?.addEventListener('click', handleDoubleClick);
36+
37+
return () => {
38+
current?.removeEventListener('click', handleDoubleClick);
39+
};
40+
});
41+
};
42+
943
export const useDoubleClick = (
1044
ref: React.MutableRefObject<HTMLDivElement | null>,
1145
onClick: OnClick | Nullish,
1246
onDoubleClick: OnClick | Nullish,
1347
node: TableNode,
14-
) =>
48+
) => {
1549
useDoubleClickBase({
1650
onSingleClick: onClick ? (event) => onClick(node, event) : NOOP,
17-
onDoubleClick: onDoubleClick ? (event) => onDoubleClick(node, event) : NOOP,
18-
latency: onDoubleClick ? 250 : 0,
51+
onDoubleClick: onDoubleClick ? (event) => onDoubleClick(node, event) : null,
1952
ref,
2053
});
54+
};

src/types/table.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { Pagination } from '@table-library/react-table-library/types/pagination'
1313

1414
export type RestProps = Record<string, any>;
1515

16-
export type OnClick = (node: TableNode, event: React.SyntheticEvent | React.KeyboardEvent) => void;
16+
export type Event = React.SyntheticEvent | React.KeyboardEvent;
17+
18+
export type OnClick = (node: TableNode, event: Event) => void;
1719

1820
export type CellProps = {
1921
className?: string;

0 commit comments

Comments
 (0)