@@ -51,7 +51,7 @@ const Checkbox = React.createClass({
51
51
*/
52
52
assistiveText : React . PropTypes . string ,
53
53
/**
54
- * The Checkbox is a controlled component, and will always be in this state.
54
+ * The Checkbox is a controlled component, and will always be in this state. If checked is not defined, the state of the uncontrolled native `input` component will be used.
55
55
*/
56
56
checked : React . PropTypes . bool ,
57
57
/**
@@ -75,7 +75,7 @@ const Checkbox = React.createClass({
75
75
*/
76
76
id : PropTypes . string ,
77
77
/**
78
- * The Checkbox will be indeterminate if its state can not be figured out.
78
+ * The Checkbox will be indeterminate if its state can not be figured out or is partially checked. Once a checkbox is indeterminate, a click should cause it to be checked. Since a user cannot put a checkbox into an indeterminate state, it is assumed you are controlling the value of `checked` with the parent, also, and that this is a controlled component .
79
79
*/
80
80
indeterminate : React . PropTypes . bool ,
81
81
/**
@@ -98,92 +98,14 @@ const Checkbox = React.createClass({
98
98
99
99
getDefaultProps ( ) {
100
100
return {
101
- id : shortid . generate ( ) ,
102
- checked : null ,
103
101
indeterminate : false
104
102
} ;
105
103
} ,
106
104
107
- getInitialState ( ) {
108
- return {
109
- checked : this . props . indeterminate === true ? false : this . props . checked ,
110
- defaultChecked : this . props . checked ,
111
- indeterminate : this . props . indeterminate === true ? true : null
112
- } ;
113
- } ,
114
-
115
- componentDidMount ( ) {
116
- const checkbox = this . input ;
117
- checkbox . checked = this . state . indeterminate === true ? false : this . state . defaultChecked ;
118
- checkbox . indeterminate = this . state . indeterminate ;
119
- } ,
120
-
121
-
122
- componentWillReceiveProps ( nextProps ) {
123
- const checkbox = this . input ;
124
- checkbox . checked = nextProps . indeterminate === true ? false : nextProps . checked ;
125
- checkbox . indeterminate = nextProps . indeterminate ;
126
- this . setState ( {
127
- checked : nextProps . indeterminate === true ? false : nextProps . checked ,
128
- indeterminate : nextProps . indeterminate
129
- } ) ;
130
- } ,
131
-
132
- shouldComponentUpdate ( nextProps , nextState ) {
133
- let toReturn = true ;
134
- let toReturnIndeterminate = true ;
135
- let toReturnChecked = true ;
136
-
137
- if (
138
- nextProps . indeterminate === this . props . indeterminate
139
- && nextProps . indeterminate === nextState . indeterminate
140
- && nextProps . indeterminate === this . state . indeterminate
141
- ) {
142
- toReturnIndeterminate = false ;
143
- }
144
-
145
- // Sometimes the indeterminate stuff is not a boolean, so double-check ehre
146
- if (
147
- ( typeof nextProps . indeterminate === 'object' && nextProps . indeterminate === null )
148
- && ( typeof this . props . indeterminate === 'object' && this . props . indeterminate === null )
149
- && ( this . state . indeterminate === false )
150
- && ( nextState . indeterminate === false )
151
- ) {
152
- toReturnIndeterminate = false ;
153
- }
154
-
155
- if (
156
- nextProps . checked === this . props . checked
157
- && nextProps . checked === nextState . checked
158
- && nextProps . checked === this . state . checked
159
- ) {
160
- toReturnChecked = false ;
161
- }
162
-
163
- toReturn = toReturnIndeterminate === true || toReturnChecked === true ;
164
- return toReturn ;
165
- } ,
166
-
167
- componentWillUpdate ( nextProps , nextState ) {
168
- if ( nextProps . indeterminate === true ) {
169
- nextState . checked = null ;
170
- }
171
- if ( nextProps . checked !== nextState . checked ) {
172
-
173
- }
105
+ componentWillMount ( ) {
106
+ this . generatedId = shortid . generate ( ) ;
174
107
} ,
175
108
176
- componentDidUpdate ( ) {
177
- const checkbox = this . input ;
178
- checkbox . checked = this . state . indeterminate === true ? null : this . state . checked === true ? true : null ;
179
- checkbox . indeterminate = this . state . indeterminate ;
180
- if ( this . state . indeterminate === false && this . state . checked === false ) {
181
- checkbox . checked = false ;
182
- }
183
- } ,
184
-
185
-
186
-
187
109
// ### Render
188
110
render ( ) {
189
111
const {
@@ -218,16 +140,21 @@ const Checkbox = React.createClass({
218
140
{ required ? < abbr className = "slds-required" title = "required" > *</ abbr > : null }
219
141
< input
220
142
{ ...props }
221
- id = { id }
222
- checked = { indeterminate === true ? null : checked }
223
- indeterminate = { indeterminate }
143
+ id = { id || this . generatedId }
144
+ checked = { checked }
224
145
name = { name }
225
146
disabled = { disabled }
226
147
onChange = { this . handleChange }
227
148
type = "checkbox"
228
- ref = { ( component ) => this . input = component }
149
+ ref = {
150
+ ( component ) => {
151
+ if ( component ) {
152
+ component . indeterminate = indeterminate ;
153
+ }
154
+ this . input = component ;
155
+ } }
229
156
/>
230
- < label className = "slds-checkbox__label" htmlFor = { id } >
157
+ < label className = "slds-checkbox__label" htmlFor = { id || this . generatedId } >
231
158
< span className = "slds-checkbox--faux" > </ span >
232
159
{ label
233
160
? < span className = "slds-form-element__label" >
@@ -247,47 +174,19 @@ const Checkbox = React.createClass({
247
174
) ;
248
175
} ,
249
176
250
- getValue ( ) {
251
- const checkbox = this . input ;
252
- return ! checkbox . checked ;
253
- } ,
254
-
255
- isChecked ( ) {
256
- return this . getValue ( ) || false ;
257
- } ,
258
-
259
177
handleChange ( event ) {
260
- let value = event . target . checked ;
261
- const props = this . props ;
262
- const oldValue = this . getValue ( ) ;
263
-
264
- if ( props . indeterminate === true ) {
265
- if ( typeof props . nextValue === 'function' ) {
266
- value = props . nextValue ( oldValue , this . props ) ;
267
- }
268
- }
178
+ const value = event . target . checked ;
179
+ const {
180
+ checked,
181
+ indeterminate,
182
+ onChange
183
+ } = this . props ;
269
184
270
- if ( oldValue !== ! this . state . defaultValue ) {
271
- if ( isFunction ( props . onChange ) ) {
272
- this . props . onChange ( value , event , {
273
- checked : value ,
274
- indeterminate : null
275
- } ) ;
276
- }
277
- this . setState ( {
278
- checked : value ,
279
- indeterminate : null
280
- } ) ;
281
- } else {
282
- if ( isFunction ( props . onChange ) ) {
283
- this . props . onChange ( value , event , {
284
- checked : props . indeterminate === true ? null : value ,
285
- indeterminate : props . indeterminate
286
- } ) ;
287
- }
288
- this . setState ( {
289
- checked : props . indeterminate === true ? null : value ,
290
- indeterminate : props . indeterminate
185
+ if ( isFunction ( onChange ) ) {
186
+ // `checked` is present twice to maintain backwards compatibility. Please remove first parameter `value` on the next breaking change.
187
+ onChange ( value , event , {
188
+ checked : indeterminate ? true : ! checked ,
189
+ indeterminate : false
291
190
} ) ;
292
191
}
293
192
} ,
0 commit comments