Skip to content

Bug Fix: Single Click event firing on Double Clicking #1768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { TableOnEventView } from "./tableTypes";
import { OB_ROW_ORI_INDEX, RecordType } from "comps/comps/tableComp/tableUtils";
import { ControlNodeCompBuilder } from "comps/generators/controlCompBuilder";

// double-click detection constants
const DOUBLE_CLICK_THRESHOLD = 300; // ms
let lastClickTime = 0;
let clickTimer: ReturnType<typeof setTimeout>;

const modeOptions = [
{
label: trans("selectionControl.single"),
Expand Down Expand Up @@ -38,8 +43,9 @@ export function getSelectedRowKeys(
return [selection.children.selectedRowKey.getView()];
case "multiple":
return selection.children.selectedRowKeys.getView();
default:
return [];
}
return [];
}

export const SelectionControl = (function () {
Expand All @@ -50,40 +56,52 @@ export const SelectionControl = (function () {
};
return new ControlNodeCompBuilder(childrenMap, (props, dispatch) => {
const changeSelectedRowKey = (record: RecordType) => {
if (getKey(record) !== props.selectedRowKey) {
dispatch(changeChildAction("selectedRowKey", getKey(record), false));
const key = getKey(record);
if (key !== props.selectedRowKey) {
dispatch(changeChildAction("selectedRowKey", key, false));
}
};

return (onEvent: TableOnEventView) => {
const handleClick = (record: RecordType) => {
return () => {
const now = Date.now();
clearTimeout(clickTimer);
if (now - lastClickTime < DOUBLE_CLICK_THRESHOLD) {

changeSelectedRowKey(record);
onEvent("doubleClick");
if (getKey(record) !== props.selectedRowKey) {
onEvent("rowSelectChange");
}
} else {
clickTimer = setTimeout(() => {
changeSelectedRowKey(record);
onEvent("rowClick");
if (getKey(record) !== props.selectedRowKey) {
onEvent("rowSelectChange");
}
}, DOUBLE_CLICK_THRESHOLD);
}
lastClickTime = now;
};
};

if (props.mode === "single" || props.mode === "close") {
return {
rowKey: getKey,
rowClassName: (record: RecordType, index: number, indent: number) => {
// Turn off row selection mode, only do visual shutdown, selectedRow still takes effect
if (props.mode === "close") {
return "";
}
return getKey(record) === props.selectedRowKey ? "ant-table-row-selected" : "";
},
onRow: (record: RecordType, index: number | undefined) => {
return {
onClick: () => {
changeSelectedRowKey(record);
onEvent("rowClick");
if (getKey(record) !== props.selectedRowKey) {
onEvent("rowSelectChange");
}
},
onDoubleClick: () => {
onEvent("doubleClick");
if (getKey(record) !== props.selectedRowKey) {
onEvent("rowSelectChange");
}
}
};
},
onRow: (record: RecordType, index: number | undefined) => ({
onClick: handleClick(record),
}),
};
}

const result: TableRowSelection<any> = {
type: "checkbox",
selectedRowKeys: props.selectedRowKeys,
Expand All @@ -92,7 +110,6 @@ export const SelectionControl = (function () {
dispatch(changeChildAction("selectedRowKeys", selectedRowKeys as string[], false));
onEvent("rowSelectChange");
},
// click checkbox also trigger row click event
onSelect: (record: RecordType) => {
changeSelectedRowKey(record);
onEvent("rowClick");
Expand All @@ -101,18 +118,9 @@ export const SelectionControl = (function () {
return {
rowKey: getKey,
rowSelection: result,
onRow: (record: RecordType) => {
return {
onClick: () => {
changeSelectedRowKey(record);
onEvent("rowClick");
},
onDoubleClick: () => {
changeSelectedRowKey(record);
onEvent("doubleClick");
}
};
},
onRow: (record: RecordType) => ({
onClick: handleClick(record),
}),
};
};
})
Expand All @@ -123,4 +131,4 @@ export const SelectionControl = (function () {
})
)
.build();
})();
})();
Loading