@@ -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
/**
@@ -96,94 +96,10 @@ const Checkbox = React.createClass({
96
96
required : PropTypes . bool
97
97
} ,
98
98
99
- getDefaultProps ( ) {
100
- return {
101
- id : shortid . generate ( ) ,
102
- checked : null ,
103
- indeterminate : false
104
- } ;
99
+ componentWillMount ( ) {
100
+ this . generatedId = shortid . generate ( ) ;
105
101
} ,
106
102
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
- }
174
- } ,
175
-
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
103
// ### Render
188
104
render ( ) {
189
105
const {
@@ -218,16 +134,21 @@ const Checkbox = React.createClass({
218
134
{ required ? < abbr className = "slds-required" title = "required" > *</ abbr > : null }
219
135
< input
220
136
{ ...props }
221
- id = { id }
222
- checked = { indeterminate === true ? null : checked }
223
- indeterminate = { indeterminate }
137
+ id = { id || this . generatedId }
138
+ checked = { checked }
224
139
name = { name }
225
140
disabled = { disabled }
226
141
onChange = { this . handleChange }
227
142
type = "checkbox"
228
- ref = { ( component ) => this . input = component }
143
+ ref = {
144
+ ( component ) => {
145
+ if ( component ) {
146
+ component . indeterminate = indeterminate ;
147
+ }
148
+ this . input = component ;
149
+ } }
229
150
/>
230
- < label className = "slds-checkbox__label" htmlFor = { id } >
151
+ < label className = "slds-checkbox__label" htmlFor = { id || this . generatedId } >
231
152
< span className = "slds-checkbox--faux" > </ span >
232
153
{ label
233
154
? < span className = "slds-form-element__label" >
@@ -247,47 +168,19 @@ const Checkbox = React.createClass({
247
168
) ;
248
169
} ,
249
170
250
- getValue ( ) {
251
- const checkbox = this . input ;
252
- return ! checkbox . checked ;
253
- } ,
254
-
255
- isChecked ( ) {
256
- return this . getValue ( ) || false ;
257
- } ,
258
-
259
171
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
- }
172
+ const value = event . target . checked ;
173
+ const {
174
+ checked,
175
+ indeterminate,
176
+ onChange
177
+ } = this . props ;
269
178
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
179
+ if ( isFunction ( onChange ) ) {
180
+ // `checked` is present twice to maintain backwards compatibility. Please remove first parameter `value` on the next breaking change.
181
+ onChange ( value , event , {
182
+ checked : indeterminate ? true : ! checked ,
183
+ indeterminate : false
291
184
} ) ;
292
185
}
293
186
} ,
0 commit comments