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

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

merged 4 commits into from
Nov 28, 2014

Conversation

romeovs
Copy link
Contributor

@romeovs romeovs commented Nov 28, 2014

I've changed the way the and Input component decides whether there is an error or not.

Input doesn't have any state related to errors any more. Instead the error is hold completely inside props.

Some examples:

// this will show an error
<Input name="input" error="this is an error" />
// these will not show an error
<Input name="input" />
<Input name="input" error={undefined} />
<Input name="input" error={null} />

This is more declarative than having to call setError and removeError and allows components to
use the Input more concisely.

Additionally I also made type="text" default for the Input (although this is something debatable).

@romeovs romeovs mentioned this pull request Nov 28, 2014
hai-cea added a commit that referenced this pull request Nov 28, 2014
Make Input component more useful (respects error prop declaratively)
@hai-cea hai-cea merged commit a0fe16e into mui:master Nov 28, 2014
@hai-cea
Copy link
Member

hai-cea commented Nov 28, 2014

Thanks @romeovs ! Fixes #95

},

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

@mikepb
Copy link
Contributor

mikepb commented Nov 28, 2014

Better to use:

  componentWillReceiveProps: function (nextProps) {
    if (nextProps.error) {
      this.setState({
        error: nextProps.error
      });
    }
  },

@romeovs
Copy link
Contributor Author

romeovs commented Nov 29, 2014

I'm not a frequent React user so I might be confused here, but I recall reading that using prop values for the state of the component is bad because it duplicates the "source of truth" (eg. all info is there in the props, so why pull it into the state?).

@mikepb
Copy link
Contributor

mikepb commented Dec 1, 2014

Yes, removing the set/removeError methods would most idiomatic.

@zannager zannager added the core Infrastructure work going on behind the scenes label Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants