@@ -6,6 +6,87 @@ import classNames from 'classnames';
66import { SelectionCheckboxAllProps } from './interface' ;
77import BaseMixin from '../_util/BaseMixin' ;
88
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+
990export default {
1091 name : 'SelectionCheckboxAll' ,
1192 mixins : [ BaseMixin ] ,
@@ -18,25 +99,23 @@ export default {
1899 {
19100 key : 'all' ,
20101 text : props . locale . selectAll ,
21- onSelect : ( ) => { } ,
22102 } ,
23103 {
24104 key : 'invert' ,
25105 text : props . locale . selectInvert ,
26- onSelect : ( ) => { } ,
27106 } ,
28107 ] ;
29108
30109 return {
31- checked : this . getCheckState ( props ) ,
32- indeterminate : this . getIndeterminateState ( props ) ,
110+ checked : getCheckState ( props ) ,
111+ indeterminate : getIndeterminateState ( props ) ,
33112 } ;
34113 } ,
35114
36115 watch : {
37116 $props : {
38117 handler : function ( ) {
39- this . setCheckState ( ) ;
118+ this . setCheckState ( this . $props ) ;
40119 } ,
41120 deep : true ,
42121 } ,
@@ -52,13 +131,6 @@ export default {
52131 }
53132 } ,
54133 methods : {
55- subscribe ( ) {
56- const { store } = this ;
57- this . unsubscribe = store . subscribe ( ( ) => {
58- this . setCheckState ( this . $props ) ;
59- } ) ;
60- } ,
61-
62134 checkSelection ( props , data , type , byDefaultChecked ) {
63135 const { store, getCheckboxPropsByItem, getRecordKey } = props || this . $props ;
64136 // type should be 'every' | 'some'
@@ -73,8 +145,8 @@ export default {
73145 } ,
74146
75147 setCheckState ( props ) {
76- const checked = this . getCheckState ( props ) ;
77- const indeterminate = this . getIndeterminateState ( props ) ;
148+ const checked = getCheckState ( props ) ;
149+ const indeterminate = getIndeterminateState ( props ) ;
78150 this . setState ( prevState => {
79151 const newState = { } ;
80152 if ( indeterminate !== prevState . indeterminate ) {
@@ -87,41 +159,16 @@ export default {
87159 } ) ;
88160 } ,
89161
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-
121162 handleSelectAllChange ( e ) {
122- const checked = e . target . checked ;
163+ const { checked } = e . target ;
123164 this . $emit ( 'select' , checked ? 'all' : 'removeAll' , 0 , null ) ;
124165 } ,
166+ subscribe ( ) {
167+ const { store } = this ;
168+ this . unsubscribe = store . subscribe ( ( ) => {
169+ this . setCheckState ( this . $props ) ;
170+ } ) ;
171+ } ,
125172
126173 renderMenus ( selections ) {
127174 return selections . map ( ( selection , index ) => {
0 commit comments