lbry-desktop/src/renderer/component/common/form-components/form-field.jsx

117 lines
3 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as React from 'react';
import ReactDOMServer from 'react-dom/server';
2018-03-26 23:32:43 +02:00
import classnames from 'classnames';
import MarkdownPreview from 'component/common/markdown-preview';
2018-03-26 23:32:43 +02:00
import SimpleMDE from 'react-simplemde-editor';
import "simplemde/dist/simplemde.min.css";
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,
};
export class FormField extends React.PureComponent<Props> {
render() {
const {
render,
label,
prefix,
postfix,
error,
helper,
name,
type,
children,
stretch,
...inputProps
} = this.props;
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') {
input = (
<div className="form-field--SimpleMDE">
<SimpleMDE
{...inputProps}
type="textarea"
options={{
hideIcons: ['heading', 'image', 'fullscreen', 'side-by-side'],
previewRender(plainText) {
2018-06-11 00:32:30 +02:00
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-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',
})}
>
{(label || errorMessage) && (
2018-03-26 23:32:43 +02:00
<label
className={classnames('form-field__label', { 'form-field__error': errorMessage })}
2018-03-26 23:32:43 +02:00
htmlFor={name}
>
{!errorMessage && label}
{errorMessage}
2018-03-26 23:32:43 +02:00
</label>
)}
<div
className={classnames('form-field__input', {
'form-field--auto-height': type === 'markdown',
})}
>
2018-03-26 23:32:43 +02:00
{prefix && (
<label htmlFor={name} className="form-field__prefix">
{prefix}
</label>
)}
{input}
{postfix && (
<label htmlFor={name} className="form-field__postfix">
{postfix}
</label>
)}
</div>
{helper && (
<label htmlFor={name} className="form-field__help">
{helper}
</label>
)}
</div>
);
}
}
export default FormField;