2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2018-06-10 23:57:46 +02:00
|
|
|
import ReactDOMServer from 'react-dom/server';
|
2018-03-26 23:32:43 +02:00
|
|
|
import classnames from 'classnames';
|
2018-06-10 23:57:46 +02:00
|
|
|
import MarkdownPreview from 'component/common/markdown-preview';
|
2018-03-26 23:32:43 +02:00
|
|
|
import SimpleMDE from 'react-simplemde-editor';
|
2018-07-25 02:50:04 +02:00
|
|
|
import 'simplemde/dist/simplemde.min.css'; // eslint-disable-line import/no-extraneous-dependencies
|
2018-06-15 22:11:02 +02:00
|
|
|
import Toggle from 'react-toggle';
|
2018-07-29 01:48:54 +02:00
|
|
|
import { openEditorMenu, stopContextMenu } from 'util/contextMenu';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
name: string,
|
|
|
|
label?: string,
|
|
|
|
render?: () => React.Node,
|
|
|
|
prefix?: string,
|
|
|
|
postfix?: string,
|
|
|
|
error?: string | boolean,
|
|
|
|
helper?: string | React.Node,
|
|
|
|
type?: string,
|
|
|
|
onChange?: any => any,
|
|
|
|
defaultValue?: string | number,
|
|
|
|
placeholder?: string | number,
|
|
|
|
children?: React.Node,
|
|
|
|
stretch?: boolean,
|
2018-06-14 22:10:50 +02:00
|
|
|
affixClass?: string, // class applied to prefix/postfix label
|
2018-07-25 20:21:41 +02:00
|
|
|
firstInList?: boolean, // at the top of a list, no padding top
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export class FormField extends React.PureComponent<Props> {
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
render,
|
|
|
|
label,
|
|
|
|
prefix,
|
|
|
|
postfix,
|
|
|
|
error,
|
|
|
|
helper,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
stretch,
|
2018-06-14 22:10:50 +02:00
|
|
|
affixClass,
|
2018-03-26 23:32:43 +02:00
|
|
|
...inputProps
|
|
|
|
} = this.props;
|
|
|
|
|
2018-04-05 23:26:20 +02:00
|
|
|
const errorMessage = typeof error === 'object' ? error.message : error;
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
let input;
|
|
|
|
if (type) {
|
|
|
|
if (type === 'select') {
|
|
|
|
input = (
|
2018-05-25 06:36:43 +02:00
|
|
|
<select className="form-field__select" id={name} {...inputProps}>
|
2018-03-26 23:32:43 +02:00
|
|
|
{children}
|
|
|
|
</select>
|
|
|
|
);
|
|
|
|
} else if (type === 'markdown') {
|
2018-06-25 22:42:49 +02:00
|
|
|
const handleEvents = {
|
2018-07-29 01:48:54 +02:00
|
|
|
contextmenu: openEditorMenu,
|
2018-06-25 22:42:49 +02:00
|
|
|
};
|
2018-07-29 01:48:54 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
input = (
|
2018-06-25 22:42:49 +02:00
|
|
|
<div className="form-field--SimpleMDE" onContextMenu={stopContextMenu}>
|
2018-03-26 23:32:43 +02:00
|
|
|
<SimpleMDE
|
|
|
|
{...inputProps}
|
|
|
|
type="textarea"
|
2018-06-25 22:42:49 +02:00
|
|
|
events={handleEvents}
|
2018-06-10 23:57:46 +02:00
|
|
|
options={{
|
2018-06-13 07:20:53 +02:00
|
|
|
hideIcons: ['heading', 'image', 'fullscreen', 'side-by-side'],
|
|
|
|
previewRender(plainText) {
|
|
|
|
const preview = <MarkdownPreview content={plainText} />;
|
|
|
|
return ReactDOMServer.renderToString(preview);
|
|
|
|
},
|
|
|
|
}}
|
2018-03-26 23:32:43 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2018-04-06 07:15:29 +02:00
|
|
|
} else if (type === 'textarea') {
|
|
|
|
input = <textarea type={type} id={name} {...inputProps} />;
|
2018-08-30 07:11:05 +02:00
|
|
|
// } else if (type === 'checkbox') {
|
|
|
|
// input = <Toggle id={name} {...inputProps} />;
|
2018-03-26 23:32:43 +02:00
|
|
|
} else {
|
|
|
|
input = <input type={type} id={name} {...inputProps} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classnames('form-field', {
|
|
|
|
'form-field--stretch': stretch || type === 'markdown',
|
2018-06-21 04:22:13 +02:00
|
|
|
'form-field--disabled': inputProps.disabled,
|
2018-03-26 23:32:43 +02:00
|
|
|
})}
|
|
|
|
>
|
2018-04-05 23:26:20 +02:00
|
|
|
{(label || errorMessage) && (
|
2018-03-26 23:32:43 +02:00
|
|
|
<label
|
2018-04-05 23:26:20 +02:00
|
|
|
className={classnames('form-field__label', { 'form-field__error': errorMessage })}
|
2018-03-26 23:32:43 +02:00
|
|
|
htmlFor={name}
|
|
|
|
>
|
2018-04-05 23:26:20 +02:00
|
|
|
{!errorMessage && label}
|
|
|
|
{errorMessage}
|
2018-03-26 23:32:43 +02:00
|
|
|
</label>
|
|
|
|
)}
|
2018-03-30 02:29:05 +02:00
|
|
|
<div
|
|
|
|
className={classnames('form-field__input', {
|
|
|
|
'form-field--auto-height': type === 'markdown',
|
|
|
|
})}
|
|
|
|
>
|
2018-03-26 23:32:43 +02:00
|
|
|
{prefix && (
|
2018-06-14 22:10:50 +02:00
|
|
|
<label htmlFor={name} className={classnames('form-field__prefix', affixClass)}>
|
2018-03-26 23:32:43 +02:00
|
|
|
{prefix}
|
|
|
|
</label>
|
|
|
|
)}
|
|
|
|
{input}
|
|
|
|
{postfix && (
|
2018-06-14 22:10:50 +02:00
|
|
|
<label htmlFor={name} className={classnames('form-field__postfix', affixClass)}>
|
2018-03-26 23:32:43 +02:00
|
|
|
{postfix}
|
|
|
|
</label>
|
|
|
|
)}
|
|
|
|
</div>
|
2018-06-13 07:20:53 +02:00
|
|
|
{helper && <div className="form-field__help">{helper}</div>}
|
2018-03-26 23:32:43 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FormField;
|