2017-08-23 00:19:50 +05:30
|
|
|
import React from 'react';
|
2019-12-07 16:58:52 +05:30
|
|
|
import { MessageDescriptor } from 'react-intl';
|
2020-05-24 04:38:24 +05:30
|
|
|
import TextareaAutosize, { TextareaAutosizeProps } from 'react-textarea-autosize';
|
2019-12-08 01:13:08 +05:30
|
|
|
import clsx from 'clsx';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { uniqueId, omit } from 'app/functions';
|
|
|
|
import { SKIN_DARK, COLOR_GREEN, Skin, Color } from 'app/components/ui';
|
2016-05-22 22:55:38 +05:30
|
|
|
|
|
|
|
import styles from './form.scss';
|
|
|
|
import FormInputComponent from './FormInputComponent';
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface OwnProps {
|
2020-05-24 04:38:24 +05:30
|
|
|
placeholder?: string | MessageDescriptor;
|
|
|
|
label?: string | MessageDescriptor;
|
|
|
|
skin: Skin;
|
|
|
|
color: Color;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export default class TextArea extends FormInputComponent<OwnProps & Omit<TextareaAutosizeProps, keyof OwnProps>> {
|
|
|
|
static defaultProps = {
|
|
|
|
color: COLOR_GREEN,
|
|
|
|
skin: SKIN_DARK,
|
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
elRef = React.createRef<HTMLTextAreaElement>();
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
const { color, skin, label: labelText, placeholder: placeholderText } = this.props;
|
|
|
|
let label: React.ReactElement | undefined;
|
|
|
|
let placeholder: string | undefined;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const props = omit(
|
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
...this.props,
|
|
|
|
},
|
|
|
|
['label', 'placeholder', 'error', 'skin', 'color'],
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (labelText) {
|
|
|
|
if (!props.id) {
|
|
|
|
props.id = uniqueId('textarea');
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
label = (
|
|
|
|
<label className={styles.textFieldLabel} htmlFor={props.id}>
|
|
|
|
{this.formatMessage(labelText)}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placeholderText) {
|
|
|
|
placeholder = this.formatMessage(placeholderText);
|
|
|
|
}
|
2016-05-22 22:55:38 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
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 16:58:52 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
getValue() {
|
|
|
|
const { current: el } = this.elRef;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return el && el.value;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
focus() {
|
|
|
|
const { current: el } = this.elRef;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
el.focus();
|
|
|
|
setTimeout(el.focus.bind(el), 10);
|
2016-05-22 22:55:38 +05:30
|
|
|
}
|
|
|
|
}
|