@@ -7,6 +7,11 @@ import { TableOnEventView } from "./tableTypes";
7
7
import { OB_ROW_ORI_INDEX , RecordType } from "comps/comps/tableComp/tableUtils" ;
8
8
import { ControlNodeCompBuilder } from "comps/generators/controlCompBuilder" ;
9
9
10
+ // double-click detection constants
11
+ const DOUBLE_CLICK_THRESHOLD = 300 ; // ms
12
+ let lastClickTime = 0 ;
13
+ let clickTimer : ReturnType < typeof setTimeout > ;
14
+
10
15
const modeOptions = [
11
16
{
12
17
label : trans ( "selectionControl.single" ) ,
@@ -38,8 +43,9 @@ export function getSelectedRowKeys(
38
43
return [ selection . children . selectedRowKey . getView ( ) ] ;
39
44
case "multiple" :
40
45
return selection . children . selectedRowKeys . getView ( ) ;
46
+ default :
47
+ return [ ] ;
41
48
}
42
- return [ ] ;
43
49
}
44
50
45
51
export const SelectionControl = ( function ( ) {
@@ -50,40 +56,52 @@ export const SelectionControl = (function () {
50
56
} ;
51
57
return new ControlNodeCompBuilder ( childrenMap , ( props , dispatch ) => {
52
58
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 ) ) ;
55
62
}
56
63
} ;
64
+
57
65
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
+
58
90
if ( props . mode === "single" || props . mode === "close" ) {
59
91
return {
60
92
rowKey : getKey ,
61
93
rowClassName : ( record : RecordType , index : number , indent : number ) => {
62
- // Turn off row selection mode, only do visual shutdown, selectedRow still takes effect
63
94
if ( props . mode === "close" ) {
64
95
return "" ;
65
96
}
66
97
return getKey ( record ) === props . selectedRowKey ? "ant-table-row-selected" : "" ;
67
98
} ,
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
+ } ) ,
85
102
} ;
86
103
}
104
+
87
105
const result : TableRowSelection < any > = {
88
106
type : "checkbox" ,
89
107
selectedRowKeys : props . selectedRowKeys ,
@@ -92,7 +110,6 @@ export const SelectionControl = (function () {
92
110
dispatch ( changeChildAction ( "selectedRowKeys" , selectedRowKeys as string [ ] , false ) ) ;
93
111
onEvent ( "rowSelectChange" ) ;
94
112
} ,
95
- // click checkbox also trigger row click event
96
113
onSelect : ( record : RecordType ) => {
97
114
changeSelectedRowKey ( record ) ;
98
115
onEvent ( "rowClick" ) ;
@@ -101,18 +118,9 @@ export const SelectionControl = (function () {
101
118
return {
102
119
rowKey : getKey ,
103
120
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
+ } ) ,
116
124
} ;
117
125
} ;
118
126
} )
@@ -123,4 +131,4 @@ export const SelectionControl = (function () {
123
131
} )
124
132
)
125
133
. build ( ) ;
126
- } ) ( ) ;
134
+ } ) ( ) ;
0 commit comments