0.0.13 • Published 10 years ago

react-rapid v0.0.13

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

The library allows you to define the structure of you data:

var { DataDefinition } = require('react-rapid');

var personDataDefinition = new DataDefinition({
  Name: {
    required: true,
    maxLength: 40
  },
  ContactMethod: {
    required: true
  },
  Phone: {
    required: function(getState) {
      return getState().state.ContactMethod == 'Mobile';
    },
    validate: function(getState) {
      var phone = getState().state.Phone;
      var validationResult = true;
      // ... validate phone
      return validationResult;
    }
  }
});

which can be wrapped into "wrappedPersonData" object that makes it easier to work with data in React components:

var App = React.createClass({
  componentWillMount: function() {
    this.wrappedPersonData = personDataDefinition.getWrappedData(
      function() { return { state: this.state, getParentState: function() { return null; } }; }.bind(this),
      function(newState) { this.setState(newState); }.bind(this)
    );
  },
  
  render: function() {
    var data = this.wrappedPersonData.data;
    return <div>
      <TextInputElement data={data.Name} label="Name:" />
      <DropDownElement data={data.ContactMethod} label="Contact Method:" />
      <PhoneElement data={data.Phone} label="Phone Number:" />
    </div>;
  }
});

var TextInputElement = React.createClass({
  _handleChange: function() {
    var newValue = event.target.value;
    var maxLength = this.props.data.meta.maxLength;
    if(!_.isUndefined(maxLength)) {
      newVal = newVal.substring(0, +maxLength);
    }
    this.props.data.val(newValue);
  },
  render: function() {
    return <input value={this.props.data.val()} onChange={this._handleChange} />;
  }
});
0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago