lbry-desktop/ui/component/common/form-components/form-field.jsx

198 lines
5.6 KiB
React
Raw Normal View History

2018-03-26 14:32:43 -07:00
// @flow
2020-02-21 17:05:10 -05:00
import type { ElementRef, Node } from 'react';
2019-05-07 21:42:56 -04:00
import React from 'react';
import ReactDOMServer from 'react-dom/server';
2019-05-07 21:42:56 -04:00
import SimpleMDE from 'react-simplemde-editor';
import MarkdownPreview from 'component/common/markdown-preview-internal';
import { openEditorMenu, stopContextMenu } from 'util/context-menu';
import { MAX_CHARACTERS_IN_COMMENT as defaultTextAreaLimit } from 'constants/comments';
import 'easymde/dist/easymde.min.css';
2019-04-03 00:56:58 -05:00
2018-03-26 14:32:43 -07:00
type Props = {
name: string,
2020-02-21 17:05:10 -05:00
label?: string | Node,
2019-04-24 10:02:08 -04:00
render?: () => React$Node,
2018-03-26 14:32:43 -07:00
prefix?: string,
postfix?: string,
error?: string | boolean,
2019-04-24 10:02:08 -04:00
helper?: string | React$Node,
2018-03-26 14:32:43 -07:00
type?: string,
onChange?: any => any,
defaultValue?: string | number,
placeholder?: string | number,
2019-04-24 10:02:08 -04:00
children?: React$Node,
2018-03-26 14:32:43 -07:00
stretch?: boolean,
2018-06-14 16:10:50 -04:00
affixClass?: string, // class applied to prefix/postfix label
2018-09-21 19:20:58 -06:00
autoFocus?: boolean,
2019-02-13 12:27:20 -04:00
labelOnLeft: boolean,
inputProps?: {
2018-09-04 13:18:11 -04:00
disabled?: boolean,
},
2019-04-24 10:02:08 -04:00
inputButton?: React$Node,
2019-02-18 12:24:56 -05:00
blockWrap: boolean,
charCount?: number,
textAreaMaxLength?: number,
range?: number,
min?: number,
max?: number,
2018-03-26 14:32:43 -07:00
};
export class FormField extends React.PureComponent<Props> {
2019-02-13 12:27:20 -04:00
static defaultProps = {
labelOnLeft: false,
2019-02-18 12:24:56 -05:00
blockWrap: true,
2019-02-13 12:27:20 -04:00
};
2019-04-24 10:02:08 -04:00
input: { current: ElementRef<any> };
2019-02-20 00:20:29 -05:00
2019-02-18 12:24:56 -05:00
constructor(props: Props) {
2018-09-21 19:20:58 -06:00
super(props);
this.input = React.createRef();
}
componentDidMount() {
const { autoFocus } = this.props;
const input = this.input.current;
if (input && autoFocus) {
input.focus();
}
}
2018-03-26 14:32:43 -07:00
render() {
const {
render,
label,
prefix,
postfix,
error,
helper,
name,
type,
children,
stretch,
2018-06-14 16:10:50 -04:00
affixClass,
2018-09-21 19:20:58 -06:00
autoFocus,
2019-02-13 12:27:20 -04:00
inputButton,
labelOnLeft,
2019-02-18 12:24:56 -05:00
blockWrap,
charCount,
textAreaMaxLength = defaultTextAreaLimit,
2018-03-26 14:32:43 -07:00
...inputProps
} = this.props;
const errorMessage = typeof error === 'object' ? error.message : error;
2019-02-18 12:24:56 -05:00
const Wrapper = blockWrap
2019-11-22 16:13:00 -05:00
? ({ children: innerChildren }) => <fieldset-section class="radio">{innerChildren}</fieldset-section>
: ({ children: innerChildren }) => <span className="radio">{innerChildren}</span>;
2019-02-18 12:24:56 -05:00
2018-03-26 14:32:43 -07:00
let input;
if (type) {
2019-02-13 12:27:20 -04:00
if (type === 'radio') {
input = (
2019-02-18 12:24:56 -05:00
<Wrapper>
2019-11-22 16:13:00 -05:00
<input id={name} type="radio" {...inputProps} />
<label htmlFor={name}>{label}</label>
2019-02-18 12:24:56 -05:00
</Wrapper>
2019-02-13 12:27:20 -04:00
);
} else if (type === 'checkbox') {
input = (
2019-11-22 16:13:00 -05:00
<div className="checkbox">
<input id={name} type="checkbox" {...inputProps} />
<label htmlFor={name}>{label}</label>
</div>
2019-02-13 12:27:20 -04:00
);
} else if (type === 'range') {
input = (
<div>
<input id={name} type="range" {...inputProps} />
<label htmlFor={name}>{label}</label>
</div>
);
2019-02-13 12:27:20 -04:00
} else if (type === 'select') {
2018-03-26 14:32:43 -07:00
input = (
2019-02-13 12:27:20 -04:00
<fieldset-section>
{(label || errorMessage) && (
<label htmlFor={name}>{errorMessage ? <span className="error__text">{errorMessage}</span> : label}</label>
)}
2019-02-13 12:27:20 -04:00
<select id={name} {...inputProps}>
{children}
</select>
2019-02-13 12:27:20 -04:00
</fieldset-section>
2018-03-26 14:32:43 -07:00
);
} else if (type === 'markdown') {
2018-06-25 14:42:49 -06:00
const handleEvents = {
2018-07-28 17:48:54 -06:00
contextmenu: openEditorMenu,
2018-06-25 14:42:49 -06:00
};
2018-07-28 17:48:54 -06:00
2018-03-26 14:32:43 -07:00
input = (
2019-03-21 11:22:23 -04:00
<div className="form-field--SimpleMDE" onContextMenu={stopContextMenu}>
2019-02-13 12:27:20 -04:00
<fieldset-section>
<label htmlFor={name}>{label}</label>
2019-05-07 21:42:56 -04:00
<SimpleMDE
{...inputProps}
id={name}
type="textarea"
events={handleEvents}
options={{
2020-01-24 15:32:22 -05:00
spellChecker: true,
2019-05-07 21:42:56 -04:00
hideIcons: ['heading', 'image', 'fullscreen', 'side-by-side'],
previewRender(plainText) {
const preview = <MarkdownPreview content={plainText} />;
return ReactDOMServer.renderToString(preview);
},
}}
/>
2019-02-13 12:27:20 -04:00
</fieldset-section>
2018-03-26 14:32:43 -07:00
</div>
);
2018-04-06 01:15:29 -04:00
} else if (type === 'textarea') {
const hasCharCount = charCount !== undefined && charCount >= 0;
const countInfo = hasCharCount && (
2019-10-01 00:08:16 +02:00
<span className="comment__char-count">{`${charCount || '0'}/${textAreaMaxLength}`}</span>
);
2019-02-13 12:27:20 -04:00
input = (
<fieldset-section>
2019-06-26 19:59:27 -04:00
<label htmlFor={name}>{label}</label>
2019-11-04 07:50:51 -05:00
<textarea type={type} id={name} maxLength={textAreaMaxLength} ref={this.input} {...inputProps} />
{countInfo}
2019-02-13 12:27:20 -04:00
</fieldset-section>
);
2018-03-26 14:32:43 -07:00
} else {
2019-02-13 12:27:20 -04:00
const inputElement = <input type={type} id={name} {...inputProps} ref={this.input} />;
const inner = inputButton ? (
<input-submit>
{inputElement}
{inputButton}
</input-submit>
) : (
inputElement
);
input = (
<React.Fragment>
<fieldset-section>
2020-02-06 13:49:05 -05:00
{(label || errorMessage) && (
2019-11-22 16:13:00 -05:00
<label htmlFor={name}>
{errorMessage ? <span className="error__text">{errorMessage}</span> : label}
2019-11-22 16:13:00 -05:00
</label>
)}
2019-09-26 12:07:11 -04:00
{prefix && <label htmlFor={name}>{prefix}</label>}
2019-02-13 12:27:20 -04:00
{inner}
</fieldset-section>
</React.Fragment>
);
2018-03-26 14:32:43 -07:00
}
}
return (
2019-02-13 12:27:20 -04:00
<React.Fragment>
{input}
2019-03-21 11:22:23 -04:00
{helper && <div className="form-field__help">{helper}</div>}
2019-02-13 12:27:20 -04:00
</React.Fragment>
2018-03-26 14:32:43 -07:00
);
}
}
export default FormField;