forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.jsx
334 lines (296 loc) · 8.83 KB
/
base.jsx
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { polyfill } from 'react-lifecycles-compat';
import ConfigProvider from '../config-provider';
import { func } from '../util';
import zhCN from '../locale/zh-cn';
class Base extends React.Component {
static propTypes = {
...ConfigProvider.propTypes,
/**
* 当前值
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* 初始化值
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* 发生改变的时候触发的回调
* @param {String} value 数据
* @param {Event} e DOM事件对象
*/
onChange: PropTypes.func,
/**
* 键盘按下的时候触发的回调
* @param {Event} e DOM事件对象
* @param {Object} opts 可扩展的附加信息:<br> - opts.overMaxLength: {Boolean} 已超出最大长度<br> - opts.beTrimed: {Boolean} 输入的空格被清理
*/
onKeyDown: PropTypes.func,
/**
* 禁用状态
*/
disabled: PropTypes.bool,
/**
* 最大长度
*/
maxLength: PropTypes.number,
/**
* 是否展现最大长度样式(旧版本为 hasLimitHint,目前仍兼容旧用法,将在2.x直接废弃)
*/
showLimitHint: PropTypes.bool,
/**
* 当设置了maxLength时,是否截断超出字符串
*/
cutString: PropTypes.bool,
/**
* 只读
*/
readOnly: PropTypes.bool,
/**
* onChange返回会自动去除头尾空字符
*/
trim: PropTypes.bool,
/**
* 输入提示
*/
placeholder: PropTypes.string,
/**
* 获取焦点时候触发的回调
* @param {Event} e DOM事件对象
*/
onFocus: PropTypes.func,
/**
* 失去焦点时候触发的回调
* @param {Event} e DOM事件对象
*/
onBlur: PropTypes.func,
/**
* 自定义字符串计算长度方式
* @param {String} value 数据
* @returns {Number} 自定义长度
*/
getValueLength: PropTypes.func,
inputStyle: PropTypes.object,
/**
* 自定义class
*/
className: PropTypes.string,
/**
* 自定义内联样式
*/
style: PropTypes.object,
/**
* 原生type
*/
htmlType: PropTypes.string,
/**
* name
*/
name: PropTypes.string,
rtl: PropTypes.bool,
state: PropTypes.oneOf(['error', 'loading', 'success', 'warning']),
locale: PropTypes.object,
/**
* 是否为预览态
*/
isPreview: PropTypes.bool,
/**
* 预览态模式下渲染的内容
* @param {number} value 评分值
*/
renderPreview: PropTypes.func,
/**
* 尺寸
* @enumdesc 小, 中, 大
*/
size: PropTypes.oneOf(['small', 'medium', 'large']),
/**
* 开启后会过滤输入法中间字母状态,文字输入完成后才会触发 onChange
* @version 1.23
*/
composition: PropTypes.bool,
onCompositionStart: PropTypes.func,
onCompositionEnd: PropTypes.func,
};
static defaultProps = {
disabled: false,
prefix: 'next-',
size: 'medium',
maxLength: null,
showLimitHint: false,
cutString: true,
readOnly: false,
isPreview: false,
trim: false,
composition: false,
onFocus: func.noop,
onBlur: func.noop,
onChange: func.noop,
onKeyDown: func.noop,
getValueLength: func.noop,
onCompositionStart: func.noop,
onCompositionEnd: func.noop,
locale: zhCN.Input,
};
static getDerivedStateFromProps(nextProps, prevState) {
if ('value' in nextProps && nextProps.value !== prevState.value && !prevState.composition) {
const value = nextProps.value;
return {
value: value === undefined || value === null ? '' : value,
};
}
return null;
}
ieHack(value) {
return value;
}
handleCompositionStart = e => {
this.setState({
composition: true,
});
this.props.onCompositionStart(e);
};
handleCompositionEnd = e => {
this.setState({
composition: false,
});
this.props.onCompositionEnd(e);
const value = e.target.value;
this.props.onChange(value, e);
};
onChange(e) {
if ('stopPropagation' in e) {
e.stopPropagation();
} else if ('cancelBubble' in e) {
e.cancelBubble();
}
let value = e.target.value;
if (this.props.trim) {
value = value.trim();
}
value = this.ieHack(value);
// not controlled
if (!('value' in this.props) || this.state.composition) {
this.setState({
value,
});
}
if (this.state.composition) {
return;
}
// Number('') = 0
if (value && this.props.htmlType === 'number') {
value = Number(value);
}
this.props.onChange(value, e);
}
onKeyDown(e) {
const value = e.target.value;
const { maxLength } = this.props;
const len = maxLength > 0 && value ? this.getValueLength(value) : 0;
const opts = {};
// has enable trim and has input whitespace
if (this.props.trim && e.keyCode === 32) {
opts.beTrimed = true;
}
// has defined maxLength and has over max length and has not input backspace and delete
if (
maxLength > 0 &&
(len > maxLength + 1 ||
((len === maxLength || len === maxLength + 1) && e.keyCode !== 8 && e.keyCode !== 46))
) {
opts.overMaxLength = true;
}
this.props.onKeyDown(e, opts);
}
onFocus(e) {
this.setState({
focus: true,
});
this.props.onFocus(e);
}
onBlur(e) {
this.setState({
focus: false,
});
this.props.onBlur(e);
}
renderLength() {
const { maxLength, showLimitHint, prefix, rtl } = this.props;
const len = maxLength > 0 && this.state.value ? this.getValueLength(this.state.value) : 0;
const classesLenWrap = classNames({
[`${prefix}input-len`]: true,
[`${prefix}error`]: len > maxLength,
});
const content = rtl ? `${maxLength}/${len}` : `${len}/${maxLength}`;
return maxLength && showLimitHint ? <span className={classesLenWrap}>{content}</span> : null;
}
renderControl() {
const lenWrap = this.renderLength();
return lenWrap ? (
<span onClick={() => this.focus()} className={`${this.props.prefix}input-control`}>
{lenWrap}
</span>
) : null;
}
getClass() {
const { disabled, state, prefix } = this.props;
return classNames({
[`${prefix}input`]: true,
[`${prefix}disabled`]: !!disabled,
[`${prefix}error`]: state === 'error',
[`${prefix}warning`]: state === 'warning',
[`${prefix}focus`]: this.state.focus,
});
}
getProps() {
const {
placeholder,
inputStyle,
disabled,
readOnly,
cutString,
maxLength,
name,
onCompositionStart,
onCompositionEnd,
} = this.props;
const props = {
style: inputStyle,
placeholder,
disabled,
readOnly,
name,
maxLength: cutString ? maxLength : undefined,
value: this.state.value,
onChange: this.onChange.bind(this),
onBlur: this.onBlur.bind(this),
onFocus: this.onFocus.bind(this),
onCompositionStart,
onCompositionEnd,
};
// fix accessibility:auto process status of aria disabled
if (disabled) {
props['aria-disabled'] = disabled;
}
return props;
}
saveRef = input => {
this.inputRef = input;
};
getInputNode() {
return this.inputRef;
}
focus(start, end) {
this.inputRef.focus();
if (typeof start === 'number') {
this.inputRef.selectionStart = start;
}
if (typeof end === 'number') {
this.inputRef.selectionEnd = end;
}
}
}
export default polyfill(Base);