forked from react-toolbox/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.js
More file actions
232 lines (207 loc) · 7.32 KB
/
Input.js
File metadata and controls
232 lines (207 loc) · 7.32 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { themr } from 'react-css-themr';
import { INPUT } from '../identifiers';
import InjectedFontIcon from '../font_icon/FontIcon';
const factory = (FontIcon) => {
class Input extends React.Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
defaultValue: PropTypes.string,
disabled: PropTypes.bool,
error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
]),
floating: PropTypes.bool,
hint: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
]),
icon: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
]),
maxLength: PropTypes.number,
multiline: PropTypes.bool,
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onKeyPress: PropTypes.func,
required: PropTypes.bool,
role: PropTypes.string,
rows: PropTypes.number,
theme: PropTypes.shape({
bar: PropTypes.string,
counter: PropTypes.string,
disabled: PropTypes.string,
error: PropTypes.string,
errored: PropTypes.string,
hidden: PropTypes.string,
hint: PropTypes.string,
icon: PropTypes.string,
input: PropTypes.string,
inputElement: PropTypes.string,
required: PropTypes.string,
withIcon: PropTypes.string,
}),
type: PropTypes.string,
value: PropTypes.oneOfType([
PropTypes.number,
PropTypes.object,
PropTypes.string,
]),
};
static defaultProps = {
className: '',
hint: '',
disabled: false,
floating: true,
multiline: false,
required: false,
role: 'input',
type: 'text',
};
componentDidMount() {
if (this.props.multiline) {
window.addEventListener('resize', this.handleAutoresize);
this.handleAutoresize();
}
}
componentWillReceiveProps(nextProps) {
if (!this.props.multiline && nextProps.multiline) {
window.addEventListener('resize', this.handleAutoresize);
} else if (this.props.multiline && !nextProps.multiline) {
window.removeEventListener('resize', this.handleAutoresize);
}
}
componentDidUpdate() {
// resize the textarea, if nessesary
if (this.props.multiline) this.handleAutoresize();
}
componentWillUnmount() {
if (this.props.multiline) window.removeEventListener('resize', this.handleAutoresize);
}
handleChange = (event) => {
const { onChange, multiline, maxLength } = this.props;
const valueFromEvent = event.target.value;
// Trim value to maxLength if that exists (only on multiline inputs).
// Note that this is still required even tho we have the onKeyPress filter
// because the user could paste smt in the textarea.
const haveToTrim = (multiline && maxLength && event.target.value.length > maxLength);
const value = haveToTrim ? valueFromEvent.substr(0, maxLength) : valueFromEvent;
// propagate to to store and therefore to the input
if (onChange) onChange(value, event);
};
handleAutoresize = () => {
const element = this.inputNode;
const rows = this.props.rows;
if (typeof rows === 'number' && !isNaN(rows)) {
element.style.height = null;
} else {
// compute the height difference between inner height and outer height
const style = getComputedStyle(element, null);
const heightOffset = style.boxSizing === 'content-box'
? -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom))
: parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
// resize the input to its content size
element.style.height = 'auto';
element.style.height = `${element.scrollHeight + heightOffset}px`;
}
}
handleKeyPress = (event) => {
// prevent insertion of more characters if we're a multiline input
// and maxLength exists
const { multiline, maxLength, onKeyPress } = this.props;
if (multiline && maxLength) {
// check if smt is selected, in which case the newly added charcter would
// replace the selected characters, so the length of value doesn't actually
// increase.
const isReplacing = event.target.selectionEnd - event.target.selectionStart;
const value = event.target.value;
if (!isReplacing && value.length === maxLength) {
event.preventDefault();
event.stopPropagation();
return undefined;
}
}
if (onKeyPress) onKeyPress(event);
return undefined;
};
blur() {
this.inputNode.blur();
}
focus() {
this.inputNode.focus();
}
valuePresent = value => (
value !== null
&& value !== undefined
&& value !== ''
&& !(typeof value === 'number' && isNaN(value))
)
render() {
const { children, defaultValue, disabled, error, floating, hint, icon,
name, label: labelText, maxLength, multiline, required, role,
theme, type, value, onKeyPress, rows = 1, ...others } = this.props;
const length = maxLength && value ? value.length : 0;
const labelClassName = classnames(theme.label, { [theme.fixed]: !floating });
const className = classnames(theme.input, {
[theme.disabled]: disabled,
[theme.errored]: error,
[theme.hidden]: type === 'hidden',
[theme.withIcon]: icon,
}, this.props.className);
const valuePresent = this.valuePresent(value) || this.valuePresent(defaultValue);
const inputElementProps = {
...others,
className: classnames(theme.inputElement, { [theme.filled]: valuePresent }),
onChange: this.handleChange,
ref: (node) => { this.inputNode = node; },
role,
name,
defaultValue,
disabled,
required,
type,
value,
};
if (!multiline) {
inputElementProps.maxLength = maxLength;
inputElementProps.onKeyPress = onKeyPress;
} else {
inputElementProps.rows = rows;
inputElementProps.onKeyPress = this.handleKeyPress;
}
return (
<div data-react-toolbox="input" className={className}>
{React.createElement(multiline ? 'textarea' : 'input', inputElementProps)}
{icon ? <FontIcon className={theme.icon} value={icon} /> : null}
<span className={theme.bar} />
{labelText
? <label className={labelClassName}>
{labelText}
{required ? <span className={theme.required}> * </span> : null}
</label>
: null}
{hint ? <span hidden={labelText} className={theme.hint}>{hint}</span> : null}
{error ? <span className={theme.error}>{error}</span> : null}
{maxLength ? <span className={theme.counter}>{length}/{maxLength}</span> : null}
{children}
</div>
);
}
}
return Input;
};
const Input = factory(InjectedFontIcon);
export default themr(INPUT)(Input);
export { factory as inputFactory };
export { Input };