UNPKG

11.9 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
10
11var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
12
13var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
14
15var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
16
17var _inherits2 = require('babel-runtime/helpers/inherits');
18
19var _inherits3 = _interopRequireDefault(_inherits2);
20
21var _class, _temp2;
22
23var _react = require('react');
24
25var _react2 = _interopRequireDefault(_react);
26
27var _propTypes = require('prop-types');
28
29var _propTypes2 = _interopRequireDefault(_propTypes);
30
31var _classnames = require('classnames');
32
33var _classnames2 = _interopRequireDefault(_classnames);
34
35var _reactLifecyclesCompat = require('react-lifecycles-compat');
36
37var _configProvider = require('../config-provider');
38
39var _configProvider2 = _interopRequireDefault(_configProvider);
40
41var _util = require('../util');
42
43var _zhCn = require('../locale/zh-cn');
44
45var _zhCn2 = _interopRequireDefault(_zhCn);
46
47function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
48
49var Base = (_temp2 = _class = function (_React$Component) {
50 (0, _inherits3.default)(Base, _React$Component);
51
52 function Base() {
53 var _temp, _this, _ret;
54
55 (0, _classCallCheck3.default)(this, Base);
56
57 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
58 args[_key] = arguments[_key];
59 }
60
61 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleCompositionStart = function (e) {
62 _this.setState({
63 composition: true
64 });
65 _this.props.onCompositionStart(e);
66 }, _this.handleCompositionEnd = function (e) {
67 _this.setState({
68 composition: false
69 });
70 _this.props.onCompositionEnd(e);
71
72 var value = e.target.value;
73 _this.props.onChange(value, e);
74 }, _this.saveRef = function (input) {
75 _this.inputRef = input;
76 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
77 }
78
79 Base.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
80 if ('value' in nextProps && nextProps.value !== prevState.value && !prevState.composition) {
81 var value = nextProps.value;
82 return {
83 value: value === undefined || value === null ? '' : value
84 };
85 }
86
87 return null;
88 };
89
90 Base.prototype.ieHack = function ieHack(value) {
91 return value;
92 };
93
94 Base.prototype.onChange = function onChange(e) {
95 if ('stopPropagation' in e) {
96 e.stopPropagation();
97 } else if ('cancelBubble' in e) {
98 e.cancelBubble();
99 }
100
101 var value = e.target.value;
102
103 if (this.props.trim) {
104 value = value.trim();
105 }
106
107 value = this.ieHack(value);
108
109 // not controlled
110 if (!('value' in this.props) || this.state.composition) {
111 this.setState({
112 value: value
113 });
114 }
115
116 if (this.state.composition) {
117 return;
118 }
119
120 // Number('') = 0
121 if (value && this.props.htmlType === 'number') {
122 value = Number(value);
123 }
124
125 this.props.onChange(value, e);
126 };
127
128 Base.prototype.onKeyDown = function onKeyDown(e) {
129 var value = e.target.value;
130 var maxLength = this.props.maxLength;
131
132 var len = maxLength > 0 && value ? this.getValueLength(value) : 0;
133 var opts = {};
134
135 // has enable trim and has input whitespace
136 if (this.props.trim && e.keyCode === 32) {
137 opts.beTrimed = true;
138 }
139
140 // has defined maxLength and has over max length and has not input backspace and delete
141 if (maxLength > 0 && (len > maxLength + 1 || (len === maxLength || len === maxLength + 1) && e.keyCode !== 8 && e.keyCode !== 46)) {
142 opts.overMaxLength = true;
143 }
144
145 this.props.onKeyDown(e, opts);
146 };
147
148 Base.prototype.onFocus = function onFocus(e) {
149 this.setState({
150 focus: true
151 });
152 this.props.onFocus(e);
153 };
154
155 Base.prototype.onBlur = function onBlur(e) {
156 this.setState({
157 focus: false
158 });
159 this.props.onBlur(e);
160 };
161
162 Base.prototype.renderLength = function renderLength() {
163 var _classNames;
164
165 var _props = this.props,
166 maxLength = _props.maxLength,
167 showLimitHint = _props.showLimitHint,
168 prefix = _props.prefix,
169 rtl = _props.rtl;
170
171 var len = maxLength > 0 && this.state.value ? this.getValueLength(this.state.value) : 0;
172
173 var classesLenWrap = (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'input-len'] = true, _classNames[prefix + 'error'] = len > maxLength, _classNames));
174
175 var content = rtl ? maxLength + '/' + len : len + '/' + maxLength;
176
177 return maxLength && showLimitHint ? _react2.default.createElement(
178 'span',
179 { className: classesLenWrap },
180 content
181 ) : null;
182 };
183
184 Base.prototype.renderControl = function renderControl() {
185 var _this2 = this;
186
187 var lenWrap = this.renderLength();
188
189 return lenWrap ? _react2.default.createElement(
190 'span',
191 { onClick: function onClick() {
192 return _this2.focus();
193 }, className: this.props.prefix + 'input-control' },
194 lenWrap
195 ) : null;
196 };
197
198 Base.prototype.getClass = function getClass() {
199 var _classNames2;
200
201 var _props2 = this.props,
202 disabled = _props2.disabled,
203 state = _props2.state,
204 prefix = _props2.prefix;
205
206
207 return (0, _classnames2.default)((_classNames2 = {}, _classNames2[prefix + 'input'] = true, _classNames2[prefix + 'disabled'] = !!disabled, _classNames2[prefix + 'error'] = state === 'error', _classNames2[prefix + 'warning'] = state === 'warning', _classNames2[prefix + 'focus'] = this.state.focus, _classNames2));
208 };
209
210 Base.prototype.getProps = function getProps() {
211 var _props3 = this.props,
212 placeholder = _props3.placeholder,
213 inputStyle = _props3.inputStyle,
214 disabled = _props3.disabled,
215 readOnly = _props3.readOnly,
216 cutString = _props3.cutString,
217 maxLength = _props3.maxLength,
218 name = _props3.name,
219 onCompositionStart = _props3.onCompositionStart,
220 onCompositionEnd = _props3.onCompositionEnd;
221
222 var props = {
223 style: inputStyle,
224 placeholder: placeholder,
225 disabled: disabled,
226 readOnly: readOnly,
227 name: name,
228 maxLength: cutString ? maxLength : undefined,
229 value: this.state.value,
230 onChange: this.onChange.bind(this),
231 onBlur: this.onBlur.bind(this),
232 onFocus: this.onFocus.bind(this),
233 onCompositionStart: onCompositionStart,
234 onCompositionEnd: onCompositionEnd
235 };
236
237 // fix accessibility:auto process status of aria disabled
238 if (disabled) {
239 props['aria-disabled'] = disabled;
240 }
241
242 return props;
243 };
244
245 Base.prototype.getInputNode = function getInputNode() {
246 return this.inputRef;
247 };
248
249 Base.prototype.focus = function focus(start, end) {
250 this.inputRef.focus();
251 if (typeof start === 'number') {
252 this.inputRef.selectionStart = start;
253 }
254 if (typeof end === 'number') {
255 this.inputRef.selectionEnd = end;
256 }
257 };
258
259 return Base;
260}(_react2.default.Component), _class.propTypes = (0, _extends3.default)({}, _configProvider2.default.propTypes, {
261 /**
262 * 当前值
263 */
264 value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
265 /**
266 * 初始化值
267 */
268 defaultValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
269 /**
270 * 发生改变的时候触发的回调
271 * @param {String} value 数据
272 * @param {Event} e DOM事件对象
273 */
274 onChange: _propTypes2.default.func,
275 /**
276 * 键盘按下的时候触发的回调
277 * @param {Event} e DOM事件对象
278 * @param {Object} opts 可扩展的附加信息:<br> - opts.overMaxLength: {Boolean} 已超出最大长度<br> - opts.beTrimed: {Boolean} 输入的空格被清理
279 */
280 onKeyDown: _propTypes2.default.func,
281 /**
282 * 禁用状态
283 */
284 disabled: _propTypes2.default.bool,
285 /**
286 * 最大长度
287 */
288 maxLength: _propTypes2.default.number,
289 /**
290 * 是否展现最大长度样式(旧版本为 hasLimitHint,目前仍兼容旧用法,将在2.x直接废弃)
291 */
292 showLimitHint: _propTypes2.default.bool,
293 /**
294 * 当设置了maxLength时,是否截断超出字符串
295 */
296 cutString: _propTypes2.default.bool,
297 /**
298 * 只读
299 */
300 readOnly: _propTypes2.default.bool,
301 /**
302 * onChange返回会自动去除头尾空字符
303 */
304 trim: _propTypes2.default.bool,
305 /**
306 * 输入提示
307 */
308 placeholder: _propTypes2.default.string,
309 /**
310 * 获取焦点时候触发的回调
311 * @param {Event} e DOM事件对象
312 */
313 onFocus: _propTypes2.default.func,
314 /**
315 * 失去焦点时候触发的回调
316 * @param {Event} e DOM事件对象
317 */
318 onBlur: _propTypes2.default.func,
319 /**
320 * 自定义字符串计算长度方式
321 * @param {String} value 数据
322 * @returns {Number} 自定义长度
323 */
324 getValueLength: _propTypes2.default.func,
325 inputStyle: _propTypes2.default.object,
326 /**
327 * 自定义class
328 */
329 className: _propTypes2.default.string,
330 /**
331 * 自定义内联样式
332 */
333 style: _propTypes2.default.object,
334 /**
335 * 原生type
336 */
337 htmlType: _propTypes2.default.string,
338 /**
339 * name
340 */
341 name: _propTypes2.default.string,
342 rtl: _propTypes2.default.bool,
343 state: _propTypes2.default.oneOf(['error', 'loading', 'success', 'warning']),
344 locale: _propTypes2.default.object,
345 /**
346 * 是否为预览态
347 */
348 isPreview: _propTypes2.default.bool,
349 /**
350 * 预览态模式下渲染的内容
351 * @param {number} value 评分值
352 */
353 renderPreview: _propTypes2.default.func,
354 /**
355 * 尺寸
356 * @enumdesc 小, 中, 大
357 */
358 size: _propTypes2.default.oneOf(['small', 'medium', 'large']),
359 /**
360 * 开启后会过滤输入法中间字母状态,文字输入完成后才会触发 onChange
361 * @version 1.23
362 */
363 composition: _propTypes2.default.bool,
364 onCompositionStart: _propTypes2.default.func,
365 onCompositionEnd: _propTypes2.default.func
366}), _class.defaultProps = {
367 disabled: false,
368 prefix: 'next-',
369 size: 'medium',
370 maxLength: null,
371 showLimitHint: false,
372 cutString: true,
373 readOnly: false,
374 isPreview: false,
375 trim: false,
376 composition: false,
377 onFocus: _util.func.noop,
378 onBlur: _util.func.noop,
379 onChange: _util.func.noop,
380 onKeyDown: _util.func.noop,
381 getValueLength: _util.func.noop,
382 onCompositionStart: _util.func.noop,
383 onCompositionEnd: _util.func.noop,
384 locale: _zhCn2.default.Input
385}, _temp2);
386Base.displayName = 'Base';
387exports.default = (0, _reactLifecyclesCompat.polyfill)(Base);
388module.exports = exports['default'];
\No newline at end of file