accounts-frontend/packages/app/components/ui/form/TextArea.tsx

88 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

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';
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';
interface OwnProps {
2020-05-24 04:38:24 +05:30
placeholder?: string | MessageDescriptor;
label?: string | MessageDescriptor;
skin: Skin;
color: Color;
}
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,
};
2020-05-24 04:38:24 +05:30
elRef = React.createRef<HTMLTextAreaElement>();
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;
2020-05-24 04:38:24 +05:30
const props = omit(
{
type: 'text',
...this.props,
},
['label', 'placeholder', 'error', 'skin', 'color'],
);
2020-05-24 04:38:24 +05:30
if (labelText) {
if (!props.id) {
props.id = uniqueId('textarea');
}
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
}
2020-05-24 04:38:24 +05:30
getValue() {
const { current: el } = this.elRef;
2020-05-24 04:38:24 +05:30
return el && el.value;
}
2020-05-24 04:38:24 +05:30
focus() {
const { current: el } = this.elRef;
2020-05-24 04:38:24 +05:30
if (!el) {
return;
}
2020-05-24 04:38:24 +05:30
el.focus();
setTimeout(el.focus.bind(el), 10);
2016-05-22 22:55:38 +05:30
}
}