@@ -22,11 +22,17 @@ export default class CheckBoxGroup extends SuperComponent {
22
22
checkboxOptions : [ ] ,
23
23
} ;
24
24
25
- properties = Props ;
25
+ properties = {
26
+ ...Props ,
27
+ defaultValue : {
28
+ type : null ,
29
+ value : undefined ,
30
+ } ,
31
+ } ;
26
32
27
33
observers = {
28
34
value : function ( ) {
29
- this . updateChildren ( 'slot' ) ;
35
+ this . updateChildren ( ) ;
30
36
} ,
31
37
} ;
32
38
@@ -36,31 +42,45 @@ export default class CheckBoxGroup extends SuperComponent {
36
42
} ,
37
43
} ;
38
44
45
+ controlledProps = [
46
+ {
47
+ key : 'value' ,
48
+ event : 'change' ,
49
+ } ,
50
+ ] ;
51
+
39
52
methods = {
40
- // slot插入选项
41
- updateChildren ( type = 'slot' ) {
42
- let items = [ ] ;
43
- if ( type === 'not-slot' ) {
53
+ getChilds ( ) {
54
+ let items = this . getRelationNodes ( '../checkbox/checkbox' ) ;
55
+ if ( ! items . length ) {
44
56
items = this . selectAllComponents ( '.t-checkbox-option' ) ;
45
- } else {
46
- items = this . getRelationNodes ( '../checkbox/checkbox' ) ;
47
57
}
58
+ return items || [ ] ;
59
+ } ,
60
+
61
+ // slot插入选项
62
+ updateChildren ( ) {
63
+ const items = this . getChilds ( ) ;
64
+
48
65
const { value, disabled } = this . data ;
49
66
if ( items . length > 0 ) {
50
67
items . forEach ( ( item : any ) => {
51
- ! item . data . checkAll && item . changeActive ( value . indexOf ( item . data . value ) > - 1 ) ;
68
+ ! item . data . checkAll &&
69
+ item . setData ( {
70
+ checked : value ?. indexOf ( item . data . value ) > - 1 ,
71
+ } ) ;
52
72
item . setDisabled ( disabled ) ;
53
73
} ) ;
54
74
// 关联可全选项
55
75
if ( items . findIndex ( ( item ) => item . data . checkAll ) > - 1 ) {
56
76
items . forEach ( ( item ) => {
57
77
item . setOptionLinked ( true ) ;
58
78
} ) ;
59
- this . handleHalfCheck ( type , items . length ) ;
79
+ this . handleHalfCheck ( items . length ) ;
60
80
}
61
81
}
62
82
} ,
63
- updateValue ( { name, checked } , type = 'slot' ) {
83
+ updateValue ( { name, checked } ) {
64
84
const { value, max } = this . data ;
65
85
let newValue = value ;
66
86
if ( max && checked && newValue . length === max ) {
@@ -72,11 +92,11 @@ export default class CheckBoxGroup extends SuperComponent {
72
92
const index = newValue . findIndex ( ( v : string ) => v === name ) ;
73
93
newValue . splice ( index , 1 ) ;
74
94
}
75
- this . setData ( {
76
- value : newValue ,
77
- } ) ;
78
- this . updateChildren ( type ) ;
79
- this . triggerEvent ( 'change' , newValue ) ;
95
+ // this.setData({
96
+ // value: newValue,
97
+ // });
98
+ // this.updateChildren();
99
+ this . _trigger ( 'change' , { value : newValue } ) ;
80
100
} ,
81
101
// 支持自定义options
82
102
handleCreateMulCheckbox ( ) {
@@ -103,57 +123,62 @@ export default class CheckBoxGroup extends SuperComponent {
103
123
this . setData ( {
104
124
checkboxOptions : optionsValue ,
105
125
} ) ;
106
- this . updateChildren ( 'not-slot' ) ;
126
+ this . updateChildren ( ) ;
107
127
} catch ( error ) {
108
128
console . error ( 'error' , error ) ;
109
129
}
110
130
} ,
111
131
// 处理全选
112
132
handleCheckAll ( e ) {
113
- const { checked, option, name, type } = e . detail || e ;
114
- let items = [ ] ;
115
- if ( type === 'not-slot' ) {
116
- items = this . selectAllComponents ( '.t-checkbox-option' ) ;
117
- } else {
118
- items = this . getRelationNodes ( '../checkbox/checkbox' ) ;
119
- }
133
+ const { checked, option, name } = e . detail || e ;
134
+ const items = this . getChilds ( ) ;
135
+
120
136
if ( ! option ) {
121
137
if ( ! items ?. length ) {
122
138
return ;
123
139
}
124
- this . setData ( {
140
+ // this.setData({
141
+ // value: items
142
+ // .map((item) => {
143
+ // if (item.data.disabled) {
144
+ // return this.data.value.includes(item.data.value) ? item.data.value : '';
145
+ // }
146
+ // item.changeActive(checked);
147
+ // return checked && !item.data.checkAll ? item.data.value : '';
148
+ // })
149
+ // .filter((val) => val),
150
+ // });
151
+ this . _trigger ( 'change' , {
125
152
value : items
126
153
. map ( ( item ) => {
127
154
if ( item . data . disabled ) {
128
- return '' ;
155
+ return this . data . value . includes ( item . data . value ) ? item . data . value : '' ;
129
156
}
130
- item . changeActive ( checked ) ;
157
+ // item.changeActive(checked);
131
158
return checked && ! item . data . checkAll ? item . data . value : '' ;
132
159
} )
133
160
. filter ( ( val ) => val ) ,
134
161
} ) ;
135
- this . triggerEvent ( 'change' , this . data . value ) ;
136
- this . handleHalfCheck ( type , items . length ) ;
162
+ // this.handleHalfCheck(items.length);
137
163
} else {
138
- this . updateValue ( { name, checked } , type ) ;
164
+ this . updateValue ( { name, checked } ) ;
139
165
}
140
166
} ,
141
167
// 处理options半选
142
- handleHalfCheck ( type : string , len : number ) {
143
- let items = [ ] ;
144
- if ( type === 'not-slot' ) {
145
- items = this . selectAllComponents ( '.t-checkbox-option' ) ;
146
- } else {
147
- items = this . getRelationNodes ( '../checkbox/checkbox' ) ;
148
- }
168
+ handleHalfCheck ( len : number ) {
169
+ const items = this . getChilds ( ) ;
149
170
const all = items . filter ( ( i ) => ! i . data . checkAll ) . map ( ( item ) => item . data . value ) ;
150
- const currentVal = Array . from ( new Set ( this . data . value . filter ( ( i ) => all . indexOf ( i ) > - 1 ) ) ) ;
171
+ const excludeDisableArr = items
172
+ . filter ( ( i ) => ! i . data . checkAll && i . data . value && ! i . data . disabled )
173
+ . map ( ( item ) => item . data . value ) ;
174
+ const currentVal = Array . from ( new Set ( this . data . value ?. filter ( ( i ) => all . indexOf ( i ) > - 1 ) ) ) ;
151
175
const element = items . find ( ( item ) => item . data . checkAll ) ;
152
176
if ( currentVal . length ) {
153
- element ?. changeActive ( true ) ;
177
+ element ?. setData ( { checked : true } ) ;
154
178
element ?. changeCheckAllHalfStatus ( currentVal . length !== len - 1 ) ;
179
+ element ?. setCancel ( currentVal . length >= excludeDisableArr . length ) ;
155
180
} else {
156
- element ?. changeActive ( false ) ;
181
+ element ?. setData ( { checked : false } ) ;
157
182
}
158
183
} ,
159
184
// 设置可全选option选项
0 commit comments