forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-field.js
More file actions
308 lines (252 loc) · 7.7 KB
/
text-field.js
File metadata and controls
308 lines (252 loc) · 7.7 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var babelHelpers = require('./babel-helpers.js');
/**
* MUI React TextInput Component
* @module react/text-field
*/
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TextField = undefined;
var _react = require('react');
var _react2 = babelHelpers.interopRequireDefault(_react);
var _jqLite = require('../js/lib/jqLite');
var jqLite = babelHelpers.interopRequireWildcard(_jqLite);
var _util = require('../js/lib/util');
var util = babelHelpers.interopRequireWildcard(_util);
var _helpers = require('./_helpers');
/**
* Input constructor
* @class
*/
var Input = function (_React$Component) {
babelHelpers.inherits(Input, _React$Component);
function Input(props) {
babelHelpers.classCallCheck(this, Input);
var _this = babelHelpers.possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props));
var value = props.value;
var innerValue = value || props.defaultValue;
if (innerValue === undefined) innerValue = '';
_this.state = {
innerValue: innerValue,
isTouched: false,
isPristine: true
};
// warn if value defined but onChange is not
if (value !== undefined && !props.onChange) {
util.raiseError(_helpers.controlledMessage, true);
}
var cb = util.callback;
_this.onBlurCB = cb(_this, 'onBlur');
_this.onChangeCB = cb(_this, 'onChange');
return _this;
}
babelHelpers.createClass(Input, [{
key: 'componentDidMount',
value: function componentDidMount() {
// disable MUI js
this.refs.inputEl._muiTextfield = true;
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
// update innerValue when new value is received to handle programmatic
// changes to input box
if ('value' in nextProps) this.setState({ innerValue: nextProps.value });
}
}, {
key: 'onBlur',
value: function onBlur(ev) {
// ignore if event is a window blur
if (document.activeElement !== this.refs.inputEl) {
this.setState({ isTouched: true });
}
// execute callback
var fn = this.props.onBlur;
fn && fn(ev);
}
}, {
key: 'onChange',
value: function onChange(ev) {
this.setState({
innerValue: ev.target.value,
isPristine: false
});
// execute callback
var fn = this.props.onChange;
fn && fn(ev);
}
}, {
key: 'triggerFocus',
value: function triggerFocus() {
// hack to enable IE10 pointer-events shim
this.refs.inputEl.focus();
}
}, {
key: 'render',
value: function render() {
var cls = {},
isNotEmpty = Boolean(this.state.innerValue.toString()),
inputEl = void 0;
var _props = this.props,
hint = _props.hint,
invalid = _props.invalid,
rows = _props.rows,
type = _props.type,
reactProps = babelHelpers.objectWithoutProperties(_props, ['hint', 'invalid', 'rows', 'type']);
cls['mui--is-touched'] = this.state.isTouched;
cls['mui--is-untouched'] = !this.state.isTouched;
cls['mui--is-pristine'] = this.state.isPristine;
cls['mui--is-dirty'] = !this.state.isPristine;
cls['mui--is-empty'] = !isNotEmpty;
cls['mui--is-not-empty'] = isNotEmpty;
cls['mui--is-invalid'] = invalid;
cls = util.classNames(cls);
if (type === 'textarea') {
inputEl = _react2.default.createElement('textarea', babelHelpers.extends({}, reactProps, {
ref: 'inputEl',
className: cls,
rows: rows,
placeholder: hint,
onBlur: this.onBlurCB,
onChange: this.onChangeCB
}));
} else {
inputEl = _react2.default.createElement('input', babelHelpers.extends({}, reactProps, {
ref: 'inputEl',
className: cls,
type: type,
placeholder: this.props.hint,
onBlur: this.onBlurCB,
onChange: this.onChangeCB
}));
}
return inputEl;
}
}]);
return Input;
}(_react2.default.Component);
/**
* Label constructor
* @class
*/
Input.defaultProps = {
hint: null,
invalid: false,
rows: 2
};
var Label = function (_React$Component2) {
babelHelpers.inherits(Label, _React$Component2);
function Label() {
var _ref;
var _temp, _this2, _ret;
babelHelpers.classCallCheck(this, Label);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this2 = babelHelpers.possibleConstructorReturn(this, (_ref = Label.__proto__ || Object.getPrototypeOf(Label)).call.apply(_ref, [this].concat(args))), _this2), _this2.state = {
style: {}
}, _temp), babelHelpers.possibleConstructorReturn(_this2, _ret);
}
babelHelpers.createClass(Label, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this3 = this;
this.styleTimer = setTimeout(function () {
var s = '.15s ease-out';
var style = void 0;
style = {
transition: s,
WebkitTransition: s,
MozTransition: s,
OTransition: s,
msTransform: s
};
_this3.setState({ style: style });
}, 150);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
// clear timer
clearTimeout(this.styleTimer);
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
'label',
{
style: this.state.style,
onClick: this.props.onClick
},
this.props.text
);
}
}]);
return Label;
}(_react2.default.Component);
/**
* TextField constructor
* @class
*/
Label.defaultProps = {
text: '',
onClick: null
};
var TextField = function (_React$Component3) {
babelHelpers.inherits(TextField, _React$Component3);
function TextField(props) {
babelHelpers.classCallCheck(this, TextField);
var _this4 = babelHelpers.possibleConstructorReturn(this, (TextField.__proto__ || Object.getPrototypeOf(TextField)).call(this, props));
_this4.onClickCB = util.callback(_this4, 'onClick');
return _this4;
}
babelHelpers.createClass(TextField, [{
key: 'onClick',
value: function onClick(ev) {
// pointer-events shim
if (util.supportsPointerEvents() === false) {
ev.target.style.cursor = 'text';
this.refs.inputEl.triggerFocus();
}
}
}, {
key: 'render',
value: function render() {
var cls = {},
labelEl = void 0;
var _props2 = this.props,
children = _props2.children,
className = _props2.className,
style = _props2.style,
label = _props2.label,
floatingLabel = _props2.floatingLabel,
other = babelHelpers.objectWithoutProperties(_props2, ['children', 'className', 'style', 'label', 'floatingLabel']);
var type = jqLite.type(label);
if (type === 'string' && label.length || type === 'object') {
labelEl = _react2.default.createElement(Label, { text: label, onClick: this.onClickCB });
}
cls['mui-textfield'] = true;
cls['mui-textfield--float-label'] = floatingLabel;
cls = util.classNames(cls);
return _react2.default.createElement(
'div',
{
className: cls + ' ' + className,
style: style
},
_react2.default.createElement(Input, babelHelpers.extends({ ref: 'inputEl' }, other)),
labelEl
);
}
}]);
return TextField;
}(_react2.default.Component);
/** Define module API */
TextField.defaultProps = {
className: '',
label: null,
floatingLabel: false
};
exports.TextField = TextField;