import React from 'react'; import {Icon} from './common.js'; var formFieldCounter = 0, formFieldNestedLabelTypes = ['radio', 'checkbox']; function formFieldId() { return "form-field-" + (++formFieldCounter); } export let FormField = React.createClass({ _fieldRequiredText: 'This field is required', _type: null, _element: null, propTypes: { type: React.PropTypes.string.isRequired, hasError: React.PropTypes.bool }, getInitialState: function() { return { isError: null, errorMessage: null, } }, componentWillMount: function() { if (['text', 'number', 'radio', 'checkbox', 'file'].includes(this.props.type)) { this._element = 'input'; this._type = this.props.type; } else if (this.props.type == 'text-number') { this._element = 'input'; this._type = 'text'; } else { // Non field, e.g. , this._element = this.props.type; } }, showError: function(text) { this.setState({ isError: true, errorMessage: text, }); }, showRequiredError: function() { this.showError(this._fieldRequiredText); }, focus: function() { this.refs.field.focus(); }, getValue: function() { if (this.props.type == 'checkbox') { return this.refs.field.checked; } else if (this.props.type == 'file') { return this.refs.field.files[0].path; } else { return this.refs.field.value; } }, getSelectedElement: function() { return this.refs.field.options[this.refs.field.selectedIndex]; }, render: function() { // Pass all unhandled props to the field element const otherProps = Object.assign({}, this.props), isError = this.state.isError !== null ? this.state.isError : this.props.hasError, elementId = this.props.id ? this.props.id : formFieldId(), renderElementInsideLabel = this.props.label && formFieldNestedLabelTypes.includes(this.props.type); delete otherProps.type; delete otherProps.label; delete otherProps.hasError; const element = {this.props.children} ; return { renderElementInsideLabel ? {element} {this.props.label} : element } { isError ? {this.state.errorMessage} : '' } return ( this.props.row ? {field} : field ); } }) export let FormRow = React.createClass({ propTypes: { label: React.PropTypes.string, // helper: React.PropTypes.html, }, getValue: function() { if (this.props.type == 'checkbox') { return this.refs.field.checked; } else if (this.props.type == 'file') { return this.refs.field.files[0].path; } else { return this.refs.field.value; } }, getInitialState: function() { return { isError: false, errorMessage: null, } }, showError: function(text) { this.setState({ isError: true, errorMessage: text, }); }, getValue: function() { return this.refs.field.getValue(); }, render: function() { const fieldProps = Object.assign({}, this.props), elementId = formFieldId(), renderLabelInFormField = formFieldNestedLabelTypes.includes(this.props.type); if (!renderLabelInFormField) { delete fieldProps.label; } delete fieldProps.helper; return { this.props.label && !renderLabelInFormField ? {this.props.label} : '' } { !this.state.isError && this.props.helper ? {this.props.helper} : '' } { this.state.isError ? {this.state.errorMessage} : '' } } })