// @flow import type { ElementRef } from 'react'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; import SimpleMDE from 'react-simplemde-editor'; import MarkdownPreview from 'component/common/markdown-preview-internal'; import { openEditorMenu, stopContextMenu } from 'util/context-menu'; import 'easymde/dist/easymde.min.css'; 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, affixClass?: string, // class applied to prefix/postfix label firstInList?: boolean, // at the top of a list, no padding top autoFocus?: boolean, labelOnLeft: boolean, inputProps?: { disabled?: boolean, }, inputButton?: React$Node, blockWrap: boolean, }; export class FormField extends React.PureComponent { static defaultProps = { labelOnLeft: false, blockWrap: true, }; input: { current: ElementRef }; constructor(props: Props) { super(props); this.input = React.createRef(); } componentDidMount() { const { autoFocus } = this.props; const input = this.input.current; if (input && autoFocus) { input.focus(); } } render() { const { render, label, prefix, postfix, error, helper, name, type, children, stretch, affixClass, autoFocus, inputButton, labelOnLeft, blockWrap, ...inputProps } = this.props; const errorMessage = typeof error === 'object' ? error.message : error; const Wrapper = blockWrap ? ({ children: innerChildren }) => {innerChildren} : ({ children: innerChildren }) => {innerChildren}; let input; if (type) { if (type === 'radio') { input = ( ); } else if (type === 'checkbox') { // web components treat props weird // we need to fully remove it for proper component:attribute css styling // $FlowFixMe const elementProps = inputProps.disabled ? { disabled: true } : {}; input = ( ); } else if (type === 'select') { input = ( {label && } ); } else if (type === 'markdown') { const handleEvents = { contextmenu: openEditorMenu, }; input = (
; return ReactDOMServer.renderToString(preview); }, }} />
); } else if (type === 'textarea') { input = (