forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.tsx
253 lines (241 loc) · 6.83 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
// base 0.0.1-alpha.7
import type { ComponentPublicInstance, VNode } from 'vue';
import { onMounted, defineComponent, nextTick, shallowRef, watch, withDirectives } from 'vue';
import classNames from '../_util/classNames';
import type { ChangeEvent, FocusEventHandler } from '../_util/EventInterface';
import omit from '../_util/omit';
import type { InputProps } from './inputProps';
import { inputProps } from './inputProps';
import type { InputFocusOptions } from './utils/commonUtils';
import {
fixControlledValue,
hasAddon,
hasPrefixSuffix,
resolveOnChange,
triggerFocus,
} from './utils/commonUtils';
import antInputDirective from '../_util/antInputDirective';
import BaseInput from './BaseInput';
export default defineComponent({
name: 'VCInput',
inheritAttrs: false,
props: inputProps(),
setup(props, { slots, attrs, expose, emit }) {
const stateValue = shallowRef(props.value === undefined ? props.defaultValue : props.value);
const focused = shallowRef(false);
const inputRef = shallowRef<HTMLInputElement>();
const rootRef = shallowRef<ComponentPublicInstance>();
watch(
() => props.value,
() => {
stateValue.value = props.value;
},
);
watch(
() => props.disabled,
() => {
if (props.disabled) {
focused.value = false;
}
},
);
const focus = (option?: InputFocusOptions) => {
if (inputRef.value) {
triggerFocus(inputRef.value, option);
}
};
const blur = () => {
inputRef.value?.blur();
};
const setSelectionRange = (
start: number,
end: number,
direction?: 'forward' | 'backward' | 'none',
) => {
inputRef.value?.setSelectionRange(start, end, direction);
};
const select = () => {
inputRef.value?.select();
};
expose({
focus,
blur,
input: inputRef,
stateValue,
setSelectionRange,
select,
});
const triggerChange = (e: Event) => {
emit('change', e);
};
const setValue = (value: string | number, callback?: Function) => {
if (stateValue.value === value) {
return;
}
if (props.value === undefined) {
stateValue.value = value;
} else {
nextTick(() => {
if (inputRef.value.value !== stateValue.value) {
rootRef.value?.$forceUpdate();
}
});
}
nextTick(() => {
callback && callback();
});
};
const handleChange = (e: ChangeEvent) => {
const { value, composing } = e.target as any;
// https://github.com/vueComponent/ant-design-vue/issues/2203
if (((e as any).isComposing && composing && props.lazy) || stateValue.value === value) return;
const newVal = e.target.value;
resolveOnChange(inputRef.value, e, triggerChange);
setValue(newVal);
};
const handleKeyDown = (e: KeyboardEvent) => {
if (e.keyCode === 13) {
emit('pressEnter', e);
}
emit('keydown', e);
};
const handleFocus: FocusEventHandler = e => {
focused.value = true;
emit('focus', e);
};
const handleBlur: FocusEventHandler = e => {
focused.value = false;
emit('blur', e);
};
const handleReset = (e: MouseEvent) => {
resolveOnChange(inputRef.value, e, triggerChange);
setValue('', () => {
focus();
});
};
const getInputElement = () => {
const {
addonBefore = slots.addonBefore,
addonAfter = slots.addonAfter,
disabled,
valueModifiers = {},
htmlSize,
autocomplete,
prefixCls,
inputClassName,
prefix = slots.prefix?.(),
suffix = slots.suffix?.(),
allowClear,
type = 'text',
} = props;
const otherProps = omit(props as InputProps & { placeholder: string }, [
'prefixCls',
'onPressEnter',
'addonBefore',
'addonAfter',
'prefix',
'suffix',
'allowClear',
// Input elements must be either controlled or uncontrolled,
// specify either the value prop, or the defaultValue prop, but not both.
'defaultValue',
'size',
'bordered',
'htmlSize',
'lazy',
'showCount',
'valueModifiers',
'showCount',
'affixWrapperClassName',
'groupClassName',
'inputClassName',
'wrapperClassName',
]);
const inputProps = {
...otherProps,
...attrs,
autocomplete,
onChange: handleChange,
onInput: handleChange,
onFocus: handleFocus,
onBlur: handleBlur,
onKeydown: handleKeyDown,
class: classNames(
prefixCls,
{
[`${prefixCls}-disabled`]: disabled,
},
inputClassName,
!hasAddon({ addonAfter, addonBefore }) &&
!hasPrefixSuffix({ prefix, suffix, allowClear }) &&
attrs.class,
),
ref: inputRef,
key: 'ant-input',
size: htmlSize,
type,
};
if (valueModifiers.lazy) {
delete inputProps.onInput;
}
if (!inputProps.autofocus) {
delete inputProps.autofocus;
}
const inputNode = <input {...omit(inputProps, ['size'])} />;
return withDirectives(inputNode as VNode, [[antInputDirective]]);
};
const getSuffix = () => {
const { maxlength, suffix = slots.suffix?.(), showCount, prefixCls } = props;
// Max length value
const hasMaxLength = Number(maxlength) > 0;
if (suffix || showCount) {
const valueLength = [...fixControlledValue(stateValue.value)].length;
const dataCount =
typeof showCount === 'object'
? showCount.formatter({ count: valueLength, maxlength })
: `${valueLength}${hasMaxLength ? ` / ${maxlength}` : ''}`;
return (
<>
{!!showCount && (
<span
class={classNames(`${prefixCls}-show-count-suffix`, {
[`${prefixCls}-show-count-has-suffix`]: !!suffix,
})}
>
{dataCount}
</span>
)}
{suffix}
</>
);
}
return null;
};
onMounted(() => {
if (process.env.NODE_ENV === 'test') {
if (props.autofocus) {
focus();
}
}
});
return () => {
const { prefixCls, disabled, ...rest } = props;
return (
<BaseInput
{...rest}
{...attrs}
ref={rootRef}
prefixCls={prefixCls}
inputElement={getInputElement()}
handleReset={handleReset}
value={fixControlledValue(stateValue.value)}
focused={focused.value}
triggerFocus={focus}
suffix={getSuffix()}
disabled={disabled}
v-slots={slots}
/>
);
};
},
});