@@ -7,6 +7,11 @@ import { TableOnEventView } from "./tableTypes";
77import { OB_ROW_ORI_INDEX , RecordType } from "comps/comps/tableComp/tableUtils" ;
88import { 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+
1015const 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
4551export 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