/
index.js
68 lines (68 loc) · 2.51 KB
/
index.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const cx = require("classnames");
class Textarea extends React.Component {
constructor() {
super(...arguments);
this.state = {
autoresize: this.props.autoresize != null,
focus: false,
keyCode: null,
};
this.toggleFocus = () => {
this.setState({ focus: !this.state.focus });
};
this.handleKeyDown = (ev) => {
this.setState({ keyCode: ev.keyCode });
if (this.props.onKeyDown) {
this.props.onKeyDown(ev);
}
};
this.handleKeyUp = (ev) => {
if (this.props.onKeyUp) {
this.props.onKeyUp(ev);
}
};
this.handleMouseUp = (ev) => {
if (this.props.onMouseUp) {
this.props.onMouseUp(ev);
}
};
this.handleChange = (ev) => {
if (this.props.onChange) {
this.props.onChange(ev.currentTarget.value, ev);
}
};
this.handleInput = (ev) => {
if (this.props.onInput) {
this.props.onInput(ev.currentTarget.value, ev, this.state.keyCode);
}
};
}
componentDidMount() {
if (this.state.autoresize)
this.adjustHeight();
}
shouldComponentUpdate(nextProps, nextState) {
let propsValueChange = this.props.value !== nextProps.value;
let stateFocusChange = this.state.focus !== nextState.focus;
return propsValueChange || stateFocusChange;
}
componentDidUpdate(prevProps) {
if (this.props.value !== prevProps.value) {
if (this.state.autoresize)
this.adjustHeight();
}
}
render() {
return (React.createElement("textarea", { className: cx("hire-forms-textarea", { focus: this.state.focus }, this.props.className), onBlur: this.toggleFocus, onChange: this.handleChange, onFocus: this.toggleFocus, onInput: this.handleInput, onKeyDown: this.handleKeyDown, onKeyUp: this.handleKeyUp, onMouseUp: this.handleMouseUp, placeholder: this.props.placeholder, ref: (node) => { this.node = node; }, style: this.props.style, value: this.props.value }));
}
adjustHeight() {
this.node.style.height = "auto";
this.node.style.height = (this.node.scrollHeight + 6 > 32) ?
(this.node.scrollHeight + 6) + "px" :
"32px";
}
}
exports.default = Textarea;