import React, { PropTypes, Component } from 'react';
import Formsy from 'formsy-react';
import FRC from 'formsy-react-components';
import DateTime from './DateTime.jsx';
import Utils from './utils.js';
const Checkbox = FRC.Checkbox;
// const CheckboxGroup = FRC.CheckboxGroup;
const Input = FRC.Input;
const RadioGroup = FRC.RadioGroup;
const Select = FRC.Select;
const Textarea = FRC.Textarea;
class FormComponent extends Component {
constructor(props) {
super(props);
this.handleBlur = this.handleBlur.bind(this);
}
handleBlur() {
// see https://facebook.github.io/react/docs/more-about-refs.html
if (this.formControl !== null) {
this.props.updateCurrentValue(this.props.name, this.formControl.getValue());
}
}
renderComponent() {
// see https://facebook.github.io/react/warnings/unknown-prop.html
const { control, group, updateCurrentValue, document, ...rest } = this.props;
const base = this.props.control === "function" ? this.props : rest;
const properties = {
...base,
onBlur: this.handleBlur,
ref: (ref) => this.formControl = ref
};
// if control is a React component, use it
if (typeof this.props.control === "function") {
return
} else { // else pick a predefined component
switch (this.props.control) {
case "text":
return ;
case "textarea":
return ;
case "checkbox":
return ;
// note: checkboxgroup cause React refs error
case "checkboxgroup":
return ;
case "radiogroup":
return ;
case "select":
return ;
case "datetime":
return ;
default:
return ;
}
}
}
render() {
return (
{this.props.beforeComponent ? this.props.beforeComponent : null}
{this.renderComponent()}
{this.props.afterComponent ? this.props.afterComponent : null}
)
}
}
FormComponent.propTypes = {
document: React.PropTypes.object,
name: React.PropTypes.string,
label: React.PropTypes.string,
value: React.PropTypes.any,
placeholder: React.PropTypes.string,
prefilledValue: React.PropTypes.any,
options: React.PropTypes.any,
control: React.PropTypes.any,
datatype: React.PropTypes.any,
disabled: React.PropTypes.bool,
updateCurrentValue: React.PropTypes.func
}
export default FormComponent;
//-------------------------------------//
// having the CheckboxGroup component in this file prevents a weird bug
import ComponentMixin from './component';
import Row from './row';
const CheckboxGroup = React.createClass({
mixins: [Formsy.Mixin, ComponentMixin],
propTypes: {
name: React.PropTypes.string.isRequired,
options: React.PropTypes.array.isRequired
},
getDefaultProps: function() {
return {
label: '',
help: null
};
},
changeCheckbox: function() {
var value = [];
this.props.options.forEach(function(option, key) {
if (this.refs['element-' + key].checked) {
value.push(option.value);
}
}.bind(this));
this.setValue(value);
this.props.onChange(this.props.name, value);
},
renderElement: function() {
var _this = this;
var controls = this.props.options.map(function(checkbox, key) {
var checked = (_this.getValue().indexOf(checkbox.value) !== -1);
var disabled = _this.isFormDisabled() || checkbox.disabled || _this.props.disabled;
return (
);
});
return controls;
},
render: function() {
if (this.getLayout() === 'elementOnly') {
return (
{this.renderElement()}
);
}
return (
{this.renderElement()}
{this.renderHelp()}
{this.renderErrorMessage()}
);
}
});