2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2022-01-24 17:07:09 +01:00
|
|
|
import 'easymde/dist/easymde.min.css';
|
|
|
|
import { FF_MAX_CHARS_DEFAULT } from 'constants/form-field';
|
2019-05-08 03:42:56 +02:00
|
|
|
import React from 'react';
|
2022-01-24 17:07:09 +01:00
|
|
|
import type { ElementRef, Node } from 'react';
|
2019-04-03 07:56:58 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
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,
|
2019-09-30 23:16:46 +02:00
|
|
|
charCount?: number,
|
2022-01-24 17:07:09 +01:00
|
|
|
children?: React$Node,
|
|
|
|
defaultValue?: string | number,
|
|
|
|
disabled?: boolean,
|
|
|
|
error?: string | boolean,
|
|
|
|
helper?: string | React$Node,
|
|
|
|
inputButton?: React$Node,
|
|
|
|
label?: string | Node,
|
|
|
|
labelOnLeft: boolean,
|
2020-03-25 04:15:05 +01:00
|
|
|
max?: number,
|
2022-01-24 17:07:09 +01:00
|
|
|
min?: number,
|
|
|
|
name: string,
|
|
|
|
placeholder?: string | number,
|
|
|
|
postfix?: string,
|
|
|
|
prefix?: string,
|
|
|
|
range?: number,
|
|
|
|
readOnly?: boolean,
|
|
|
|
stretch?: boolean,
|
|
|
|
textAreaMaxLength?: number,
|
|
|
|
type?: string,
|
2021-04-23 21:59:48 +02:00
|
|
|
value?: string | number,
|
2022-01-24 17:07:09 +01:00
|
|
|
onChange?: (any) => any,
|
|
|
|
render?: () => React$Node,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export class FormField extends React.PureComponent<Props> {
|
2022-01-24 17:07:09 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { autoFocus } = this.props;
|
|
|
|
const input = this.input.current;
|
|
|
|
|
2022-01-24 17:07:09 +01:00
|
|
|
if (input && autoFocus) input.focus();
|
2018-09-22 03:20:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
render() {
|
|
|
|
const {
|
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,
|
2019-09-30 23:16:46 +02:00
|
|
|
charCount,
|
2022-01-24 17:07:09 +01:00
|
|
|
children,
|
|
|
|
error,
|
|
|
|
helper,
|
|
|
|
inputButton,
|
|
|
|
label,
|
|
|
|
labelOnLeft,
|
|
|
|
name,
|
|
|
|
postfix,
|
|
|
|
prefix,
|
|
|
|
stretch,
|
|
|
|
textAreaMaxLength,
|
|
|
|
type,
|
|
|
|
render,
|
2018-03-26 23:32:43 +02:00
|
|
|
...inputProps
|
|
|
|
} = this.props;
|
2022-01-24 17:07:09 +01:00
|
|
|
|
2018-04-05 23:26:20 +02:00
|
|
|
const errorMessage = typeof error === 'object' ? error.message : error;
|
2022-01-24 17:07:09 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-01-24 17:07:09 +01:00
|
|
|
const inputSimple = (type: string) => (
|
|
|
|
<>
|
|
|
|
<input id={name} type={type} {...inputProps} />
|
|
|
|
<label htmlFor={name}>{label}</label>
|
|
|
|
</>
|
|
|
|
);
|
2018-07-29 01:48:54 +02:00
|
|
|
|
2022-01-24 17:07:09 +01: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>
|
|
|
|
);
|
2021-02-26 08:38:33 +01:00
|
|
|
|
2022-01-24 17:07:09 +01:00
|
|
|
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 'textarea':
|
|
|
|
return (
|
2019-02-13 17:27:20 +01:00
|
|
|
<fieldset-section>
|
2022-11-04 13:42:36 +01:00
|
|
|
{label && (
|
2022-01-24 17:07:09 +01:00
|
|
|
<div className="form-field__two-column">
|
2020-05-21 09:25:37 +02:00
|
|
|
<label htmlFor={name}>{label}</label>
|
|
|
|
</div>
|
2022-01-24 17:07:09 +01:00
|
|
|
)}
|
2022-11-04 13:42:36 +01:00
|
|
|
<textarea
|
|
|
|
type={type}
|
|
|
|
id={name}
|
|
|
|
maxLength={textAreaMaxLength || FF_MAX_CHARS_DEFAULT}
|
|
|
|
ref={this.input}
|
|
|
|
{...inputProps}
|
|
|
|
/>
|
|
|
|
<div className="form-field__textarea-info">{countInfo}</div>
|
2019-02-13 17:27:20 +01:00
|
|
|
</fieldset-section>
|
2022-01-24 17:07:09 +01:00
|
|
|
);
|
|
|
|
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
|
|
|
|
2022-01-24 17:07:09 +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}>
|
2020-04-13 21:16:07 +02:00
|
|
|
{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>
|
2022-01-24 17:07:09 +01:00
|
|
|
);
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2022-01-24 17:07:09 +01:00
|
|
|
};
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
return (
|
2022-01-24 17:07:09 +01:00
|
|
|
<>
|
|
|
|
{type && input()}
|
2019-03-21 16:22:23 +01:00
|
|
|
{helper && <div className="form-field__help">{helper}</div>}
|
2022-01-24 17:07:09 +01:00
|
|
|
</>
|
2018-03-26 23:32:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FormField;
|