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

427 lines
14 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import 'easymde/dist/easymde.min.css';
import { FF_MAX_CHARS_DEFAULT } from 'constants/form-field';
import { openEditorMenu, stopContextMenu } from 'util/context-menu';
import { lazyImport } from 'util/lazyImport';
import Button from 'component/button';
import MarkdownPreview from 'component/common/markdown-preview';
2019-05-08 03:42:56 +02:00
import React from 'react';
import ReactDOMServer from 'react-dom/server';
2019-05-08 03:42:56 +02:00
import SimpleMDE from 'react-simplemde-editor';
import type { ElementRef, Node } from 'react';
import Drawer from '@mui/material/Drawer';
import CommentSelectors from 'component/commentCreate/comment-selectors';
2019-04-03 07:56:58 +02:00
// prettier-ignore
const TextareaWithSuggestions = lazyImport(() => import('component/textareaWithSuggestions' /* webpackChunkName: "suggestions" */));
2018-03-26 23:32:43 +02:00
type Props = {
uri?: string,
2018-06-14 22:10:50 +02:00
affixClass?: string, // class applied to prefix/postfix label
2018-09-22 03:20:58 +02:00
autoFocus?: boolean,
2019-02-18 18:24:56 +01:00
blockWrap: boolean,
charCount?: number,
children?: React$Node,
defaultValue?: string | number,
disabled?: boolean,
error?: string | boolean,
helper?: string | React$Node,
hideSuggestions?: boolean,
inputButton?: React$Node,
isLivestream?: boolean,
label?: string | Node,
labelOnLeft: boolean,
max?: number,
min?: number,
name: string,
placeholder?: string | number,
postfix?: string,
prefix?: string,
quickActionLabel?: string,
range?: number,
readOnly?: boolean,
stretch?: boolean,
textAreaMaxLength?: number,
type?: string,
2021-04-23 21:59:48 +02:00
value?: string | number,
slimInput?: boolean,
2022-02-07 20:30:42 +01:00
slimInputButtonRef?: any,
commentSelectorsProps?: any,
2022-02-07 20:30:42 +01:00
showSelectors?: any,
submitButtonRef?: any,
tipModalOpen?: boolean,
2022-02-07 14:37:19 +01:00
noticeLabel?: any,
onChange?: (any) => any,
2022-02-07 20:30:42 +01:00
setShowSelectors?: ({ tab?: string, open: boolean }) => void,
quickActionHandler?: (any) => any,
render?: () => React$Node,
handleTip?: (isLBC: boolean) => any,
handleSubmit?: () => any,
2018-03-26 23:32:43 +02:00
};
type State = {
drawerOpen: boolean,
};
export class FormField extends React.PureComponent<Props, State> {
static defaultProps = { labelOnLeft: false, blockWrap: true };
2019-02-13 17:27:20 +01:00
2019-04-24 16:02:08 +02:00
input: { current: ElementRef<any> };
2019-02-20 06:20:29 +01:00
2019-02-18 18:24:56 +01:00
constructor(props: Props) {
2018-09-22 03:20:58 +02:00
super(props);
this.input = React.createRef();
this.state = {
drawerOpen: false,
};
2018-09-22 03:20:58 +02:00
}
componentDidMount() {
const { autoFocus } = this.props;
const input = this.input.current;
if (input && autoFocus) input.focus();
2018-09-22 03:20:58 +02:00
}
componentDidUpdate() {
const { showSelectors, slimInput } = this.props;
const input = this.input.current;
// Opened selectors (emoji/sticker) -> blur input and hide keyboard
2022-02-07 20:30:42 +01:00
if (slimInput && showSelectors && showSelectors.open && input) input.blur();
}
2018-03-26 23:32:43 +02:00
render() {
const {
uri,
2018-06-14 22:10:50 +02:00
affixClass,
2018-09-22 03:20:58 +02:00
autoFocus,
2019-02-18 18:24:56 +01:00
blockWrap,
charCount,
children,
error,
helper,
hideSuggestions,
inputButton,
isLivestream,
label,
labelOnLeft,
name,
postfix,
prefix,
quickActionLabel,
stretch,
textAreaMaxLength,
type,
slimInput,
2022-02-07 20:30:42 +01:00
slimInputButtonRef,
commentSelectorsProps,
showSelectors,
submitButtonRef,
tipModalOpen,
2022-02-07 14:37:19 +01:00
noticeLabel,
quickActionHandler,
setShowSelectors,
render,
handleTip,
handleSubmit,
2018-03-26 23:32:43 +02:00
...inputProps
} = this.props;
const errorMessage = typeof error === 'object' ? error.message : error;
// Ideally, the character count should (and can) be appended to the
// SimpleMDE's "options::status" bar. However, I couldn't figure out how
// to pass the current value to it's callback, nor query the current
// text length from the callback. So, we'll use our own widget.
const hasCharCount = charCount !== undefined && charCount >= 0;
const countInfo = hasCharCount && textAreaMaxLength !== undefined && (
<span className="comment__char-count-mde">{`${charCount || '0'}/${textAreaMaxLength}`}</span>
);
2019-02-18 18:24:56 +01:00
const Wrapper = blockWrap
2019-11-22 22:13:00 +01:00
? ({ children: innerChildren }) => <fieldset-section class="radio">{innerChildren}</fieldset-section>
: ({ children: innerChildren }) => <span className="radio">{innerChildren}</span>;
2019-02-18 18:24:56 +01:00
const quickAction =
quickActionLabel && quickActionHandler ? (
<div className="form-field__quick-action">
<Button button="link" onClick={quickActionHandler} label={quickActionLabel} />
</div>
) : null;
const inputSimple = (type: string) => (
<>
<input id={name} type={type} {...inputProps} />
<label htmlFor={name}>{label}</label>
</>
);
2018-07-29 01:48:54 +02:00
const inputSelect = (selectClass: string) => (
<fieldset-section class={selectClass}>
{(label || errorMessage) && (
<label htmlFor={name}>{errorMessage ? <span className="error__text">{errorMessage}</span> : label}</label>
)}
<select id={name} {...inputProps}>
{children}
</select>
</fieldset-section>
);
const input = () => {
switch (type) {
case 'radio':
return <Wrapper>{inputSimple('radio')}</Wrapper>;
case 'checkbox':
return <div className="checkbox">{inputSimple('checkbox')}</div>;
case 'range':
return <div>{inputSimple('range')}</div>;
case 'select':
return inputSelect('');
case 'select-tiny':
return inputSelect('select--slim');
case 'markdown':
const handleEvents = { contextmenu: openEditorMenu };
const getInstance = (editor) => {
// SimpleMDE max char check
editor.codemirror.on('beforeChange', (instance, changes) => {
if (textAreaMaxLength && changes.update) {
var str = changes.text.join('\n');
var delta = str.length - (instance.indexFromPos(changes.to) - instance.indexFromPos(changes.from));
if (delta <= 0) return;
delta = instance.getValue().length + delta - textAreaMaxLength;
if (delta > 0) {
str = str.substr(0, str.length - delta);
changes.update(changes.from, changes.to, str.split('\n'));
}
}
});
// "Create Link (Ctrl-K)": highlight URL instead of label:
editor.codemirror.on('changes', (instance, changes) => {
try {
// Grab the last change from the buffered list. I assume the
// buffered one ('changes', instead of 'change') is more efficient,
// and that "Create Link" will always end up last in the list.
const lastChange = changes[changes.length - 1];
if (lastChange.origin === '+input') {
// https://github.com/Ionaru/easy-markdown-editor/blob/8fa54c496f98621d5f45f57577ce630bee8c41ee/src/js/easymde.js#L765
const EASYMDE_URL_PLACEHOLDER = '(https://)';
// The URL placeholder is always placed last, so just look at the
// last text in the array to also cover the multi-line case:
const urlLineText = lastChange.text[lastChange.text.length - 1];
if (urlLineText.endsWith(EASYMDE_URL_PLACEHOLDER) && urlLineText !== '[]' + EASYMDE_URL_PLACEHOLDER) {
const from = lastChange.from;
const to = lastChange.to;
const isSelectionMultiline = lastChange.text.length > 1;
const baseIndex = isSelectionMultiline ? 0 : from.ch;
// Everything works fine for the [Ctrl-K] case, but for the
// [Button] case, this handler happens before the original
// code, thus our change got wiped out.
// Add a small delay to handle that case.
setTimeout(() => {
instance.setSelection(
{ line: to.line, ch: baseIndex + urlLineText.lastIndexOf('(') + 1 },
{ line: to.line, ch: baseIndex + urlLineText.lastIndexOf(')') }
);
}, 25);
}
}
} catch (e) {} // Do nothing (revert to original behavior)
});
};
return (
<div className="form-field--SimpleMDE" onContextMenu={stopContextMenu}>
<fieldset-section>
<div className="form-field__two-column">
<div>
<label htmlFor={name}>{label}</label>
</div>
{quickAction}
</div>
<SimpleMDE
{...inputProps}
id={name}
type="textarea"
events={handleEvents}
getMdeInstance={getInstance}
options={{
spellChecker: true,
hideIcons: ['heading', 'image', 'fullscreen', 'side-by-side'],
previewRender(plainText) {
const preview = <MarkdownPreview content={plainText} noDataStore />;
return ReactDOMServer.renderToString(preview);
},
}}
/>
{countInfo}
</fieldset-section>
</div>
);
case 'textarea':
return (
2019-02-13 17:27:20 +01:00
<fieldset-section>
<TextareaWrapper
isDrawerOpen={Boolean(this.state.drawerOpen)}
toggleDrawer={() => this.setState({ drawerOpen: !this.state.drawerOpen })}
2022-02-07 20:30:42 +01:00
closeSelector={
setShowSelectors && showSelectors
? () => setShowSelectors({ tab: showSelectors.tab || undefined, open: false })
: () => {}
}
commentSelectorsProps={commentSelectorsProps}
2022-02-07 20:30:42 +01:00
showSelectors={Boolean(showSelectors && showSelectors.open)}
slimInput={slimInput}
2022-02-07 20:30:42 +01:00
slimInputButtonRef={slimInputButtonRef}
tipModalOpen={tipModalOpen}
>
{(!slimInput || this.state.drawerOpen) && (label || quickAction) && (
<div className="form-field__two-column">
<label htmlFor={name}>{label}</label>
{quickAction}
{countInfo}
</div>
)}
2022-02-07 14:37:19 +01:00
{noticeLabel}
{hideSuggestions ? (
<textarea
type={type}
id={name}
maxLength={textAreaMaxLength || FF_MAX_CHARS_DEFAULT}
ref={this.input}
{...inputProps}
/>
) : (
<React.Suspense fallback={null}>
<TextareaWithSuggestions
uri={uri}
type={type}
id={name}
maxLength={textAreaMaxLength || FF_MAX_CHARS_DEFAULT}
inputRef={this.input}
isLivestream={isLivestream}
2022-02-07 20:30:42 +01:00
toggleSelectors={
setShowSelectors && showSelectors
? () => setShowSelectors({ tab: showSelectors.tab || undefined, open: !showSelectors.open })
: undefined
}
handleTip={handleTip}
handleSubmit={() => {
if (handleSubmit) handleSubmit();
if (slimInput) this.setState({ drawerOpen: false });
}}
claimIsMine={commentSelectorsProps && commentSelectorsProps.claimIsMine}
{...inputProps}
2022-02-07 20:30:42 +01:00
slimInput={slimInput}
handlePreventClick={
!this.state.drawerOpen ? () => this.setState({ drawerOpen: true }) : undefined
}
autoFocus={this.state.drawerOpen}
submitButtonRef={submitButtonRef}
/>
</React.Suspense>
)}
</TextareaWrapper>
</fieldset-section>
);
default:
const inputElement = <input type={type} id={name} {...inputProps} ref={this.input} />;
const inner = inputButton ? (
<input-submit>
{inputElement}
{inputButton}
</input-submit>
) : (
inputElement
);
2019-02-13 17:27:20 +01:00
return (
2019-02-13 17:27:20 +01:00
<fieldset-section>
2020-02-06 19:49:05 +01:00
{(label || errorMessage) && (
2019-11-22 22:13:00 +01:00
<label htmlFor={name}>
{errorMessage ? <span className="error__text">{errorMessage}</span> : label}
2019-11-22 22:13:00 +01:00
</label>
)}
2019-09-26 18:07:11 +02:00
{prefix && <label htmlFor={name}>{prefix}</label>}
2019-02-13 17:27:20 +01:00
{inner}
</fieldset-section>
);
2018-03-26 23:32:43 +02:00
}
};
2018-03-26 23:32:43 +02:00
return (
<>
{type && input()}
2019-03-21 16:22:23 +01:00
{helper && <div className="form-field__help">{helper}</div>}
</>
2018-03-26 23:32:43 +02:00
);
}
}
export default FormField;
type TextareaWrapperProps = {
slimInput?: boolean,
2022-02-07 20:30:42 +01:00
slimInputButtonRef?: any,
children: Node,
isDrawerOpen: boolean,
showSelectors?: boolean,
commentSelectorsProps?: any,
tipModalOpen?: boolean,
toggleDrawer: () => void,
closeSelector?: () => void,
};
function TextareaWrapper(wrapperProps: TextareaWrapperProps) {
const {
children,
slimInput,
2022-02-07 20:30:42 +01:00
slimInputButtonRef,
isDrawerOpen,
commentSelectorsProps,
showSelectors,
tipModalOpen,
toggleDrawer,
closeSelector,
} = wrapperProps;
function handleCloseAll() {
toggleDrawer();
if (closeSelector) closeSelector();
}
return slimInput ? (
!isDrawerOpen ? (
2022-02-07 20:30:42 +01:00
<div ref={slimInputButtonRef} role="button" onClick={toggleDrawer}>
{children}
</div>
) : (
<Drawer
className="comment-create--drawer"
anchor="bottom"
open
onClose={handleCloseAll}
// The Modal tries to enforce focus when open and doesn't allow clicking or changing any
// other input boxes, so in this case it is disabled when trying to type in a custom tip
ModalProps={{ disableEnforceFocus: tipModalOpen }}
>
{children}
{showSelectors && <CommentSelectors closeSelector={closeSelector} {...commentSelectorsProps} />}
</Drawer>
)
) : (
<>{children}</>
);
}