-
Notifications
You must be signed in to change notification settings - Fork 175
/
AutosizeInput.js
212 lines (199 loc) · 6.41 KB
/
AutosizeInput.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const sizerStyle = {
position: 'absolute',
top: 0,
left: 0,
visibility: 'hidden',
height: 0,
overflow: 'scroll',
whiteSpace: 'pre',
};
const INPUT_PROPS_BLACKLIST = [
'extraWidth',
'injectStyles',
'inputClassName',
'inputRef',
'inputStyle',
'minWidth',
'onAutosize',
'placeholderIsMinWidth',
];
const cleanInputProps = (inputProps) => {
INPUT_PROPS_BLACKLIST.forEach(field => delete inputProps[field]);
return inputProps;
};
const copyStyles = (styles, node) => {
node.style.fontSize = styles.fontSize;
node.style.fontFamily = styles.fontFamily;
node.style.fontWeight = styles.fontWeight;
node.style.fontStyle = styles.fontStyle;
node.style.letterSpacing = styles.letterSpacing;
node.style.textTransform = styles.textTransform;
};
const isIE = (typeof window !== 'undefined' && window.navigator) ? /MSIE |Trident\/|Edge\//.test(window.navigator.userAgent) : false;
const generateId = () => {
// we only need an auto-generated ID for stylesheet injection, which is only
// used for IE. so if the browser is not IE, this should return undefined.
return isIE ? '_' + Math.random().toString(36).substr(2, 12) : undefined;
};
class AutosizeInput extends Component {
static getDerivedStateFromProps (props, state) {
const { id } = props;
return id !== state.prevId ? { inputId: id || generateId(), prevId: id } : null;
}
constructor (props) {
super(props);
this.state = {
inputWidth: props.minWidth,
inputId: props.id || generateId(),
prevId: props.id,
};
}
componentDidMount () {
this.mounted = true;
this.copyInputStyles();
this.updateInputWidth();
}
componentDidUpdate (prevProps, prevState) {
if (prevState.inputWidth !== this.state.inputWidth) {
if (typeof this.props.onAutosize === 'function') {
this.props.onAutosize(this.state.inputWidth);
}
}
this.updateInputWidth();
}
componentWillUnmount () {
this.mounted = false;
}
inputRef = (el) => {
this.input = el;
if (typeof this.props.inputRef === 'function') {
this.props.inputRef(el);
}
};
placeHolderSizerRef = (el) => {
this.placeHolderSizer = el;
};
sizerRef = (el) => {
this.sizer = el;
};
copyInputStyles () {
if (!this.mounted || !window.getComputedStyle) {
return;
}
const inputStyles = this.input && window.getComputedStyle(this.input);
if (!inputStyles) {
return;
}
copyStyles(inputStyles, this.sizer);
if (this.placeHolderSizer) {
copyStyles(inputStyles, this.placeHolderSizer);
}
}
updateInputWidth () {
if (!this.mounted || !this.sizer || typeof this.sizer.scrollWidth === 'undefined') {
return;
}
let newInputWidth;
if (this.props.placeholder && (!this.props.value || (this.props.value && this.props.placeholderIsMinWidth))) {
newInputWidth = Math.max(this.sizer.scrollWidth, this.placeHolderSizer.scrollWidth) + 2;
} else {
newInputWidth = this.sizer.scrollWidth + 2;
}
// add extraWidth to the detected width. for number types, this defaults to 16 to allow for the stepper UI
const extraWidth = (this.props.type === 'number' && this.props.extraWidth === undefined)
? 16 : parseInt(this.props.extraWidth) || 0;
newInputWidth += extraWidth;
if (newInputWidth < this.props.minWidth) {
newInputWidth = this.props.minWidth;
}
if (newInputWidth !== this.state.inputWidth) {
this.setState({
inputWidth: newInputWidth,
});
}
}
getInput () {
return this.input;
}
focus () {
this.input.focus();
}
blur () {
this.input.blur();
}
select () {
this.input.select();
}
renderStyles () {
// this method injects styles to hide IE's clear indicator, which messes
// with input size detection. the stylesheet is only injected when the
// browser is IE, and can also be disabled by the `injectStyles` prop.
const { injectStyles } = this.props;
return isIE && injectStyles ? (
<style dangerouslySetInnerHTML={{
__html: `input#${this.state.inputId}::-ms-clear {display: none;}`,
}} />
) : null;
}
render () {
const sizerValue = [this.props.defaultValue, this.props.value, ''].reduce((previousValue, currentValue) => {
if (previousValue !== null && previousValue !== undefined) {
return previousValue;
}
return currentValue;
});
const wrapperStyle = { ...this.props.style };
if (!wrapperStyle.display) wrapperStyle.display = 'inline-block';
const inputStyle = {
boxSizing: 'content-box',
width: `${this.state.inputWidth}px`,
...this.props.inputStyle,
};
const { ...inputProps } = this.props;
cleanInputProps(inputProps);
inputProps.className = this.props.inputClassName;
inputProps.id = this.state.inputId;
inputProps.style = inputStyle;
return (
<div className={this.props.className} style={wrapperStyle}>
{this.renderStyles()}
<input {...inputProps} ref={this.inputRef} />
<div ref={this.sizerRef} style={sizerStyle}>{sizerValue}</div>
{this.props.placeholder
? <div ref={this.placeHolderSizerRef} style={sizerStyle}>{this.props.placeholder}</div>
: null
}
</div>
);
}
}
AutosizeInput.propTypes = {
className: PropTypes.string, // className for the outer element
defaultValue: PropTypes.any, // default field value
extraWidth: PropTypes.oneOfType([ // additional width for input element
PropTypes.number,
PropTypes.string,
]),
id: PropTypes.string, // id to use for the input, can be set for consistent snapshots
injectStyles: PropTypes.bool, // inject the custom stylesheet to hide clear UI, defaults to true
inputClassName: PropTypes.string, // className for the input element
inputRef: PropTypes.func, // ref callback for the input element
inputStyle: PropTypes.object, // css styles for the input element
minWidth: PropTypes.oneOfType([ // minimum width for input element
PropTypes.number,
PropTypes.string,
]),
onAutosize: PropTypes.func, // onAutosize handler: function(newWidth) {}
onChange: PropTypes.func, // onChange handler: function(event) {}
placeholder: PropTypes.string, // placeholder text
placeholderIsMinWidth: PropTypes.bool, // don't collapse size to less than the placeholder
style: PropTypes.object, // css styles for the outer element
value: PropTypes.any, // field value
};
AutosizeInput.defaultProps = {
minWidth: 1,
injectStyles: true,
};
export default AutosizeInput;