2018-03-26 00:46:45 +05:30
|
|
|
// @flow
|
2018-05-05 12:13:43 +05:30
|
|
|
import type { Skin, Color } from 'components/ui';
|
|
|
|
import type { MessageDescriptor } from 'react-intl';
|
2017-08-23 00:19:50 +05:30
|
|
|
import React from 'react';
|
2018-03-26 00:46:45 +05:30
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
2016-05-22 22:55:38 +05:30
|
|
|
import classNames from 'classnames';
|
2016-07-29 22:44:52 +05:30
|
|
|
import { uniqueId, omit } from 'functions';
|
2018-03-26 00:46:45 +05:30
|
|
|
import { SKIN_DARK, COLOR_GREEN } from 'components/ui';
|
2016-05-22 22:55:38 +05:30
|
|
|
|
|
|
|
import styles from './form.scss';
|
|
|
|
import FormInputComponent from './FormInputComponent';
|
|
|
|
|
2018-03-26 00:46:45 +05:30
|
|
|
type TextareaAutosizeProps = {
|
|
|
|
onHeightChange?: (number, TextareaAutosizeProps) => void,
|
|
|
|
useCacheForDOMMeasurements?: bool,
|
|
|
|
minRows?: number,
|
|
|
|
maxRows?: number,
|
2018-05-05 12:13:43 +05:30
|
|
|
inputRef?: (?HTMLTextAreaElement) => void,
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
|
|
|
|
export default class TextArea extends FormInputComponent<{
|
|
|
|
placeholder?: string | MessageDescriptor,
|
|
|
|
label?: string | MessageDescriptor,
|
|
|
|
error?: string,
|
|
|
|
skin: Skin,
|
|
|
|
color: Color,
|
2018-05-05 12:13:43 +05:30
|
|
|
} & TextareaAutosizeProps> {
|
2016-05-30 10:10:59 +05:30
|
|
|
static defaultProps = {
|
|
|
|
color: COLOR_GREEN,
|
2018-03-26 00:46:45 +05:30
|
|
|
skin: SKIN_DARK,
|
2016-05-22 22:55:38 +05:30
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
elRef = React.createRef<HTMLTextAreaElement>();
|
|
|
|
|
2016-05-22 22:55:38 +05:30
|
|
|
render() {
|
2016-05-30 10:10:59 +05:30
|
|
|
const { color, skin } = this.props;
|
|
|
|
let { label } = this.props;
|
2016-05-22 22:55:38 +05:30
|
|
|
|
2016-07-29 22:44:52 +05:30
|
|
|
const props = omit({
|
2016-05-22 22:55:38 +05:30
|
|
|
type: 'text',
|
2018-03-26 00:46:45 +05:30
|
|
|
...this.props,
|
|
|
|
}, ['label', 'error', 'skin', 'color']);
|
2016-05-22 22:55:38 +05:30
|
|
|
|
|
|
|
if (label) {
|
|
|
|
if (!props.id) {
|
|
|
|
props.id = uniqueId('textarea');
|
|
|
|
}
|
|
|
|
|
|
|
|
label = this.formatMessage(label);
|
|
|
|
|
|
|
|
label = (
|
|
|
|
<label className={styles.textFieldLabel} htmlFor={props.id}>
|
|
|
|
{label}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
props.placeholder = this.formatMessage(props.placeholder);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.formRow}>
|
|
|
|
{label}
|
|
|
|
<div className={styles.textAreaContainer}>
|
2019-06-30 19:02:50 +05:30
|
|
|
<TextareaAutosize inputRef={this.elRef}
|
2016-05-22 22:55:38 +05:30
|
|
|
className={classNames(
|
|
|
|
styles.textArea,
|
|
|
|
styles[`${skin}TextField`],
|
|
|
|
styles[`${color}TextField`]
|
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{this.renderError()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue() {
|
2019-06-30 19:02:50 +05:30
|
|
|
const { current: el } = this.elRef;
|
|
|
|
|
|
|
|
return el && el.value;
|
2016-05-22 22:55:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
2019-06-30 19:02:50 +05:30
|
|
|
const { current: el } = this.elRef;
|
2018-05-05 14:31:25 +05:30
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.focus();
|
|
|
|
setTimeout(el.focus.bind(el), 10);
|
2016-05-22 22:55:38 +05:30
|
|
|
}
|
|
|
|
}
|