import React from 'react'; import FileSelector from './file-selector.js'; import {Icon} from './common.js'; var formFieldCounter = 0, formFieldFileSelectorTypes = ['file', 'directory'], 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, prefix: React.PropTypes.string, postfix: React.PropTypes.string, hasError: React.PropTypes.bool }, handleFileChosen: function(path) { this.refs.field.value = path; if (this.props.onChange) { // Updating inputs programmatically doesn't generate an event, so we have to make our own const event = new Event('change', {bubbles: true}) this.refs.field.dispatchEvent(event); // This alone won't generate a React event, but we use it to attach the field as a target this.props.onChange(event); } }, getInitialState: function() { return { isError: null, errorMessage: null, } }, componentWillMount: function() { if (['text', 'number', 'radio', 'checkbox'].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 if (formFieldFileSelectorTypes.includes(this.props.type)) { this._element = 'input'; this._type = 'hidden'; } else { // Non field, e.g.