1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-frontend/public/js/components/strategy/StrategyForm.jsx

140 lines
4.3 KiB
React
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
const React = require('react');
const TextInput = require('../form/TextInput');
2016-06-18 21:53:18 +02:00
const StrategyForm = React.createClass({
2016-07-02 11:54:50 +02:00
getDefaultProps () {
return {
2016-06-18 21:55:46 +02:00
maxParams: 4,
};
},
2016-07-02 11:54:50 +02:00
getInitialState () {
return {
2016-06-18 21:55:46 +02:00
parameters: [],
};
},
onSubmit (evt) {
evt.preventDefault();
2016-06-18 21:53:18 +02:00
const strategy = {};
strategy.name = this.refs.name.getValue();
strategy.description = this.refs.description.getValue();
strategy.parametersTemplate = {};
2016-06-18 21:53:18 +02:00
this.state.parameters.forEach(parameter => {
const value = this.refs[parameter.name].getDOMNode().value.trim();
if (value) {
strategy.parametersTemplate[value] = 'string';
}
});
this.props.onSave(strategy);
},
onCancel (evt) {
evt.preventDefault();
2014-11-25 13:54:25 +01:00
this.props.onCancelNewStrategy();
},
onAddParam (evt) {
evt.preventDefault();
2016-06-18 21:53:18 +02:00
const id = this.state.parameters.length + 1;
2016-06-18 21:55:46 +02:00
const params = this.state.parameters.concat([{ id, name: `param_${id}`, label: `Parameter ${id}` }]);
this.setState({ parameters: params });
},
onRemoveParam (evt) {
evt.preventDefault();
2016-06-18 21:53:18 +02:00
const params = this.state.parameters.slice(0, -1);
2016-06-18 21:55:46 +02:00
this.setState({ parameters: params });
},
2016-07-02 11:54:50 +02:00
render () {
return (
<div className="line r-pam bg-lilac-xlt">
<div className="unit r-size1of2">
<form onSubmit={this.onSubmit}>
<fieldset>
<legend>Create strategy</legend>
<TextInput
id="name"
name="name"
label="Name"
ref="name"
placeholder="Strategy name" />
<TextInput
id="description"
name="description"
label="Description"
ref="description"
2014-11-25 13:54:25 +01:00
placeholder="Please write a short description" />
{this.renderParameters()}
{this.renderRemoveLink()}
<div className="actions">
<input type="submit" value="Save" className="primary mrs" />
2014-11-25 13:54:25 +01:00
<button onClick={this.onCancel} className="mrs">Cancel</button>
{this.renderAddLink()}
</div>
</fieldset>
</form>
</div>
</div>
);
},
2016-07-02 11:54:50 +02:00
renderParameters () {
2016-06-18 21:53:18 +02:00
return this.state.parameters.map(param => <div className="formelement" key={param.name}>
<label className="t4">{param.label}</label>
<div className="input">
<div className="line">
<div className="unit size2of3">
<input
type="text"
name={param.name}
ref={param.name}
placeholder="Parameter name"
/>
</div>
2016-06-18 21:53:18 +02:00
<div className="unit size1of3">
<select defaultValue="string">
<option value="string">string</option>
</select>
</div>
</div>
2016-06-18 21:53:18 +02:00
</div>
</div>);
},
2016-07-02 11:54:50 +02:00
renderAddLink () {
2016-06-18 21:55:46 +02:00
if (this.state.parameters.length < this.props.maxParams) {
return <a href="#add" onClick={this.onAddParam}>+ Add required parameter</a>;
}
},
2016-07-02 11:54:50 +02:00
renderRemoveLink () {
2016-06-18 21:55:46 +02:00
if (this.state.parameters.length > 0) {
return (
<div className="formelement mtn">
2015-03-23 18:47:23 +01:00
<a href="#add"
className="negative"
onClick={this.onRemoveParam}>
- Remove parameter
</a>
</div>
);
}
2016-06-18 21:55:46 +02:00
},
});
2015-03-23 18:47:23 +01:00
module.exports = StrategyForm;