-
Notifications
You must be signed in to change notification settings - Fork 7
/
textarea-autosize.js
175 lines (166 loc) · 4.11 KB
/
textarea-autosize.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require('./textarea-autosize.css')
import React, {createElement as el} from 'react'
import {sans, colors} from './rebass-theme'
const containerStyle =
{ fontFamily: sans,
fontSize: 12,
}
const labelStyle = {}
const labelStyleError =
{ color: colors.error,
}
const areaStyle =
{ fontFamily: sans,
minHeight: '1.5rem',
display: 'block',
width: '100%',
padding: 0,
resize: 'none',
color: 'inherit',
border: 0,
borderBottom: '1px dotted rgba(0, 136, 238, .2)',
borderRadius: 0,
outline: 'none',
overflow: 'hidden',
marginBottom: '0.75rem',
}
class TextareaAutosize extends React.Component {
constructor (props) {
super(props)
this.boundResize = this.resize.bind(this)
this.boundDebounceResize = this.debounceResize.bind(this)
this.boundOnChange = this.onChange.bind(this)
this.boundOnKeyDown = this.onKeyDown.bind(this)
this.state =
{ value: props.defaultValue,
valid: true,
}
}
componentDidMount () {
this.boundDebounceResize()
if (this.props.defaultFocus === true) {
this.refs.textarea.focus()
}
}
componentDidUpdate () {
this.boundDebounceResize()
}
componentWillUnmount () {
if (this.debounce) {
clearTimeout(this.debounce)
}
}
componentWillReceiveProps (props) {
const {defaultValue, validator} = props
let valid = true
if (validator) {
valid = validator(defaultValue)
}
this.setState({value: defaultValue, valid})
}
render () {
const {label, placeholder} = this.props
let {inputMode, autoCapitalize} = this.props
const {value, valid} = this.state
if (label === 'Link') {
inputMode = 'url'
autoCapitalize = 'none'
}
return el('div',
{
className: `TextareaAutosize ${this.props.className}`,
style: containerStyle,
},
el('label',
{style: (valid ? labelStyle : labelStyleError)},
label,
this.renderLink(),
el('textarea', {
ref: 'textarea',
style: areaStyle,
value: value || '',
placeholder,
inputMode,
autoCapitalize,
onChange: this.boundOnChange,
rows: 1,
onKeyDown: this.boundOnKeyDown,
})
)
)
}
renderLink () {
const {label} = this.props
if (label !== 'Link') return
const {value, valid} = this.state
if (!value) return
if (!valid) {
return el('span', {}, ' - must be a valid url (http...)')
}
return el('a'
, { href: value,
target: '_blank',
rel: 'noreferrer noopener',
style:
{ marginLeft: '0.5rem',
textDecoration: 'none',
},
}
, 'open'
)
}
resize () {
const { textarea } = this.refs
textarea.style.height = 'auto'
textarea.style.height = textarea.scrollHeight + 'px'
}
debounceResize () {
if (this.debounce) {
clearTimeout(this.debounce)
}
this.debounce = setTimeout(this.boundResize, 100)
}
onKeyDown (event) {
if (this.props.onKeyDown) {
this.props.onKeyDown(event)
}
if (!this.props.multiline && event.key === 'Enter') {
event.preventDefault()
}
}
onChange (event) {
const {validator, onChange} = this.props
let valid = true
let {value} = event.target
if (!this.props.multiline) {
value = value
.replace(/\r\n/g, ' ')
.replace(/[\r\n]/g, ' ')
}
if (validator) {
valid = validator(value)
}
this.setState({value, valid})
onChange(event)
this.boundResize()
}
}
TextareaAutosize.propTypes = {
className: React.PropTypes.string,
defaultValue: React.PropTypes.string,
defaultFocus: React.PropTypes.bool,
label: React.PropTypes.string,
placeholder: React.PropTypes.string,
inputMode: React.PropTypes.string,
autoCapitalize: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired,
onKeyDown: React.PropTypes.func,
multiline: React.PropTypes.bool,
validator: React.PropTypes.func,
}
TextareaAutosize.defaultProps = {
multiline: false,
inputMode: '',
autoCapitalize: 'sentences',
}
export default React.createFactory(TextareaAutosize)