1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _extends2 = require('babel-runtime/helpers/extends');
|
6 |
|
7 | var _extends3 = _interopRequireDefault(_extends2);
|
8 |
|
9 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
10 |
|
11 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
12 |
|
13 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
14 |
|
15 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
16 |
|
17 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
18 |
|
19 | var _inherits3 = _interopRequireDefault(_inherits2);
|
20 |
|
21 | var _class, _temp2;
|
22 |
|
23 | var _react = require('react');
|
24 |
|
25 | var _react2 = _interopRequireDefault(_react);
|
26 |
|
27 | var _propTypes = require('prop-types');
|
28 |
|
29 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
30 |
|
31 | var _classnames = require('classnames');
|
32 |
|
33 | var _classnames2 = _interopRequireDefault(_classnames);
|
34 |
|
35 | var _reactLifecyclesCompat = require('react-lifecycles-compat');
|
36 |
|
37 | var _configProvider = require('../config-provider');
|
38 |
|
39 | var _configProvider2 = _interopRequireDefault(_configProvider);
|
40 |
|
41 | var _util = require('../util');
|
42 |
|
43 | var _zhCn = require('../locale/zh-cn');
|
44 |
|
45 | var _zhCn2 = _interopRequireDefault(_zhCn);
|
46 |
|
47 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
48 |
|
49 | var 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 |
|
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 |
|
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 |
|
136 | if (this.props.trim && e.keyCode === 32) {
|
137 | opts.beTrimed = true;
|
138 | }
|
139 |
|
140 |
|
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 |
|
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 |
|
272 |
|
273 |
|
274 | onChange: _propTypes2.default.func,
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 | onKeyDown: _propTypes2.default.func,
|
281 | |
282 |
|
283 |
|
284 | disabled: _propTypes2.default.bool,
|
285 | |
286 |
|
287 |
|
288 | maxLength: _propTypes2.default.number,
|
289 | |
290 |
|
291 |
|
292 | showLimitHint: _propTypes2.default.bool,
|
293 | |
294 |
|
295 |
|
296 | cutString: _propTypes2.default.bool,
|
297 | |
298 |
|
299 |
|
300 | readOnly: _propTypes2.default.bool,
|
301 | |
302 |
|
303 |
|
304 | trim: _propTypes2.default.bool,
|
305 | |
306 |
|
307 |
|
308 | placeholder: _propTypes2.default.string,
|
309 | |
310 |
|
311 |
|
312 |
|
313 | onFocus: _propTypes2.default.func,
|
314 | |
315 |
|
316 |
|
317 |
|
318 | onBlur: _propTypes2.default.func,
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 | getValueLength: _propTypes2.default.func,
|
325 | inputStyle: _propTypes2.default.object,
|
326 | |
327 |
|
328 |
|
329 | className: _propTypes2.default.string,
|
330 | |
331 |
|
332 |
|
333 | style: _propTypes2.default.object,
|
334 | |
335 |
|
336 |
|
337 | htmlType: _propTypes2.default.string,
|
338 | |
339 |
|
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 |
|
352 |
|
353 | renderPreview: _propTypes2.default.func,
|
354 | |
355 |
|
356 |
|
357 |
|
358 | size: _propTypes2.default.oneOf(['small', 'medium', 'large']),
|
359 | |
360 |
|
361 |
|
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);
|
386 | Base.displayName = 'Base';
|
387 | exports.default = (0, _reactLifecyclesCompat.polyfill)(Base);
|
388 | module.exports = exports['default']; |
\ | No newline at end of file |