/
input.tsx
386 lines (361 loc) · 9.31 KB
/
input.tsx
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import type { PropType } from 'vue';
import { computed, defineComponent, ref, nextTick, watch } from 'vue';
import { getPrefixCls } from '../_utils/global-config';
import { INPUT_EVENTS, Size, SIZES } from '../_utils/constant';
import { Enter } from '../_utils/keycode';
import IconHover from '../_components/icon-hover.vue';
import IconClose from '../icon/icon-close';
import { omit } from '../_utils/omit';
import pick from '../_utils/pick';
const INPUT_TYPES = ['text', 'password'] as const;
type InputType = typeof INPUT_TYPES[number];
export default defineComponent({
name: 'Input',
inheritAttrs: false,
props: {
/**
* @zh 绑定值
* @en Value
*/
modelValue: String,
/**
* @zh 默认值(非受控状态)
* @en Default value (uncontrolled state)
*/
defaultValue: {
type: String,
default: '',
},
/**
* @zh 输入框大小
* @en Input size
* @values 'mini', 'small', 'medium', 'large'
*/
size: {
type: String as PropType<Size>,
default: 'medium',
validator: (value: any) => {
return SIZES.includes(value);
},
},
/**
* @zh 是否允许清空输入框
* @en Whether to allow the input to be cleared
*/
allowClear: {
type: Boolean,
default: false,
},
/**
* @zh 是否禁用
* @en Whether to disable
*/
disabled: {
type: Boolean,
default: false,
},
/**
* @zh 是否为只读状态
* @en Whether it is read-only
*/
readonly: {
type: Boolean,
default: false,
},
/**
* @zh 是否为错误状态
* @en Whether it is an error state
*/
error: {
type: Boolean,
default: false,
},
/**
* @zh 提示文字
* @en Prompt text
*/
placeholder: String,
/**
* @zh 输入值得最大长度
* @en Enter the maximum length of the value
*/
maxLength: Number,
/**
* @zh 是否显示字数统计
* @en Whether to display word count
*/
showWordLimit: {
type: Boolean,
default: false,
},
// private
type: {
type: String as PropType<InputType>,
default: 'text',
},
// for JSX
onInput: {
type: Function as PropType<(value: string, e: Event) => void>,
},
onChange: {
type: Function as PropType<(value: string) => void>,
},
onPressEnter: {
type: Function as PropType<() => void>,
},
onClear: {
type: Function as PropType<() => void>,
},
onFocus: {
type: Function as PropType<() => void>,
},
onBlur: {
type: Function as PropType<() => void>,
},
},
emits: [
'update:modelValue',
/**
* @zh 用户输入时触发
* @en Triggered when the user enters
*/
'input',
/**
* @zh 仅在输入框失焦或按下回车时触发
* @en Only triggered when the input box is out of focus or when you press Enter
*/
'change',
/**
* @zh 用户按下回车时触发
* @en Triggered when the user presses enter
*/
'pressEnter',
/**
* @zh 用户点击清除按钮时触发
* @en Triggered when the user clicks the clear button
*/
'clear',
/**
* @zh 输入框获取焦点时触发
* @en Triggered when the input box gets focus
*/
'focus',
/**
* @zh 输入框失去焦点时触发
* @en Triggered when the input box loses focus
*/
'blur',
],
/**
* @zh 前缀元素
* @en Prefix
* @slot prefix
*/
/**
* @zh 后缀元素
* @en Suffix
* @slot suffix
*/
/**
* @zh 前置标签
* @en Prepend
* @slot prepend
*/
/**
* @zh 后置标签
* @en Append
* @slot append
*/
setup(props, { emit, slots, attrs }) {
const prefixCls = getPrefixCls('input');
const inputRef = ref<HTMLInputElement>();
// 值相关
const _value = ref(props.defaultValue);
const computedValue = computed(() => props.modelValue ?? _value.value);
// 状态相关
const focused = ref(false);
const showClearBtn = computed(
() => props.allowClear && !props.disabled && Boolean(computedValue.value)
);
// 输入法相关
const isComposition = ref(false);
const compositionValue = ref('');
const updateValue = (value: string, inner = true) => {
if (props.maxLength && value.length > props.maxLength) {
value = value.slice(0, props.maxLength);
}
_value.value = value;
if (inner) {
emit('update:modelValue', value);
}
nextTick(() => {
if (inputRef.value && computedValue.value !== inputRef.value.value) {
inputRef.value.value = computedValue.value;
}
});
};
const handleMousedown = (e: MouseEvent) => {
if (inputRef.value && e.target !== inputRef.value) {
e.preventDefault();
inputRef.value.focus();
}
};
const handleFocus = (e: Event) => {
focused.value = true;
emit('focus', e);
};
const handleBlur = (e: Event) => {
focused.value = false;
emit('change', computedValue.value);
emit('blur', e);
};
const handleComposition = (e: Event) => {
const { value } = e.target as HTMLInputElement;
if (e.type === 'compositionend') {
isComposition.value = false;
compositionValue.value = '';
emit('input', value, e);
updateValue(value);
} else {
isComposition.value = true;
}
};
const handleInput = (e: Event) => {
const { value } = e.target as HTMLInputElement;
if (!isComposition.value) {
emit('input', value, e);
updateValue(value);
} else {
compositionValue.value = value;
}
};
const handleClear = () => {
emit('clear');
updateValue('');
};
const handleKeyDown = (e: KeyboardEvent) => {
const keyCode = e.code || e.key;
if (!isComposition.value && keyCode === Enter.code) {
emit('change', computedValue.value);
emit('pressEnter', e);
e.preventDefault();
}
};
// modelValue发生改变时,更新内部值
watch(
() => props.modelValue,
(value: string | undefined) => {
if (value !== computedValue.value) {
updateValue(value ?? '', false);
}
}
);
const outerCls = computed(() => [
`${prefixCls}-outer`,
`${prefixCls}-outer-size-${props.size}`,
{
[`${prefixCls}-outer-has-suffix`]: Boolean(slots.suffix),
[`${prefixCls}-outer-disabled`]: props.disabled,
},
]);
const wrapperCls = computed(() => [
`${prefixCls}-wrapper`,
{
[`${prefixCls}-error`]: props.error,
[`${prefixCls}-disabled`]: props.disabled,
[`${prefixCls}-focus`]: focused.value,
},
]);
const cls = computed(() => [prefixCls, `${prefixCls}-size-${props.size}`]);
const wrapperAttrs = omit(attrs, INPUT_EVENTS);
const inputAttrs = pick(attrs, INPUT_EVENTS);
const renderInput = () => (
<span
class={wrapperCls.value}
onMousedown={handleMousedown}
{...wrapperAttrs}
>
{slots.prefix && (
<span class={`${prefixCls}-prefix`}>{slots.prefix()}</span>
)}
<input
ref={inputRef}
class={cls.value}
value={computedValue.value}
type={props.type}
placeholder={props.placeholder}
readonly={props.readonly}
disabled={props.disabled}
onInput={handleInput}
onKeydown={handleKeyDown}
onFocus={handleFocus}
onBlur={handleBlur}
onCompositionstart={handleComposition}
onCompositionupdate={handleComposition}
onCompositionend={handleComposition}
{...inputAttrs}
/>
{showClearBtn.value && (
<IconHover
prefix={prefixCls}
class={`${prefixCls}-clear-btn`}
onClick={handleClear}
>
<IconClose />
</IconHover>
)}
{(slots.suffix || (props.maxLength && props.showWordLimit)) && (
<span class={`${prefixCls}-suffix`}>
{props.maxLength && props.showWordLimit && (
<span class={`${prefixCls}-word-limit`}>
{computedValue.value.length}/{props.maxLength}
</span>
)}
{slots.suffix?.()}
</span>
)}
</span>
);
const render = () => {
if (slots.prepend || slots.append) {
return (
<span class={outerCls.value}>
{slots.prepend && (
<span class={`${prefixCls}-prepend`}>{slots.prepend()}</span>
)}
{renderInput()}
{slots.append && (
<span class={`${prefixCls}-append`}>{slots.append()}</span>
)}
</span>
);
}
return renderInput();
};
return {
inputRef,
render,
};
},
methods: {
/**
* @zh 使输入框获取焦点
* @en Make the input box focus
* @public
*/
focus() {
(this.inputRef as HTMLInputElement)?.focus();
},
/**
* @zh 使输入框失去焦点
* @en Make the input box lose focus
* @public
*/
blur() {
(this.inputRef as HTMLInputElement)?.blur();
},
},
render() {
return this.render();
},
});