2018-03-01 17:53:29 +00:00
|
|
|
import React, { HTMLProps } from 'react';
|
2018-05-13 19:24:50 +00:00
|
|
|
import classnames from 'classnames';
|
2018-03-01 17:53:29 +00:00
|
|
|
import './Input.scss';
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
hasBlurred: boolean;
|
2018-05-13 19:24:50 +00:00
|
|
|
/**
|
|
|
|
* @description when the input has not had any values inputted yet
|
|
|
|
* e.g. "Pristine" condition
|
|
|
|
*/
|
|
|
|
isStateless: boolean;
|
2018-03-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:24:50 +00:00
|
|
|
interface OwnProps extends HTMLProps<HTMLTextAreaElement> {
|
|
|
|
isValid: boolean;
|
|
|
|
showValidAsPlain?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TextArea extends React.Component<OwnProps, State> {
|
2018-03-01 17:53:29 +00:00
|
|
|
public state: State = {
|
2018-05-13 19:24:50 +00:00
|
|
|
hasBlurred: false,
|
|
|
|
isStateless: true
|
2018-03-01 17:53:29 +00:00
|
|
|
};
|
2018-05-13 19:24:50 +00:00
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
public render() {
|
2018-05-13 19:24:50 +00:00
|
|
|
const { showValidAsPlain, isValid, ...htmlProps } = this.props;
|
|
|
|
const classname = classnames(
|
|
|
|
this.props.className,
|
|
|
|
'input-group-input',
|
2018-05-14 01:39:54 +00:00
|
|
|
this.state.isStateless ? '' : isValid ? (showValidAsPlain ? '' : '') : `invalid`,
|
2018-05-13 19:24:50 +00:00
|
|
|
this.state.hasBlurred && 'has-blurred'
|
|
|
|
);
|
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
return (
|
|
|
|
<textarea
|
2018-05-13 19:24:50 +00:00
|
|
|
{...htmlProps}
|
2018-03-01 17:53:29 +00:00
|
|
|
onBlur={e => {
|
|
|
|
this.setState({ hasBlurred: true });
|
|
|
|
if (this.props && this.props.onBlur) {
|
|
|
|
this.props.onBlur(e);
|
|
|
|
}
|
|
|
|
}}
|
2018-05-13 19:24:50 +00:00
|
|
|
onChange={this.handleOnChange}
|
|
|
|
className={classname}
|
2018-03-01 17:53:29 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-05-13 19:24:50 +00:00
|
|
|
|
|
|
|
private handleOnChange = (args: React.FormEvent<HTMLTextAreaElement>) => {
|
|
|
|
if (this.state.isStateless) {
|
|
|
|
this.setState({ isStateless: false });
|
|
|
|
}
|
|
|
|
if (this.props.onChange) {
|
|
|
|
this.props.onChange(args);
|
|
|
|
}
|
|
|
|
};
|
2018-03-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default TextArea;
|