Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Input component more useful (respects error prop declaratively) #98

Merged
merged 4 commits into from
Nov 28, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/js/input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var Input = React.createClass({
error: React.PropTypes.string,
description: React.PropTypes.string,
placeholder: React.PropTypes.string,
type: React.PropTypes.string.isRequired,
type: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func
},
Expand All @@ -25,50 +25,48 @@ var Input = React.createClass({
getInitialState: function() {
return {
value: this.props.defaultValue,
error: false,
rows: 1
};
},

getDefaultProps: function() {
return {
multiline: false,
required: true
required: true,
type: "text"
};
},

setError: function(error) {
this.props.error = error;
this.setState({ error: true });
this.setProps({ error: error });
},

removeError: function() {
this.props.error = null;
this.setState({ error: false });
this.setProps({error: undefined});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is a keyword, whereas undefined is not. Better to use null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using setProps is an anti-pattern in React.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say using setError and removeError are the actual anti-pattern.

},

render: function() {
var classes = this.getClasses('mui-input', {
'mui-floating': this.props.inputStyle === 'floating',
'mui-text': this.props.type === 'text',
'mui-error': this.state.error === true
'mui-error': this.props.error !== undefined && this.props.error !== null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error != null matches both undefined and null

}),
inputElement = this.props.multiline ?
this.props.valueLink ?
<textarea {...this.props} className="mui-input-textarea" placeholder=""
rows={this.state.rows} /> :
<textarea {...this.props} value={this.state.value} className="mui-input-textarea"
placeholder="" rows={this.state.rows} onChange={this._onTextAreaChange} /> :
this.props.valueLink ?
<input {...this.props} ref="input" placeholder="" /> :
<input {...this.props} ref="input" value={this.state.value} placeholder=""
onChange={this._onInputChange} />;
inputElement = this.props.multiline ?
this.props.valueLink ?
<textarea {...this.props} className="mui-input-textarea" placeholder=""
rows={this.state.rows} /> :
<textarea {...this.props} value={this.state.value} className="mui-input-textarea"
placeholder="" rows={this.state.rows} onChange={this._onTextAreaChange} /> :
this.props.valueLink ?
<input {...this.props} ref="input" placeholder="" /> :
<input {...this.props} ref="input" value={this.state.value} placeholder=""
onChange={this._onInputChange} />;

return (
<div ref={this.props.ref} className={classes}>
{inputElement}
<span className="mui-input-placeholder"
onClick={this._onPlaceholderClick}>{this.props.placeholder}</span>
onClick={this._onPlaceholderClick}>{this.props.placeholder}</span>
<span className="mui-input-highlight"></span>
<span className="mui-input-bar"></span>
<span className="mui-input-description">{this.props.description}</span>
Expand Down