88 lines
2.4 KiB
TypeScript
Raw Normal View History

import React from 'react';
2019-12-07 13:28:52 +02:00
import { MessageDescriptor } from 'react-intl';
2020-05-24 02:08:24 +03:00
import TextareaAutosize, { TextareaAutosizeProps } from 'react-textarea-autosize';
2019-12-07 21:43:08 +02:00
import clsx from 'clsx';
import { uniqueId, omit } from 'app/functions';
import { SKIN_DARK, COLOR_GREEN, Skin, Color } from 'app/components/ui';
2016-05-22 20:25:38 +03:00
import styles from './form.scss';
import FormInputComponent from './FormInputComponent';
interface OwnProps {
2020-05-24 02:08:24 +03:00
placeholder?: string | MessageDescriptor;
label?: string | MessageDescriptor;
skin: Skin;
color: Color;
}
2020-05-24 02:08:24 +03:00
export default class TextArea extends FormInputComponent<OwnProps & Omit<TextareaAutosizeProps, keyof OwnProps>> {
static defaultProps = {
color: COLOR_GREEN,
skin: SKIN_DARK,
};
2020-05-24 02:08:24 +03:00
elRef = React.createRef<HTMLTextAreaElement>();
2020-05-24 02:08:24 +03:00
render() {
const { color, skin, label: labelText, placeholder: placeholderText } = this.props;
let label: React.ReactElement | undefined;
let placeholder: string | undefined;
2020-05-24 02:08:24 +03:00
const props = omit(
{
type: 'text',
...this.props,
},
['label', 'placeholder', 'error', 'skin', 'color'],
);
2020-05-24 02:08:24 +03:00
if (labelText) {
if (!props.id) {
props.id = uniqueId('textarea');
}
2020-05-24 02:08:24 +03:00
label = (
<label className={styles.textFieldLabel} htmlFor={props.id}>
{this.formatMessage(labelText)}
</label>
);
}
if (placeholderText) {
placeholder = this.formatMessage(placeholderText);
}
2016-05-22 20:25:38 +03:00
2020-05-24 02:08:24 +03:00
return (
<div className={styles.formRow}>
{label}
<div className={styles.textAreaContainer}>
<TextareaAutosize
inputRef={this.elRef}
className={clsx(styles.textArea, styles[`${skin}TextField`], styles[`${color}TextField`])}
placeholder={placeholder}
{...props}
/>
</div>
{this.renderError()}
</div>
);
2019-12-07 13:28:52 +02:00
}
2020-05-24 02:08:24 +03:00
getValue() {
const { current: el } = this.elRef;
2020-05-24 02:08:24 +03:00
return el && el.value;
}
2020-05-24 02:08:24 +03:00
focus() {
const { current: el } = this.elRef;
2020-05-24 02:08:24 +03:00
if (!el) {
return;
}
2020-05-24 02:08:24 +03:00
el.focus();
setTimeout(el.focus.bind(el), 10);
2016-05-22 20:25:38 +03:00
}
}