This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
TextArea.js
95 lines (86 loc) · 2.66 KB
/
TextArea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react';
import {findDOMNode} from 'react-dom';
import {GenericScrollBox, ScrollAxes} from 'react-scroll-box';
import {GenericInput} from './GenericInput';
const {string, bool, any} = React.PropTypes;
export class TextArea extends React.Component {
static propTypes = {
value: string,
defaultValue: string,
disabled: bool,
className: string,
placeholder: any,
fitLineLength: bool,
propagateWheelScroll: bool
};
static defaultProps = {
disabled: false,
fitLineLength: false,
propagateWheelScroll: true
};
constructor(props) {
super(props);
this.state = {
focused: false,
value: props.defaultValue
};
}
onFocus = e => this.setState({focused: true});
onBlur = e => this.setState({focused: false});
onChange = e => {
if (this.props.value == null) {
this.setState({value: e.target.value});
}
};
componentDidMount () {
findDOMNode(this).addEventListener('focus', e => findDOMNode(this.refs.textarea).focus());
}
render () {
const {value, propagateWheelScroll, defaultValue, className, style, fitLineLength, placeholder, onViewportScroll, ...rest} = this.props;
let classNames = ['text-input--text-area'];
if (className) {
classNames = classNames.concat(className);
}
if (this.state.focused) {
classNames.push('text-input--focus');
}
if (fitLineLength) {
classNames.push('text-input--fit-line-length');
}
let textValue = value;
if (value == null) {
textValue = this.state.value;
}
let listeners = {};
for (let key in rest) {
if (/^on[A-Z]/.test(key)) {
listeners[key] = rest[key];
delete rest[key];
}
}
return (
<GenericInput {...listeners}
{...rest}
style={style}
className={classNames}
value={textValue}
placeholder={placeholder}>
<GenericScrollBox {...rest}
captureKeyboard={false}
propagateWheelScroll={propagateWheelScroll}
outset={false}
onViewportScroll={onViewportScroll}
className="text-input__area scroll-box--wrapped">
<textarea {...rest}
ref="textarea"
className="text-input__control scroll-box__viewport"
value={value}
defaultValue={defaultValue}
onChange={this.onChange}
onFocus={this.onFocus}
onBlur={this.onBlur}/>
</GenericScrollBox>
</GenericInput>
);
}
}