Sha256: 3c5b60b94720e9c70672661c27f6120db5640454b577ff245c952279cf9c73b2

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

Rev.registerComponent('ModelInput', class ModelInput extends React.Component {
  static get mixins() {
    return [Rev.Mixins.ModelInputMixin, Backbone.Events]
  }

  static get propTypes() {
    return {
      field: React.PropTypes.string.isRequired,
      model: React.PropTypes.object.isRequired,
      name: React.PropTypes.string,
      baseName: React.PropTypes.string,
    }
  }

  constructor(props) {
    super(props)

    this.state = {
      error: null,
    }
  }

  render() {
    const formParams = {
      defaultValue: this.defaultValue(),
      name: this.name(),
    }

    if(this.isCheckbox()) {
      formParams.defaultChecked = this.value()
    }

    const type = ['textarea', 'select'].find((item) => item === this.props.type)

    return (
      <Rev.Components.Input
        key={`${this.props.model.cid}-${this.name()}`}
        dom={type}
        {...formParams}
        {...this.props}
        onChange={this.onChange.bind(this)}
        error={this.state.error}
        onBlur={this.onBlur.bind(this)}
      >
        {this.props.children}
      </Rev.Components.Input>
    )
  }

  componentWillMount() {
    this.listenTo(this.props.model, 'error', this.onError.bind(this))
  }

  componentDidUnmount() {
    this.stopListening()
  }

  onError(_model, resp, _options) {
    const errors = resp.responseJSON.errors

    if(errors && errors[this.props.field]) {
      this.setState({
        error: errors[this.props.field].join(' and '),
      })
    }
  }

  onChange(e) {
    this.setState({
      error: null,
    })
    this.onFieldChange(e)
  }
})

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
revelry_core-0.1.24.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.23.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.22.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.21.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.20.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.19.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.18.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.17.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.16.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.15.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.14.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6
revelry_core-0.1.13.0 app/assets/javascripts/revelry/ui/forms/ModelForm/ModelInput.es6