-
Notifications
You must be signed in to change notification settings - Fork 342
/
TextAreaDynamicRows.tsx
51 lines (42 loc) · 1.25 KB
/
TextAreaDynamicRows.tsx
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
import * as React from 'react'
import * as constants from 'src/sidebar-overlay/comment-box/constants'
interface ChildProps {
// children: ReactChild
}
type Props = ChildProps
export default class TextAreaDynamicRows extends React.Component<
Props,
{ rows: number }
> {
private ref: HTMLTextAreaElement
constructor(props) {
super(props)
this.state = { rows: constants.NUM_DEFAULT_ROWS }
}
// -- Methods primarily to do with keeping the selection state in sync --
componentDidMount() {
this.ref.addEventListener('onChange', this.handleRows)
}
componentWillUnmount() {
this.ref.addEventListener('onChange', this.handleRows)
}
private handleRows() {
const rows =
this.ref.value.length === 0
? constants.NUM_DEFAULT_ROWS
: Math.max(this.state.rows, constants.NUM_MIN_ROWS)
if (rows !== this.state.rows) {
this.setState({ rows })
}
}
private onChange = () => {
this.handleRows()
}
private updateRef = e => (this.ref = e)
render() {
return React.cloneElement(this.props.children[0], {
updateRef: this.updateRef,
rows: this.state.rows,
})
}
}