var React = require('react'); var TextInput = require('../form/TextInput'); var StrategyForm = React.createClass({ getDefaultProps: function() { return { maxParams: 4 }; }, getInitialState: function() { return { parameters: [] }; }, onSubmit: function(event) { event.preventDefault(); var strategy = {}; strategy.name = this.refs.name.getValue(); strategy.description = this.refs.description.getValue(); strategy.parametersTemplate = {}; var that = this; this.state.parameters.forEach(function(parameter) { var name = that.refs[parameter.name].getDOMNode().value.trim(); if(name) { strategy.parametersTemplate[name] = "string"; } }); this.props.onSave(strategy); }, onCancel: function(event) { event.preventDefault(); this.props.onCancelNewStrategy(); }, onAddParam: function(event) { event.preventDefault(); var id = this.state.parameters.length + 1; var params = this.state.parameters.concat([{id:id, name: "param_" + id, label: "Parameter " +id}]); this.setState({parameters: params}); }, onRemoveParam: function(event) { event.preventDefault(); var id = this.state.parameters.length + 1; var params = this.state.parameters.slice(0, -1); this.setState({parameters: params}); }, render: function() { return (
Create strategy {this.renderParameters()} {this.renderRemoveLink()}
{this.renderAddLink()}
); }, renderParameters: function() { return this.state.parameters.map(function(param) { return (
) }); }, renderAddLink: function() { if(this.state.parameters.length < this.props.maxParams) { return + Add required parameter; } }, renderRemoveLink: function() { if(this.state.parameters.length > 0) { return (
- Remove parameter
); } } }); module.exports = StrategyForm;