2018-03-26 23:32:43 +02:00
|
|
|
// This file is going to die
|
|
|
|
/* eslint-disable */
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-03-26 23:32:43 +02:00
|
|
|
import FileSelector from 'component/common/file-selector';
|
2017-12-21 22:08:54 +01:00
|
|
|
import SimpleMDE from 'react-simplemde-editor';
|
2018-03-26 23:32:43 +02:00
|
|
|
import { formFieldNestedLabelTypes, formFieldId } from 'component/common/form';
|
2017-12-21 22:08:54 +01:00
|
|
|
import style from 'react-simplemde-editor/dist/simplemde.min.css';
|
2017-08-07 00:24:55 +02:00
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
const formFieldFileSelectorTypes = ['file', 'directory'];
|
2017-08-07 00:24:55 +02:00
|
|
|
|
|
|
|
class FormField extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2017-11-29 06:03:53 +01:00
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
prefix: PropTypes.string,
|
|
|
|
postfix: PropTypes.string,
|
|
|
|
hasError: PropTypes.bool,
|
|
|
|
trim: PropTypes.bool,
|
2017-12-21 22:08:54 +01:00
|
|
|
regexp: PropTypes.oneOfType([PropTypes.instanceOf(RegExp), PropTypes.string]),
|
2017-09-01 08:14:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
trim: false,
|
2017-08-07 00:24:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
this._fieldRequiredText = __('This field is required');
|
2017-08-07 00:24:55 +02:00
|
|
|
this._type = null;
|
|
|
|
this._element = null;
|
|
|
|
this._extraElementProps = {};
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isError: null,
|
|
|
|
errorMessage: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-12-21 22:08:54 +01:00
|
|
|
if (['text', 'number', 'radio', 'checkbox'].includes(this.props.type)) {
|
|
|
|
this._element = 'input';
|
2017-08-07 00:24:55 +02:00
|
|
|
this._type = this.props.type;
|
2017-12-21 22:08:54 +01:00
|
|
|
} else if (this.props.type == 'text-number') {
|
|
|
|
this._element = 'input';
|
|
|
|
this._type = 'text';
|
|
|
|
} else if (this.props.type == 'SimpleMDE') {
|
2017-08-07 00:24:55 +02:00
|
|
|
this._element = SimpleMDE;
|
2017-12-21 22:08:54 +01:00
|
|
|
this._type = 'textarea';
|
2017-08-07 00:24:55 +02:00
|
|
|
this._extraElementProps.options = {
|
2017-10-16 02:38:47 +02:00
|
|
|
placeholder: this.props.placeholder,
|
2017-12-21 22:08:54 +01:00
|
|
|
hideIcons: ['heading', 'image', 'fullscreen', 'side-by-side'],
|
2017-08-07 00:24:55 +02:00
|
|
|
};
|
|
|
|
} else if (formFieldFileSelectorTypes.includes(this.props.type)) {
|
2017-12-21 22:08:54 +01:00
|
|
|
this._element = 'input';
|
|
|
|
this._type = 'hidden';
|
2017-08-07 00:24:55 +02:00
|
|
|
} else {
|
|
|
|
// Non <input> field, e.g. <select>, <textarea>
|
|
|
|
this._element = this.props.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
/**
|
|
|
|
* We have to add the webkitdirectory attribute here because React doesn't allow it in JSX
|
|
|
|
* https://github.com/facebook/react/issues/3468
|
|
|
|
*/
|
2017-12-21 22:08:54 +01:00
|
|
|
if (this.props.type == 'directory') {
|
2017-08-07 00:24:55 +02:00
|
|
|
this.refs.field.webkitdirectory = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFileChosen(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
|
2017-12-21 22:08:54 +01:00
|
|
|
const event = new Event('change', { bubbles: true });
|
2017-08-07 00:24:55 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showError(text) {
|
|
|
|
this.setState({
|
|
|
|
isError: true,
|
|
|
|
errorMessage: text,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-01 08:14:38 +02:00
|
|
|
clearError() {
|
|
|
|
this.setState({
|
|
|
|
isError: false,
|
2017-12-21 22:08:54 +01:00
|
|
|
errorMessage: '',
|
2017-09-01 08:14:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:24:55 +02:00
|
|
|
getValue() {
|
2017-12-21 22:08:54 +01:00
|
|
|
if (this.props.type == 'checkbox') {
|
2017-08-07 00:24:55 +02:00
|
|
|
return this.refs.field.checked;
|
2017-12-21 22:08:54 +01:00
|
|
|
} else if (this.props.type == 'SimpleMDE') {
|
2017-08-07 00:24:55 +02:00
|
|
|
return this.refs.field.simplemde.value();
|
|
|
|
}
|
2017-12-21 22:08:54 +01:00
|
|
|
return this.props.trim ? this.refs.field.value.trim() : this.refs.field.value;
|
2017-08-07 00:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedElement() {
|
|
|
|
return this.refs.field.options[this.refs.field.selectedIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptions() {
|
|
|
|
return this.refs.field.options;
|
|
|
|
}
|
|
|
|
|
2017-09-01 08:14:38 +02:00
|
|
|
validate() {
|
2017-12-21 22:08:54 +01:00
|
|
|
if ('regexp' in this.props) {
|
2017-09-01 08:14:38 +02:00
|
|
|
if (!this.getValue().match(this.props.regexp)) {
|
2017-12-21 22:08:54 +01:00
|
|
|
this.showError(__('Invalid format.'));
|
2017-09-01 08:14:38 +02:00
|
|
|
} else {
|
|
|
|
this.clearError();
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 00:03:05 +02:00
|
|
|
this.props.onBlur && this.props.onBlur();
|
2017-09-01 08:14:38 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:39:19 +02:00
|
|
|
focus() {
|
|
|
|
this.refs.field.focus();
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:24:55 +02:00
|
|
|
render() {
|
|
|
|
// Pass all unhandled props to the field element
|
|
|
|
const otherProps = Object.assign({}, this.props),
|
2017-12-21 22:08:54 +01:00
|
|
|
isError = this.state.isError !== null ? this.state.isError : this.props.hasError,
|
2017-08-07 00:24:55 +02:00
|
|
|
elementId = this.props.elementId ? this.props.elementId : formFieldId(),
|
|
|
|
renderElementInsideLabel =
|
|
|
|
this.props.label && formFieldNestedLabelTypes.includes(this.props.type);
|
|
|
|
|
|
|
|
delete otherProps.type;
|
|
|
|
delete otherProps.label;
|
|
|
|
delete otherProps.hasError;
|
|
|
|
delete otherProps.className;
|
|
|
|
delete otherProps.postfix;
|
|
|
|
delete otherProps.prefix;
|
|
|
|
delete otherProps.dispatch;
|
2017-09-01 08:14:38 +02:00
|
|
|
delete otherProps.regexp;
|
|
|
|
delete otherProps.trim;
|
2017-08-07 00:24:55 +02:00
|
|
|
|
|
|
|
const element = (
|
|
|
|
<this._element
|
|
|
|
id={elementId}
|
|
|
|
type={this._type}
|
|
|
|
name={this.props.name}
|
|
|
|
ref="field"
|
|
|
|
placeholder={this.props.placeholder}
|
2017-09-01 08:14:38 +02:00
|
|
|
onBlur={() => this.validate()}
|
2017-10-15 00:03:05 +02:00
|
|
|
onFocus={() => this.props.onFocus && this.props.onFocus()}
|
2017-12-21 22:08:54 +01:00
|
|
|
className={`form-field__input form-field__input-${this.props.type} ${this.props.className ||
|
|
|
|
''}${isError ? 'form-field__input--error' : ''}`}
|
2017-08-07 00:24:55 +02:00
|
|
|
{...otherProps}
|
|
|
|
{...this._extraElementProps}
|
|
|
|
>
|
|
|
|
{this.props.children}
|
|
|
|
</this._element>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2017-12-21 22:08:54 +01:00
|
|
|
<div className={`form-field form-field--${this.props.type}`}>
|
|
|
|
{this.props.prefix ? <span className="form-field__prefix">{this.props.prefix}</span> : ''}
|
2017-10-15 01:41:41 +02:00
|
|
|
{element}
|
2017-11-24 15:31:05 +01:00
|
|
|
{renderElementInsideLabel && (
|
2017-10-15 01:41:41 +02:00
|
|
|
<label
|
|
|
|
htmlFor={elementId}
|
2017-12-21 22:08:54 +01:00
|
|
|
className={`form-field__label ${isError ? 'form-field__label--error' : ''}`}
|
2017-10-15 01:41:41 +02:00
|
|
|
>
|
|
|
|
{this.props.label}
|
2017-11-24 15:31:05 +01:00
|
|
|
</label>
|
|
|
|
)}
|
|
|
|
{formFieldFileSelectorTypes.includes(this.props.type) ? (
|
|
|
|
<FileSelector
|
|
|
|
type={this.props.type}
|
|
|
|
onFileChosen={this.handleFileChosen.bind(this)}
|
2017-12-21 22:08:54 +01:00
|
|
|
{...(this.props.defaultValue ? { initPath: this.props.defaultValue } : {})}
|
2017-11-24 15:31:05 +01:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{this.props.postfix ? (
|
|
|
|
<span className="form-field__postfix">{this.props.postfix}</span>
|
|
|
|
) : (
|
2017-12-21 22:08:54 +01:00
|
|
|
''
|
2017-11-24 15:31:05 +01:00
|
|
|
)}
|
|
|
|
{isError && this.state.errorMessage ? (
|
|
|
|
<div className="form-field__error">{this.state.errorMessage}</div>
|
|
|
|
) : (
|
2017-12-21 22:08:54 +01:00
|
|
|
''
|
2017-11-24 15:31:05 +01:00
|
|
|
)}
|
2017-08-07 00:24:55 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FormField;
|
2018-03-26 23:32:43 +02:00
|
|
|
/* eslint-enable */
|