-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathtextarea-editor.js
66 lines (60 loc) · 1.52 KB
/
textarea-editor.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
/* eslint no-return-assign: 0 */
import React, { Component } from 'react';
import cs from 'classnames';
import PropTypes from 'prop-types';
class TextAreaEditor extends Component {
constructor(props) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
componentDidMount() {
const { defaultValue, didMount, autoSelectText } = this.props;
this.text.value = defaultValue;
this.text.focus();
if (autoSelectText) this.text.select();
if (didMount) didMount();
}
getValue() {
return this.text.value;
}
handleKeyDown(e) {
if (e.keyCode === 13 && !e.shiftKey) return;
if (this.props.onKeyDown) {
this.props.onKeyDown(e);
}
}
render() {
const { defaultValue, didMount, className, autoSelectText, ...rest } = this.props;
const editorClass = cs('form-control editor edit-textarea', className);
return (
<textarea
ref={ node => this.text = node }
type="textarea"
className={ editorClass }
{ ...rest }
onKeyDown={ this.handleKeyDown }
/>
);
}
}
TextAreaEditor.propTypes = {
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]),
defaultValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
onKeyDown: PropTypes.func,
autoSelectText: PropTypes.bool,
didMount: PropTypes.func
};
TextAreaEditor.defaultProps = {
className: '',
defaultValue: '',
autoSelectText: false,
onKeyDown: undefined,
didMount: undefined
};
export default TextAreaEditor;