scratch-www/src/components/forms/formset.jsx

98 lines
3.4 KiB
React
Raw Normal View History

2016-03-29 10:07:23 -04:00
var React = require('react');
var classNames = require('classnames');
var StepNavigationComponents = require('../stepnavigation/stepnavigation.jsx');
var StepNavigation = StepNavigationComponents.StepNavigation;
var StepNavigationIndicator = StepNavigationComponents.StepNavigationIndicator;
require('./formset.scss');
module.exports = {
FormStep: React.createClass({
type: 'FormStep',
propTypes: {
},
getDefaultProps: function () {
return {
navigation: null
2016-05-09 10:42:01 -04:00
};
2016-03-29 10:07:23 -04:00
},
onSubmit: function (e) {
e.preventDefault();
this.props.onNextStep();
},
2016-03-29 10:07:23 -04:00
render: function () {
var classes = classNames(
'step',
this.props.className
);
return (
<div {... this.props} className={classes}>
<img className="icon" src={this.props.icon} />
{this.props.description}
{this.props.navigation}
{React.Children.map(this.props.children, function (child){
if (child.type === 'form') {
return React.cloneElement(child, {onSubmit: this.onSubmit});
} else {
return child;
}
}, this)}
2016-03-29 10:07:23 -04:00
</div>
);
}
}),
FormSet: React.createClass({
type: 'FormSet',
propTypes: {
2016-05-09 10:42:01 -04:00
step: function (props, propName, componentName) {
2016-03-29 10:07:23 -04:00
var stepValidator = function (props, propName) {
if (props[propName] > -1 && props[propName] < props.children.length) {
return null;
} else {
return new Error('Prop `step` out of range');
}
2016-05-09 10:42:01 -04:00
};
2016-03-29 10:07:23 -04:00
return (
React.PropTypes.number.isRequired(props, propName, componentName) ||
stepValidator(props, propName, componentName)
);
}
},
getDefaultProps: function () {
return {
step: 0
};
},
render: function () {
var classes = classNames(
'formset',
this.props.className
);
var navigation = (
<StepNavigation>
{React.Children.map(this.props.children, function (child, id) {
2016-03-29 10:07:23 -04:00
return (
<StepNavigationIndicator key={id}
active={id <= this.props.step}
selected={id === this.props.step} />
);
}, this)}
2016-03-29 10:07:23 -04:00
</StepNavigation>);
return (
<div {... this.props} className={classes}>
{React.Children.map(this.props.children, function (child, id) {
2016-03-29 10:07:23 -04:00
if (id === this.props.step) {
return React.cloneElement(child, {onNextStep: function () {
this.props.onSetStep(this.props.step + 1);
}.bind(this)});
2016-03-29 10:07:23 -04:00
}
}, this)}
2016-03-29 10:07:23 -04:00
</div>
);
}
})
};