2021-06-26 01:35:40 +00:00
|
|
|
import type { VNode } from 'vue';
|
2021-12-14 15:50:22 +00:00
|
|
|
import {
|
|
|
|
getCurrentInstance,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
watch,
|
|
|
|
ref,
|
|
|
|
defineComponent,
|
|
|
|
nextTick,
|
|
|
|
withDirectives,
|
|
|
|
} from 'vue';
|
2020-09-21 10:45:27 +00:00
|
|
|
import antInputDirective from '../_util/antInputDirective';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-12-14 15:50:22 +00:00
|
|
|
import type { InputProps } from './inputProps';
|
2019-01-12 03:33:27 +00:00
|
|
|
import inputProps from './inputProps';
|
2022-03-12 01:56:32 +00:00
|
|
|
import { getInputClassName } from './util';
|
2020-03-07 11:45:13 +00:00
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
|
|
|
import omit from '../_util/omit';
|
2021-12-14 15:50:22 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import type { ChangeEvent, FocusEventHandler } from '../_util/EventInterface';
|
2017-12-06 10:54:20 +00:00
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
export function fixControlledValue(value: string | number) {
|
2017-12-06 10:54:20 +00:00
|
|
|
if (typeof value === 'undefined' || value === null) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return '';
|
2017-12-06 10:54:20 +00:00
|
|
|
}
|
2022-03-12 01:56:32 +00:00
|
|
|
return String(value);
|
2017-12-06 10:54:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 15:50:22 +00:00
|
|
|
export function resolveOnChange(
|
|
|
|
target: HTMLInputElement,
|
|
|
|
e: Event,
|
|
|
|
onChange: Function,
|
|
|
|
targetValue?: string,
|
|
|
|
) {
|
|
|
|
if (!onChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const event: any = e;
|
|
|
|
|
|
|
|
if (e.type === 'click') {
|
|
|
|
Object.defineProperty(event, 'target', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
Object.defineProperty(event, 'currentTarget', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
// click clear icon
|
|
|
|
//event = Object.create(e);
|
2021-12-16 06:30:27 +00:00
|
|
|
const currentTarget = target.cloneNode(true);
|
|
|
|
|
|
|
|
event.target = currentTarget;
|
|
|
|
event.currentTarget = currentTarget;
|
2021-12-14 15:50:22 +00:00
|
|
|
// change target ref value cause e.target.value should be '' when clear input
|
2021-12-16 06:30:27 +00:00
|
|
|
(currentTarget as any).value = '';
|
2020-03-07 11:45:13 +00:00
|
|
|
onChange(event);
|
2021-12-14 15:50:22 +00:00
|
|
|
return;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
2021-12-14 15:50:22 +00:00
|
|
|
// Trigger by composition event, this means we need force change the input value
|
|
|
|
if (targetValue !== undefined) {
|
|
|
|
Object.defineProperty(event, 'target', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
Object.defineProperty(event, 'currentTarget', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
event.target = target;
|
|
|
|
event.currentTarget = target;
|
|
|
|
target.value = targetValue;
|
|
|
|
onChange(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(event);
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 15:50:22 +00:00
|
|
|
export interface InputFocusOptions extends FocusOptions {
|
|
|
|
cursor?: 'start' | 'end' | 'all';
|
|
|
|
}
|
|
|
|
export function triggerFocus(
|
|
|
|
element?: HTMLInputElement | HTMLTextAreaElement,
|
|
|
|
option?: InputFocusOptions,
|
|
|
|
) {
|
|
|
|
if (!element) return;
|
|
|
|
|
|
|
|
element.focus(option);
|
|
|
|
|
|
|
|
// Selection content
|
|
|
|
const { cursor } = option || {};
|
|
|
|
if (cursor) {
|
|
|
|
const len = element.value.length;
|
|
|
|
|
|
|
|
switch (cursor) {
|
|
|
|
case 'start':
|
|
|
|
element.setSelectionRange(0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'end':
|
|
|
|
element.setSelectionRange(len, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
element.setSelectionRange(0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:46:05 +00:00
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AInput',
|
2019-02-01 09:23:00 +00:00
|
|
|
inheritAttrs: false,
|
2022-03-26 14:52:54 +00:00
|
|
|
props: inputProps(),
|
2021-12-14 15:50:22 +00:00
|
|
|
setup(props, { slots, attrs, expose, emit }) {
|
|
|
|
const inputRef = ref();
|
|
|
|
const clearableInputRef = ref();
|
|
|
|
let removePasswordTimeout: any;
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2021-12-14 15:50:22 +00:00
|
|
|
const { direction, prefixCls, size, autocomplete } = useConfigInject('input', props);
|
2021-12-15 12:48:24 +00:00
|
|
|
const stateValue = ref(props.value === undefined ? props.defaultValue : props.value);
|
2021-12-14 15:50:22 +00:00
|
|
|
const focused = ref(false);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.value,
|
|
|
|
() => {
|
2021-12-23 14:00:14 +00:00
|
|
|
stateValue.value = props.value;
|
2021-12-15 12:48:24 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
watch(
|
|
|
|
() => props.disabled,
|
|
|
|
() => {
|
|
|
|
if (props.value !== undefined) {
|
2021-12-14 15:50:22 +00:00
|
|
|
stateValue.value = props.value;
|
|
|
|
}
|
2022-03-12 01:56:32 +00:00
|
|
|
if (props.disabled) {
|
|
|
|
focused.value = false;
|
|
|
|
}
|
2021-12-14 15:50:22 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const clearPasswordValueAttribute = () => {
|
|
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
|
|
removePasswordTimeout = setTimeout(() => {
|
|
|
|
if (
|
|
|
|
inputRef.value?.getAttribute('type') === 'password' &&
|
|
|
|
inputRef.value.hasAttribute('value')
|
|
|
|
) {
|
|
|
|
inputRef.value.removeAttribute('value');
|
|
|
|
}
|
|
|
|
});
|
2020-06-27 06:02:30 +00:00
|
|
|
};
|
2021-12-14 15:50:22 +00:00
|
|
|
|
|
|
|
const focus = (option?: InputFocusOptions) => {
|
|
|
|
triggerFocus(inputRef.value, option);
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-12-14 15:50:22 +00:00
|
|
|
|
|
|
|
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,
|
2021-12-15 12:48:24 +00:00
|
|
|
input: inputRef,
|
2021-12-14 15:50:22 +00:00
|
|
|
stateValue,
|
|
|
|
setSelectionRange,
|
|
|
|
select,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2021-12-14 15:50:22 +00:00
|
|
|
|
|
|
|
const onFocus: FocusEventHandler = e => {
|
|
|
|
const { onFocus } = props;
|
|
|
|
focused.value = true;
|
|
|
|
onFocus?.(e);
|
|
|
|
nextTick(() => {
|
|
|
|
clearPasswordValueAttribute();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onBlur: FocusEventHandler = e => {
|
|
|
|
const { onBlur } = props;
|
|
|
|
focused.value = false;
|
|
|
|
onBlur?.(e);
|
|
|
|
formItemContext.onFieldBlur();
|
|
|
|
nextTick(() => {
|
|
|
|
clearPasswordValueAttribute();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const triggerChange = (e: Event) => {
|
|
|
|
emit('update:value', (e.target as HTMLInputElement).value);
|
|
|
|
emit('change', e);
|
|
|
|
emit('input', e);
|
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
|
|
|
const instance = getCurrentInstance();
|
|
|
|
const setValue = (value: string | number, callback?: Function) => {
|
|
|
|
if (stateValue.value === value) {
|
2019-04-07 09:19:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-15 12:48:24 +00:00
|
|
|
if (props.value === undefined) {
|
2021-12-14 15:50:22 +00:00
|
|
|
stateValue.value = value;
|
2019-04-07 09:19:18 +00:00
|
|
|
} else {
|
2021-12-16 06:30:27 +00:00
|
|
|
nextTick(() => {
|
|
|
|
if (inputRef.value.value !== stateValue.value) {
|
|
|
|
instance.update();
|
|
|
|
}
|
|
|
|
});
|
2019-04-07 09:19:18 +00:00
|
|
|
}
|
2020-10-28 07:31:45 +00:00
|
|
|
nextTick(() => {
|
2020-07-30 10:41:38 +00:00
|
|
|
callback && callback();
|
|
|
|
});
|
2021-12-14 15:50:22 +00:00
|
|
|
};
|
|
|
|
const handleReset = (e: MouseEvent) => {
|
|
|
|
resolveOnChange(inputRef.value, e, triggerChange);
|
|
|
|
setValue('', () => {
|
|
|
|
focus();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = (e: ChangeEvent) => {
|
2021-12-15 12:48:24 +00:00
|
|
|
const { value, composing } = e.target as any;
|
2021-12-14 15:50:22 +00:00
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2203
|
2021-12-15 12:48:24 +00:00
|
|
|
if ((((e as any).isComposing || composing) && props.lazy) || stateValue.value === value)
|
|
|
|
return;
|
2021-12-14 15:50:22 +00:00
|
|
|
const newVal = e.target.value;
|
|
|
|
resolveOnChange(inputRef.value, e, triggerChange);
|
|
|
|
setValue(newVal, () => {
|
|
|
|
clearPasswordValueAttribute();
|
2019-10-17 11:26:58 +00:00
|
|
|
});
|
2021-12-14 15:50:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
emit('pressEnter', e);
|
|
|
|
}
|
|
|
|
emit('keydown', e);
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
clearPasswordValueAttribute();
|
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
clearTimeout(removePasswordTimeout);
|
|
|
|
});
|
|
|
|
|
|
|
|
const renderInput = () => {
|
|
|
|
const {
|
|
|
|
addonBefore = slots.addonBefore,
|
|
|
|
addonAfter = slots.addonAfter,
|
|
|
|
disabled,
|
|
|
|
bordered = true,
|
|
|
|
valueModifiers = {},
|
2021-12-15 12:48:24 +00:00
|
|
|
htmlSize,
|
2021-12-14 15:50:22 +00:00
|
|
|
} = props;
|
2022-03-26 14:52:54 +00:00
|
|
|
const otherProps = omit(props as InputProps & { placeholder: string }, [
|
2017-12-07 04:31:12 +00:00
|
|
|
'prefixCls',
|
2020-07-30 10:41:38 +00:00
|
|
|
'onPressEnter',
|
2018-04-07 06:29:59 +00:00
|
|
|
'addonBefore',
|
|
|
|
'addonAfter',
|
|
|
|
'prefix',
|
|
|
|
'suffix',
|
2019-09-11 10:11:48 +00:00
|
|
|
'allowClear',
|
2021-12-14 15:50:22 +00:00
|
|
|
// Input elements must be either controlled or uncontrolled,
|
|
|
|
// specify either the value prop, or the defaultValue prop, but not both.
|
2019-01-03 12:51:56 +00:00
|
|
|
'defaultValue',
|
2020-03-07 11:45:13 +00:00
|
|
|
'size',
|
2021-12-14 15:50:22 +00:00
|
|
|
'bordered',
|
2021-12-15 12:48:24 +00:00
|
|
|
'htmlSize',
|
2021-12-15 14:27:07 +00:00
|
|
|
'lazy',
|
2022-03-12 01:56:32 +00:00
|
|
|
'showCount',
|
2022-04-19 03:28:21 +00:00
|
|
|
'valueModifiers',
|
2019-01-12 03:33:27 +00:00
|
|
|
]);
|
2021-12-14 15:50:22 +00:00
|
|
|
const inputProps = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...otherProps,
|
2021-12-15 12:48:24 +00:00
|
|
|
...attrs,
|
2021-12-14 15:50:22 +00:00
|
|
|
autocomplete: autocomplete.value,
|
|
|
|
onChange: handleChange,
|
|
|
|
onInput: handleChange,
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
2020-06-27 06:02:30 +00:00
|
|
|
onKeydown: handleKeyDown,
|
2021-12-14 15:50:22 +00:00
|
|
|
class: classNames(
|
|
|
|
getInputClassName(prefixCls.value, bordered, size.value, disabled, direction.value),
|
|
|
|
{
|
|
|
|
[attrs.class as string]: attrs.class && !addonBefore && !addonAfter,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ref: inputRef,
|
2019-09-11 10:11:48 +00:00
|
|
|
key: 'ant-input',
|
2021-12-15 12:48:24 +00:00
|
|
|
size: htmlSize,
|
2021-12-15 14:27:07 +00:00
|
|
|
id: otherProps.id ?? formItemContext.id.value,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-12-10 08:29:04 +00:00
|
|
|
if (valueModifiers.lazy) {
|
|
|
|
delete inputProps.onInput;
|
|
|
|
}
|
2020-07-17 09:36:58 +00:00
|
|
|
if (!inputProps.autofocus) {
|
|
|
|
delete inputProps.autofocus;
|
2020-07-07 05:57:11 +00:00
|
|
|
}
|
2022-04-19 01:52:00 +00:00
|
|
|
const inputNode = <input {...omit(inputProps, ['size'])} />;
|
2020-10-16 06:24:14 +00:00
|
|
|
return withDirectives(inputNode as VNode, [[antInputDirective]]);
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
|
2022-03-12 01:56:32 +00:00
|
|
|
const renderShowCountSuffix = () => {
|
|
|
|
const value = stateValue.value;
|
|
|
|
const { maxlength, suffix = slots.suffix?.(), showCount } = props;
|
|
|
|
// Max length value
|
|
|
|
const hasMaxLength = Number(maxlength) > 0;
|
|
|
|
|
|
|
|
if (suffix || showCount) {
|
|
|
|
const valueLength = [...fixControlledValue(value)].length;
|
|
|
|
let dataCount = null;
|
|
|
|
if (typeof showCount === 'object') {
|
|
|
|
dataCount = showCount.formatter({ count: valueLength, maxlength });
|
|
|
|
} else {
|
|
|
|
dataCount = `${valueLength}${hasMaxLength ? ` / ${maxlength}` : ''}`;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!!showCount && (
|
|
|
|
<span
|
|
|
|
class={classNames(`${prefixCls.value}-show-count-suffix`, {
|
|
|
|
[`${prefixCls.value}-show-count-has-suffix`]: !!suffix,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{dataCount}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{suffix}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2021-12-14 15:50:22 +00:00
|
|
|
return () => {
|
|
|
|
const inputProps: any = {
|
|
|
|
...attrs,
|
|
|
|
...props,
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
inputType: 'input',
|
|
|
|
value: fixControlledValue(stateValue.value),
|
|
|
|
handleReset,
|
2022-01-22 03:09:49 +00:00
|
|
|
focused: focused.value && !props.disabled,
|
2021-12-14 15:50:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ClearableLabeledInput
|
2022-03-12 01:56:32 +00:00
|
|
|
{...omit(inputProps, ['element', 'valueModifiers', 'suffix', 'showCount'])}
|
2021-12-14 15:50:22 +00:00
|
|
|
ref={clearableInputRef}
|
2022-03-12 01:56:32 +00:00
|
|
|
v-slots={{ ...slots, element: renderInput, suffix: renderShowCountSuffix }}
|
2021-12-14 15:50:22 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2020-10-12 10:46:05 +00:00
|
|
|
});
|