Skip to content

Commit 539f929

Browse files
jacobpennyjstirling91
authored andcommitted
Removed internal state from several input components (aces#2433)
* Removed internal state from several input components These components were duplicating/mirroring state when they could simply use props. This change gives us a single source of truth for state and is more in line with React best practices. * Only show input errors for requried fields when empty string is passed * Update form elements used in media * Pass values to form elements
1 parent d142afc commit 539f929

File tree

8 files changed

+50
-218
lines changed

8 files changed

+50
-218
lines changed

htdocs/js/components/Form.js

Lines changed: 7 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ var SelectElement = React.createClass({
164164
name: '',
165165
options: {},
166166
label: '',
167-
value: null,
167+
value: undefined,
168168
id: '',
169169
class: '',
170170
multiple: false,
@@ -178,32 +178,10 @@ var SelectElement = React.createClass({
178178
}
179179
};
180180
},
181-
componentDidMount: function componentDidMount() {
182-
if (this.props.value) {
183-
this.setState({
184-
value: this.props.value
185-
});
186-
}
187-
},
188-
componentWillReceiveProps: function componentWillReceiveProps() {
189-
if (this.props.hasError) {
190-
this.setState({
191-
hasError: this.props.hasError
192-
});
193-
}
194-
},
195-
getInitialState: function getInitialState() {
196-
var value = this.props.multiple ? [] : '';
197-
return {
198-
value: value,
199-
hasError: false
200-
};
201-
},
181+
202182
handleChange: function handleChange(e) {
203183
var value = e.target.value;
204184
var options = e.target.options;
205-
var hasError = false;
206-
var isEmpty = value === "";
207185

208186
// Multiple values
209187
if (this.props.multiple && options.length > 1) {
@@ -213,19 +191,8 @@ var SelectElement = React.createClass({
213191
value.push(options[i].value);
214192
}
215193
}
216-
isEmpty = value.length > 1;
217194
}
218195

219-
// Check for errors
220-
if (this.props.required && isEmpty) {
221-
hasError = true;
222-
}
223-
224-
this.setState({
225-
value: value,
226-
hasError: hasError
227-
});
228-
229196
this.props.onUserInput(this.props.name, value);
230197
},
231198
render: function render() {
@@ -253,7 +220,7 @@ var SelectElement = React.createClass({
253220
}
254221

255222
// Add error message
256-
if (this.state.hasError) {
223+
if (this.props.hasError || this.props.required && this.props.value === "") {
257224
errorMessage = React.createElement(
258225
'span',
259226
null,
@@ -281,7 +248,7 @@ var SelectElement = React.createClass({
281248
multiple: multiple,
282249
className: 'form-control',
283250
id: this.props.label,
284-
value: this.state.value,
251+
value: this.props.value,
285252
onChange: this.handleChange,
286253
required: required,
287254
disabled: disabled
@@ -332,20 +299,7 @@ var TextareaElement = React.createClass({
332299
}
333300
};
334301
},
335-
getInitialState: function getInitialState() {
336-
return {
337-
value: ''
338-
};
339-
},
340-
componentDidMount: function componentDidMount() {
341-
if (this.props.value) {
342-
this.setState({ value: this.props.value });
343-
}
344-
},
345302
handleChange: function handleChange(e) {
346-
this.setState({
347-
value: e.target.value
348-
});
349303
this.props.onUserInput(this.props.name, e.target.value);
350304
},
351305
render: function render() {
@@ -380,7 +334,7 @@ var TextareaElement = React.createClass({
380334
className: 'form-control',
381335
name: this.props.name,
382336
id: this.props.id,
383-
value: this.state.value,
337+
value: this.props.value,
384338
required: required,
385339
disabled: disabled,
386340
onChange: this.handleChange
@@ -406,11 +360,6 @@ var TextboxElement = React.createClass({
406360
required: React.PropTypes.bool,
407361
onUserInput: React.PropTypes.func
408362
},
409-
getInitialState: function getInitialState() {
410-
return {
411-
value: ''
412-
};
413-
},
414363
getDefaultProps: function getDefaultProps() {
415364
return {
416365
name: '',
@@ -424,17 +373,7 @@ var TextboxElement = React.createClass({
424373
}
425374
};
426375
},
427-
componentDidMount: function componentDidMount() {
428-
if (this.props.value) {
429-
this.setState({
430-
value: this.props.value
431-
});
432-
}
433-
},
434376
handleChange: function handleChange(e) {
435-
this.setState({
436-
value: e.target.value
437-
});
438377
this.props.onUserInput(this.props.name, e.target.value);
439378
},
440379
render: function render() {
@@ -468,7 +407,7 @@ var TextboxElement = React.createClass({
468407
className: 'form-control',
469408
name: this.props.name,
470409
id: this.props.id,
471-
value: this.state.value,
410+
value: this.props.value,
472411
required: required,
473412
disabled: disabled,
474413
onChange: this.handleChange
@@ -509,20 +448,7 @@ var DateElement = React.createClass({
509448
}
510449
};
511450
},
512-
getInitialState: function getInitialState() {
513-
return {
514-
value: ''
515-
};
516-
},
517-
componentDidMount: function componentDidMount() {
518-
if (this.props.value) {
519-
this.setState({ value: this.props.value });
520-
}
521-
},
522451
handleChange: function handleChange(e) {
523-
this.setState({
524-
value: e.target.value
525-
});
526452
this.props.onUserInput(this.props.name, e.target.value);
527453
},
528454
render: function render() {
@@ -559,7 +485,7 @@ var DateElement = React.createClass({
559485
min: this.props.minYear,
560486
max: this.props.maxYear,
561487
onChange: this.handleChange,
562-
value: this.state.value,
488+
value: this.props.value,
563489
required: required,
564490
disabled: disabled
565491
})
@@ -586,11 +512,6 @@ var NumericElement = React.createClass({
586512
required: React.PropTypes.bool,
587513
onUserInput: React.PropTypes.func
588514
},
589-
getInitialState: function getInitialState() {
590-
return {
591-
value: ''
592-
};
593-
},
594515
getDefaultProps: function getDefaultProps() {
595516
return {
596517
name: '',
@@ -606,17 +527,7 @@ var NumericElement = React.createClass({
606527
}
607528
};
608529
},
609-
componentDidMount: function componentDidMount() {
610-
if (this.props.value) {
611-
this.setState({
612-
value: this.props.value
613-
});
614-
}
615-
},
616530
handleChange: function handleChange(e) {
617-
this.setState({
618-
value: e.target.value
619-
});
620531
this.props.onUserInput(this.props.name, e.target.value);
621532
},
622533
render: function render() {
@@ -970,9 +881,6 @@ var ButtonElement = React.createClass({
970881
type: React.PropTypes.string,
971882
onUserInput: React.PropTypes.func
972883
},
973-
getInitialState: function getInitialState() {
974-
return {};
975-
},
976884
getDefaultProps: function getDefaultProps() {
977885
return {
978886
label: 'Submit',

0 commit comments

Comments
 (0)