2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
2017-01-18 04:36:48 +01:00
|
|
|
import {Icon} from './common.js';
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
var requiredFieldWarningStyle = {
|
|
|
|
color: '#cc0000',
|
|
|
|
transition: 'opacity 400ms ease-in',
|
|
|
|
};
|
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
var formFieldCounter = 0;
|
|
|
|
|
2016-08-07 22:10:44 +02:00
|
|
|
var FormField = React.createClass({
|
2016-11-18 09:43:28 +01:00
|
|
|
_fieldRequiredText: 'This field is required',
|
2016-08-07 22:10:44 +02:00
|
|
|
_type: null,
|
|
|
|
_element: null,
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
type: React.PropTypes.string.isRequired,
|
2017-04-09 17:06:23 +02:00
|
|
|
row: React.PropTypes.bool,
|
2016-08-07 22:10:44 +02:00
|
|
|
hidden: React.PropTypes.bool,
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2017-04-09 17:06:23 +02:00
|
|
|
errorState: 'hidden',
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceText: null,
|
2016-08-07 22:10:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
2017-03-01 21:31:05 +01:00
|
|
|
if (['text', 'radio', 'checkbox', 'file'].includes(this.props.type)) {
|
2016-08-07 22:10:44 +02:00
|
|
|
this._element = 'input';
|
|
|
|
this._type = this.props.type;
|
2017-03-01 21:31:05 +01:00
|
|
|
} else if (this.props.type == 'text-number') {
|
|
|
|
this._element = 'input';
|
|
|
|
this._type = 'text';
|
2016-08-07 22:10:44 +02:00
|
|
|
} else {
|
|
|
|
// Non <input> field, e.g. <select>, <textarea>
|
|
|
|
this._element = this.props.type;
|
|
|
|
}
|
|
|
|
},
|
2017-04-09 17:06:23 +02:00
|
|
|
showError: function(text) {
|
2016-08-07 22:10:44 +02:00
|
|
|
this.setState({
|
2017-04-09 17:06:23 +02:00
|
|
|
errorState: 'shown',
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceText: text,
|
2016-08-07 22:10:44 +02:00
|
|
|
});
|
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
// setTimeout(() => {
|
|
|
|
// this.setState({
|
|
|
|
// errorState: 'fading',
|
|
|
|
// });
|
|
|
|
// setTimeout(() => {
|
|
|
|
// this.setState({
|
|
|
|
// errorState: 'hidden',
|
|
|
|
// });
|
|
|
|
// }, 450);
|
|
|
|
// }, 5000);
|
2016-08-07 22:10:44 +02:00
|
|
|
},
|
2016-11-18 09:43:28 +01:00
|
|
|
warnRequired: function() {
|
2017-04-09 17:06:23 +02:00
|
|
|
this.showError(this._fieldRequiredText);
|
2016-11-18 09:43:28 +01:00
|
|
|
},
|
2016-08-07 22:10:44 +02:00
|
|
|
focus: function() {
|
|
|
|
this.refs.field.focus();
|
|
|
|
},
|
|
|
|
getValue: function() {
|
|
|
|
if (this.props.type == 'checkbox') {
|
|
|
|
return this.refs.field.checked;
|
2017-03-01 21:31:05 +01:00
|
|
|
} else if (this.props.type == 'file') {
|
2017-02-08 20:11:58 +01:00
|
|
|
return this.refs.field.files[0].path;
|
2016-08-07 22:10:44 +02:00
|
|
|
} else {
|
|
|
|
return this.refs.field.value;
|
|
|
|
}
|
|
|
|
},
|
2016-09-20 12:38:46 +02:00
|
|
|
getSelectedElement: function() {
|
|
|
|
return this.refs.field.options[this.refs.field.selectedIndex];
|
|
|
|
},
|
2016-08-07 22:10:44 +02:00
|
|
|
render: function() {
|
|
|
|
// Pass all unhandled props to the field element
|
2017-04-09 17:06:23 +02:00
|
|
|
const otherProps = Object.assign({}, this.props),
|
|
|
|
hasError = this.state.errorState != 'hidden';
|
2016-08-07 22:10:44 +02:00
|
|
|
delete otherProps.type;
|
|
|
|
delete otherProps.hidden;
|
2017-04-09 17:06:23 +02:00
|
|
|
delete otherProps.label;
|
|
|
|
delete otherProps.row;
|
|
|
|
delete otherProps.helper;
|
2016-08-07 22:10:44 +02:00
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
++formFieldCounter;
|
|
|
|
const elementId = "form-field-" + formFieldCounter
|
2016-11-18 09:05:28 +01:00
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
if (this.props.hidden) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const field = <div className="form-field">
|
|
|
|
{ this.props.label ?
|
|
|
|
<div className={"form-field__label " + (hasError ? 'form-field__label--error' : '')}>
|
|
|
|
<label htmlFor={elementId}>{this.props.label}</label>
|
|
|
|
</div> : ''
|
|
|
|
}
|
|
|
|
<this._element id={elementId} type={this._type} name={this.props.name} ref="field" placeholder={this.props.placeholder}
|
|
|
|
className={'form-field__input form-field__input-' + this.props.type + ' ' + (this.props.className || '') + (hasError ? 'form-field__input--error' : '')}
|
|
|
|
{...otherProps}>
|
|
|
|
{this.props.children}
|
|
|
|
</this._element>
|
|
|
|
{ !hasError && this.props.helper ? <div className="form-field__helper">{this.props.helper}</div> : '' }
|
|
|
|
{ hasError ? <div className="form-field__error">{this.state.adviceText}</div> : '' }
|
|
|
|
</div>
|
2016-11-18 09:05:28 +01:00
|
|
|
return (
|
2017-04-09 17:06:23 +02:00
|
|
|
this.props.row ?
|
|
|
|
<div className="form-row">{field}</div> :
|
|
|
|
field
|
2016-08-07 22:10:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
export default FormField;
|