2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
var requiredFieldWarningStyle = {
|
|
|
|
color: '#cc0000',
|
|
|
|
transition: 'opacity 400ms ease-in',
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
hidden: React.PropTypes.bool,
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceState: 'hidden',
|
|
|
|
adviceText: null,
|
2016-08-07 22:10:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
|
|
|
if (['text', 'radio', 'checkbox', 'file'].indexOf(this.props.type) != -1) {
|
|
|
|
this._element = 'input';
|
|
|
|
this._type = this.props.type;
|
|
|
|
} else {
|
|
|
|
// Non <input> field, e.g. <select>, <textarea>
|
|
|
|
this._element = this.props.type;
|
|
|
|
}
|
|
|
|
},
|
2016-11-18 09:43:28 +01:00
|
|
|
showAdvice: function(text) {
|
2016-08-07 22:10:44 +02:00
|
|
|
this.setState({
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceState: 'shown',
|
|
|
|
adviceText: text,
|
2016-08-07 22:10:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceState: 'fading',
|
2016-08-07 22:10:44 +02:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({
|
2016-11-18 09:43:28 +01:00
|
|
|
adviceState: 'hidden',
|
2016-08-07 22:10:44 +02:00
|
|
|
});
|
|
|
|
}, 450);
|
|
|
|
}, 5000);
|
|
|
|
},
|
2016-11-18 09:43:28 +01:00
|
|
|
warnRequired: function() {
|
|
|
|
this.showAdvice(this._fieldRequiredText);
|
|
|
|
},
|
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;
|
|
|
|
} 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
|
|
|
|
var otherProps = Object.assign({}, this.props);
|
|
|
|
delete otherProps.type;
|
|
|
|
delete otherProps.hidden;
|
|
|
|
|
|
|
|
return (
|
2016-11-22 06:24:29 +01:00
|
|
|
!this.props.hidden
|
|
|
|
? <div className="form-field-container">
|
|
|
|
<this._element type={this._type} className="form-field" name={this.props.name} ref="field" placeholder={this.props.placeholder}
|
|
|
|
{...otherProps}>
|
|
|
|
{this.props.children}
|
|
|
|
</this._element>
|
|
|
|
<FormFieldAdvice field={this.refs.field} state={this.state.adviceState}>{this.state.adviceText}</FormFieldAdvice>
|
|
|
|
</div>
|
|
|
|
: null
|
2016-11-18 09:05:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var FormFieldAdvice = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
state: React.PropTypes.string.isRequired,
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
this.props.state != 'hidden'
|
|
|
|
? <div className="form-field-advice-container">
|
|
|
|
<div className={'form-field-advice' + (this.props.state == 'fading' ? ' form-field-advice--fading' : '')}>
|
|
|
|
<Icon icon="icon-caret-up" className="form-field-advice__arrow" />
|
|
|
|
<div className="form-field-advice__content-container">
|
|
|
|
<span className="form-field-advice__content">
|
|
|
|
{this.props.children}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
: null
|
2016-08-07 22:10:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
export default FormField;
|