This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
FormTextarea.js
130 lines (114 loc) 路 3.19 KB
/
FormTextarea.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React from 'react';
import classNames from 'classnames';
import autosize from 'autosize';
import CollapsableFormField from './CollapsableFormField';
import FormInputHelp from './FormInputHelp';
import FormInputErrors from './FormInputErrors';
import FormInputLabel from './FormInputLabel';
class FormTextarea extends React.Component {
static propTypes = {
label: React.PropTypes.string.isRequired,
rows: React.PropTypes.number.isRequired,
name: React.PropTypes.string,
value: React.PropTypes.string,
placeholder: React.PropTypes.string,
help: React.PropTypes.string,
spellCheck: React.PropTypes.bool,
onChange: React.PropTypes.func,
collapsable: React.PropTypes.bool,
resizable: React.PropTypes.string,
autoresize: React.PropTypes.bool,
className: React.PropTypes.string,
errors: React.PropTypes.array
};
static defaultProps = {
spellCheck: false,
collapsable: false,
autoresize: false
};
state = {
collapsed: this.props.collapsable && this._hasEmptyValue() && !this._hasErrors() && this._hasEmptyValue()
};
componentDidMount() {
if (this.props.autoresize) {
autosize(this._textarea);
}
}
componentWillUnmount() {
if (this.props.autoresize) {
autosize.destroy(this._textarea);
}
}
render() {
if (this.props.collapsable) {
return (
<CollapsableFormField label={this.props.label} collapsed={this.state.collapsed}>
{this._renderTextArea()}
{this._renderErrors()}
{this._renderHelp()}
</CollapsableFormField>
);
} else {
return (
<div className="mb2">
<FormInputLabel label={this.props.label} errors={this._hasErrors()}>
{this._renderTextArea()}
</FormInputLabel>
{this._renderErrors()}
{this._renderHelp()}
</div>
);
}
}
// In some cases the initial height can be incorrect and you need to explicit tell
// us to autosize the textarea for you. See:
// http://www.jacklmoore.com/autosize/#faq-hidden
updateAutoresize() {
if (this.props.autoresize) {
autosize.update(this._textarea);
}
}
getValue() {
return this._textarea.value;
}
_hasErrors() {
return this.props.errors && this.props.errors.length > 0;
}
_hasEmptyValue() {
return !this.props.value || this.props.value.length == 0;
}
_renderErrors() {
if (this._hasErrors()) {
return (
<FormInputErrors errors={this.props.errors}/>
);
}
}
_renderHelp() {
if (this.props.help) {
return (
<FormInputHelp html={this.props.help}/>
);
}
}
_renderTextArea() {
var style = {}
if (this.props.resizable) {
style.resize = this.props.resizable;
}
return (
<textarea
className={classNames("form-control", this.props.className)}
name={this.props.name}
type="text"
defaultValue={this.props.value}
placeholder={this.props.placeholder}
spellCheck={this.props.spellCheck}
onChange={this.props.onChange}
rows={this.props.rows}
style={style}
ref={(c) => this._textarea = c} />
);
}
}
export default FormTextarea;