Skip to content

Commit 8778c63

Browse files
Merge pull request #1768 from kamalqureshi/table_doubleClick_eventHandler
Bug Fix: Single Click event firing on Double Clicking
2 parents 18b9078 + 0431090 commit 8778c63

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

client/packages/lowcoder/src/comps/comps/tableComp/selectionControl.tsx

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { TableOnEventView } from "./tableTypes";
77
import { OB_ROW_ORI_INDEX, RecordType } from "comps/comps/tableComp/tableUtils";
88
import { ControlNodeCompBuilder } from "comps/generators/controlCompBuilder";
99

10+
// double-click detection constants
11+
const DOUBLE_CLICK_THRESHOLD = 300; // ms
12+
let lastClickTime = 0;
13+
let clickTimer: ReturnType<typeof setTimeout>;
14+
1015
const modeOptions = [
1116
{
1217
label: trans("selectionControl.single"),
@@ -38,8 +43,9 @@ export function getSelectedRowKeys(
3843
return [selection.children.selectedRowKey.getView()];
3944
case "multiple":
4045
return selection.children.selectedRowKeys.getView();
46+
default:
47+
return [];
4148
}
42-
return [];
4349
}
4450

4551
export const SelectionControl = (function () {
@@ -50,40 +56,52 @@ export const SelectionControl = (function () {
5056
};
5157
return new ControlNodeCompBuilder(childrenMap, (props, dispatch) => {
5258
const changeSelectedRowKey = (record: RecordType) => {
53-
if (getKey(record) !== props.selectedRowKey) {
54-
dispatch(changeChildAction("selectedRowKey", getKey(record), false));
59+
const key = getKey(record);
60+
if (key !== props.selectedRowKey) {
61+
dispatch(changeChildAction("selectedRowKey", key, false));
5562
}
5663
};
64+
5765
return (onEvent: TableOnEventView) => {
66+
const handleClick = (record: RecordType) => {
67+
return () => {
68+
const now = Date.now();
69+
clearTimeout(clickTimer);
70+
if (now - lastClickTime < DOUBLE_CLICK_THRESHOLD) {
71+
72+
changeSelectedRowKey(record);
73+
onEvent("doubleClick");
74+
if (getKey(record) !== props.selectedRowKey) {
75+
onEvent("rowSelectChange");
76+
}
77+
} else {
78+
clickTimer = setTimeout(() => {
79+
changeSelectedRowKey(record);
80+
onEvent("rowClick");
81+
if (getKey(record) !== props.selectedRowKey) {
82+
onEvent("rowSelectChange");
83+
}
84+
}, DOUBLE_CLICK_THRESHOLD);
85+
}
86+
lastClickTime = now;
87+
};
88+
};
89+
5890
if (props.mode === "single" || props.mode === "close") {
5991
return {
6092
rowKey: getKey,
6193
rowClassName: (record: RecordType, index: number, indent: number) => {
62-
// Turn off row selection mode, only do visual shutdown, selectedRow still takes effect
6394
if (props.mode === "close") {
6495
return "";
6596
}
6697
return getKey(record) === props.selectedRowKey ? "ant-table-row-selected" : "";
6798
},
68-
onRow: (record: RecordType, index: number | undefined) => {
69-
return {
70-
onClick: () => {
71-
changeSelectedRowKey(record);
72-
onEvent("rowClick");
73-
if (getKey(record) !== props.selectedRowKey) {
74-
onEvent("rowSelectChange");
75-
}
76-
},
77-
onDoubleClick: () => {
78-
onEvent("doubleClick");
79-
if (getKey(record) !== props.selectedRowKey) {
80-
onEvent("rowSelectChange");
81-
}
82-
}
83-
};
84-
},
99+
onRow: (record: RecordType, index: number | undefined) => ({
100+
onClick: handleClick(record),
101+
}),
85102
};
86103
}
104+
87105
const result: TableRowSelection<any> = {
88106
type: "checkbox",
89107
selectedRowKeys: props.selectedRowKeys,
@@ -92,7 +110,6 @@ export const SelectionControl = (function () {
92110
dispatch(changeChildAction("selectedRowKeys", selectedRowKeys as string[], false));
93111
onEvent("rowSelectChange");
94112
},
95-
// click checkbox also trigger row click event
96113
onSelect: (record: RecordType) => {
97114
changeSelectedRowKey(record);
98115
onEvent("rowClick");
@@ -101,18 +118,9 @@ export const SelectionControl = (function () {
101118
return {
102119
rowKey: getKey,
103120
rowSelection: result,
104-
onRow: (record: RecordType) => {
105-
return {
106-
onClick: () => {
107-
changeSelectedRowKey(record);
108-
onEvent("rowClick");
109-
},
110-
onDoubleClick: () => {
111-
changeSelectedRowKey(record);
112-
onEvent("doubleClick");
113-
}
114-
};
115-
},
121+
onRow: (record: RecordType) => ({
122+
onClick: handleClick(record),
123+
}),
116124
};
117125
};
118126
})
@@ -123,4 +131,4 @@ export const SelectionControl = (function () {
123131
})
124132
)
125133
.build();
126-
})();
134+
})();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy