Created
April 30, 2017 09:57
-
-
Save timcase/3cfdc93fba835b511bdddc1e997a2e20 to your computer and use it in GitHub Desktop.
Redux form with lore bootstrap create form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
var DialogMixin = require('../mixins/DialogMixin'); | |
var StringField = require('../fields/StringField'); | |
var TextField = require('../fields/TextField'); | |
var BooleanField = require('../fields/BooleanField'); | |
var NumberField = require('../fields/NumberField'); | |
var _ = require('lodash'); | |
var reduxForm = require('redux-form').reduxForm; | |
var Field = require('redux-form').Field; | |
module.exports = function(options) { | |
options = options || {}; | |
console.log(options); | |
var title = options.title; | |
var cancelButtonText = options.cancelButtonText; | |
var submitButtonText = options.submitButtonText; | |
var attributes = options.attributes; | |
var defaults = options.defaults; | |
var attributeNames = _.keys(attributes); | |
var validations = function(values){ | |
var errors = []; | |
if (!values.text) { | |
errors.text = 'Required' | |
} | |
return errors; | |
}; | |
return reduxForm({form: 'createTweet', validate: validations})(React.createClass({ | |
displayName: 'Dialog', | |
mixins: [DialogMixin], | |
getInitialState: function() { | |
var that = this; | |
// Create change callbacks | |
this.onChangeCallbacks = _.reduce(attributeNames, function(callbacks, attributeName) { | |
callbacks[attributeName] = function(event) { | |
var newState = {}; | |
newState[attributeName] = event.target.value; | |
that.setState(newState); | |
}; | |
return callbacks; | |
}, {}); | |
// Create initial state | |
return _.reduce(attributeNames, function(initialState, attributeName) { | |
initialState[attributeName] = defaults[attributeName] || attributes[attributeName].defaultValue; | |
return initialState; | |
}, {}); | |
}, | |
render: function () { | |
var formFields = _.map(attributeNames, function(attributeName) { | |
var attribute = attributes[attributeName]; | |
if (attribute.type === 'string') { | |
return ( | |
<StringField | |
key={attributeName} | |
attribute={attribute} | |
label={attributeName} | |
value={this.state[attributeName]} | |
onChange={this.onChangeCallbacks[attributeName]} /> | |
); | |
} else if (attribute.type === 'text') { | |
return ( | |
<Field | |
key={attributeName} | |
name={attributeName} | |
attribute={attribute} | |
label={attributeName} | |
value={this.state[attributeName]} | |
component={TextField} | |
/> | |
); | |
} else if (attribute.type === 'boolean') { | |
return ( | |
<BooleanField | |
key={attributeName} | |
attribute={attribute} | |
label={attributeName} | |
value={this.state[attributeName]} | |
onChange={this.onChangeCallbacks[attributeName]} /> | |
); | |
} else if (attribute.type === 'number') { | |
return ( | |
<NumberField | |
key={attributeName} | |
attribute={attribute} | |
label={attributeName} | |
value={this.state[attributeName]} | |
onChange={this.onChangeCallbacks[attributeName]} /> | |
); | |
} | |
}.bind(this)); | |
return ( | |
<div className="modal fade"> | |
<div className="modal-dialog"> | |
<div className="modal-content"> | |
<div className="modal-header"> | |
<button type="button" className="close" data-dismiss="modal"> | |
<span>×</span> | |
</button> | |
<h4 className="modal-title"> | |
{title} | |
</h4> | |
</div> | |
<div className="modal-body"> | |
<form> | |
{formFields} | |
</form> | |
</div> | |
<div className="modal-footer"> | |
<button type="button" className="btn btn-default" data-dismiss="modal"> | |
{cancelButtonText} | |
</button> | |
<button type="submit" onClick={this.props.handleSubmit(this.onSubmit)} className="btn btn-primary"> | |
{submitButtonText} | |
</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
); | |
} | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment