@@ -6,6 +6,87 @@ import classNames from 'classnames';
6
6
import { SelectionCheckboxAllProps } from './interface' ;
7
7
import BaseMixin from '../_util/BaseMixin' ;
8
8
9
+ function checkSelection ( {
10
+ store,
11
+ getCheckboxPropsByItem,
12
+ getRecordKey,
13
+ data,
14
+ type,
15
+ byDefaultChecked,
16
+ } ) {
17
+ return byDefaultChecked
18
+ ? data [ type ] ( ( item , i ) => getCheckboxPropsByItem ( item , i ) . defaultChecked )
19
+ : data [ type ] ( ( item , i ) => store . getState ( ) . selectedRowKeys . indexOf ( getRecordKey ( item , i ) ) >= 0 ) ;
20
+ }
21
+
22
+ function getIndeterminateState ( props ) {
23
+ const { store, data } = props ;
24
+ if ( ! data . length ) {
25
+ return false ;
26
+ }
27
+
28
+ const someCheckedNotByDefaultChecked =
29
+ checkSelection ( {
30
+ ...props ,
31
+ data,
32
+ type : 'some' ,
33
+ byDefaultChecked : false ,
34
+ } ) &&
35
+ ! checkSelection ( {
36
+ ...props ,
37
+ data,
38
+ type : 'every' ,
39
+ byDefaultChecked : false ,
40
+ } ) ;
41
+ const someCheckedByDefaultChecked =
42
+ checkSelection ( {
43
+ ...props ,
44
+ data,
45
+ type : 'some' ,
46
+ byDefaultChecked : true ,
47
+ } ) &&
48
+ ! checkSelection ( {
49
+ ...props ,
50
+ data,
51
+ type : 'every' ,
52
+ byDefaultChecked : true ,
53
+ } ) ;
54
+
55
+ if ( store . getState ( ) . selectionDirty ) {
56
+ return someCheckedNotByDefaultChecked ;
57
+ }
58
+ return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked ;
59
+ }
60
+
61
+ function getCheckState ( props ) {
62
+ const { store, data } = props ;
63
+ if ( ! data . length ) {
64
+ return false ;
65
+ }
66
+ if ( store . getState ( ) . selectionDirty ) {
67
+ return checkSelection ( {
68
+ ...props ,
69
+ data,
70
+ type : 'every' ,
71
+ byDefaultChecked : false ,
72
+ } ) ;
73
+ }
74
+ return (
75
+ checkSelection ( {
76
+ ...props ,
77
+ data,
78
+ type : 'every' ,
79
+ byDefaultChecked : false ,
80
+ } ) ||
81
+ checkSelection ( {
82
+ ...props ,
83
+ data,
84
+ type : 'every' ,
85
+ byDefaultChecked : true ,
86
+ } )
87
+ ) ;
88
+ }
89
+
9
90
export default {
10
91
name : 'SelectionCheckboxAll' ,
11
92
mixins : [ BaseMixin ] ,
@@ -18,25 +99,23 @@ export default {
18
99
{
19
100
key : 'all' ,
20
101
text : props . locale . selectAll ,
21
- onSelect : ( ) => { } ,
22
102
} ,
23
103
{
24
104
key : 'invert' ,
25
105
text : props . locale . selectInvert ,
26
- onSelect : ( ) => { } ,
27
106
} ,
28
107
] ;
29
108
30
109
return {
31
- checked : this . getCheckState ( props ) ,
32
- indeterminate : this . getIndeterminateState ( props ) ,
110
+ checked : getCheckState ( props ) ,
111
+ indeterminate : getIndeterminateState ( props ) ,
33
112
} ;
34
113
} ,
35
114
36
115
watch : {
37
116
$props : {
38
117
handler : function ( ) {
39
- this . setCheckState ( ) ;
118
+ this . setCheckState ( this . $props ) ;
40
119
} ,
41
120
deep : true ,
42
121
} ,
@@ -52,13 +131,6 @@ export default {
52
131
}
53
132
} ,
54
133
methods : {
55
- subscribe ( ) {
56
- const { store } = this ;
57
- this . unsubscribe = store . subscribe ( ( ) => {
58
- this . setCheckState ( this . $props ) ;
59
- } ) ;
60
- } ,
61
-
62
134
checkSelection ( props , data , type , byDefaultChecked ) {
63
135
const { store, getCheckboxPropsByItem, getRecordKey } = props || this . $props ;
64
136
// type should be 'every' | 'some'
@@ -73,8 +145,8 @@ export default {
73
145
} ,
74
146
75
147
setCheckState ( props ) {
76
- const checked = this . getCheckState ( props ) ;
77
- const indeterminate = this . getIndeterminateState ( props ) ;
148
+ const checked = getCheckState ( props ) ;
149
+ const indeterminate = getIndeterminateState ( props ) ;
78
150
this . setState ( prevState => {
79
151
const newState = { } ;
80
152
if ( indeterminate !== prevState . indeterminate ) {
@@ -87,41 +159,16 @@ export default {
87
159
} ) ;
88
160
} ,
89
161
90
- getCheckState ( props ) {
91
- const { store, data } = this ;
92
- let checked ;
93
- if ( ! data . length ) {
94
- checked = false ;
95
- } else {
96
- checked = store . getState ( ) . selectionDirty
97
- ? this . checkSelection ( props , data , 'every' , false )
98
- : this . checkSelection ( props , data , 'every' , false ) ||
99
- this . checkSelection ( props , data , 'every' , true ) ;
100
- }
101
- return checked ;
102
- } ,
103
-
104
- getIndeterminateState ( props ) {
105
- const { store, data } = this ;
106
- let indeterminate ;
107
- if ( ! data . length ) {
108
- indeterminate = false ;
109
- } else {
110
- indeterminate = store . getState ( ) . selectionDirty
111
- ? this . checkSelection ( props , data , 'some' , false ) &&
112
- ! this . checkSelection ( props , data , 'every' , false )
113
- : ( this . checkSelection ( props , data , 'some' , false ) &&
114
- ! this . checkSelection ( props , data , 'every' , false ) ) ||
115
- ( this . checkSelection ( props , data , 'some' , true ) &&
116
- ! this . checkSelection ( props , data , 'every' , true ) ) ;
117
- }
118
- return indeterminate ;
119
- } ,
120
-
121
162
handleSelectAllChange ( e ) {
122
- const checked = e . target . checked ;
163
+ const { checked } = e . target ;
123
164
this . $emit ( 'select' , checked ? 'all' : 'removeAll' , 0 , null ) ;
124
165
} ,
166
+ subscribe ( ) {
167
+ const { store } = this ;
168
+ this . unsubscribe = store . subscribe ( ( ) => {
169
+ this . setCheckState ( this . $props ) ;
170
+ } ) ;
171
+ } ,
125
172
126
173
renderMenus ( selections ) {
127
174
return selections . map ( ( selection , index ) => {
0 commit comments