2021-06-26 01:35:40 +00:00
|
|
|
import type { VNode } from 'vue';
|
|
|
|
import { defineComponent, inject, 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';
|
2019-01-12 03:33:27 +00:00
|
|
|
import omit from 'omit.js';
|
|
|
|
import inputProps from './inputProps';
|
2020-06-27 06:02:30 +00:00
|
|
|
import { hasProp, getComponent, getOptionProps } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
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
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return value;
|
2017-12-06 10:54:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
export function resolveOnChange(target: HTMLInputElement, e: Event, onChange?: Function) {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (onChange) {
|
2020-11-07 14:39:18 +00:00
|
|
|
const event = e as any;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (e.type === 'click') {
|
|
|
|
// click clear icon
|
|
|
|
//event = Object.create(e);
|
|
|
|
Object.defineProperty(event, 'target', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
Object.defineProperty(event, 'currentTarget', {
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
event.target = target;
|
|
|
|
event.currentTarget = target;
|
|
|
|
const originalInputValue = target.value;
|
|
|
|
// change target ref value cause e.target.value should be '' when clear input
|
|
|
|
target.value = '';
|
|
|
|
onChange(event);
|
|
|
|
// reset target ref value
|
|
|
|
target.value = originalInputValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
export function getInputClassName(prefixCls: string, size: string, disabled: boolean) {
|
2020-03-07 11:45:13 +00:00
|
|
|
return classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
});
|
2019-04-07 09:19:18 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
props: {
|
|
|
|
...inputProps,
|
|
|
|
},
|
2020-06-27 06:02:30 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-16 06:24:14 +00:00
|
|
|
removePasswordTimeout: undefined,
|
|
|
|
input: null,
|
2020-10-19 08:43:10 +00:00
|
|
|
clearableInput: null,
|
2020-06-27 06:02:30 +00:00
|
|
|
};
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const props = this.$props;
|
|
|
|
const value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
|
2017-12-06 10:54:20 +00:00
|
|
|
return {
|
2020-05-06 07:05:08 +00:00
|
|
|
stateValue: typeof value === 'undefined' ? '' : value,
|
2020-09-29 14:11:06 +00:00
|
|
|
isFocused: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
watch: {
|
2020-09-21 13:48:12 +00:00
|
|
|
value(val) {
|
2019-09-11 10:11:48 +00:00
|
|
|
this.stateValue = val;
|
2020-09-21 13:48:12 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2020-10-28 07:31:45 +00:00
|
|
|
nextTick(() => {
|
2020-07-28 07:23:56 +00:00
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
if (this.autofocus) {
|
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
this.clearPasswordValueAttribute();
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (this.removePasswordTimeout) {
|
|
|
|
clearTimeout(this.removePasswordTimeout);
|
|
|
|
}
|
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
methods: {
|
2020-11-07 14:39:18 +00:00
|
|
|
handleInputFocus(e: Event) {
|
2020-09-29 14:11:06 +00:00
|
|
|
this.isFocused = true;
|
|
|
|
this.onFocus && this.onFocus(e);
|
|
|
|
},
|
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
handleInputBlur(e: Event) {
|
2020-09-29 14:11:06 +00:00
|
|
|
this.isFocused = false;
|
|
|
|
this.onBlur && this.onBlur(e);
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input.focus();
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input.blur();
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
select() {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input.select();
|
|
|
|
},
|
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
saveClearableInput(input: HTMLInputElement) {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.clearableInput = input;
|
|
|
|
},
|
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
saveInput(input: HTMLInputElement) {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input = input;
|
2018-12-09 03:43:27 +00:00
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
|
2020-11-07 14:39:18 +00:00
|
|
|
setValue(value: string | number, callback?: Function) {
|
2019-11-15 10:21:50 +00:00
|
|
|
if (this.stateValue === value) {
|
2019-04-07 09:19:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
this.stateValue = value;
|
|
|
|
} else {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.$forceUpdate();
|
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();
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
triggerChange(e: Event) {
|
2020-11-07 14:39:18 +00:00
|
|
|
this.$emit('update:value', (e.target as HTMLInputElement).value);
|
2019-04-07 09:19:18 +00:00
|
|
|
this.$emit('change', e);
|
|
|
|
this.$emit('input', e);
|
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
handleReset(e: Event) {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.setValue('', () => {
|
2019-10-17 11:26:58 +00:00
|
|
|
this.focus();
|
|
|
|
});
|
2020-07-30 10:41:38 +00:00
|
|
|
resolveOnChange(this.input, e, this.triggerChange);
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
renderInput(prefixCls: string, { addonBefore, addonAfter }) {
|
2017-12-07 04:31:12 +00:00
|
|
|
const otherProps = omit(this.$props, [
|
|
|
|
'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',
|
2019-01-03 12:51:56 +00:00
|
|
|
'defaultValue',
|
2019-12-06 09:11:17 +00:00
|
|
|
'lazy',
|
2020-03-07 11:45:13 +00:00
|
|
|
'size',
|
2020-06-27 06:02:30 +00:00
|
|
|
'inputPrefixCls',
|
|
|
|
'loading',
|
2019-01-12 03:33:27 +00:00
|
|
|
]);
|
2020-09-29 14:11:06 +00:00
|
|
|
const {
|
|
|
|
handleKeyDown,
|
|
|
|
handleChange,
|
|
|
|
handleInputFocus,
|
|
|
|
handleInputBlur,
|
|
|
|
size,
|
|
|
|
disabled,
|
|
|
|
$attrs,
|
|
|
|
} = this;
|
2020-06-27 06:02:30 +00:00
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
const inputProps: any = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...otherProps,
|
|
|
|
...$attrs,
|
|
|
|
onKeydown: handleKeyDown,
|
|
|
|
class: classNames(getInputClassName(prefixCls, size, disabled), {
|
2020-10-16 06:24:14 +00:00
|
|
|
[$attrs.class as string]: $attrs.class && !addonBefore && !addonAfter,
|
2020-06-27 06:02:30 +00:00
|
|
|
}),
|
|
|
|
ref: this.saveInput,
|
2019-09-11 10:11:48 +00:00
|
|
|
key: 'ant-input',
|
2020-07-06 09:44:52 +00:00
|
|
|
onInput: handleChange,
|
2020-08-11 10:26:09 +00:00
|
|
|
onChange: handleChange,
|
2020-09-29 14:11:06 +00:00
|
|
|
onFocus: handleInputFocus,
|
|
|
|
onBlur: handleInputBlur,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-07-17 09:36:58 +00:00
|
|
|
if (!inputProps.autofocus) {
|
|
|
|
delete inputProps.autofocus;
|
2020-07-07 05:57:11 +00:00
|
|
|
}
|
2020-10-19 08:43:10 +00:00
|
|
|
const inputNode = <input {...inputProps} />;
|
2020-10-16 06:24:14 +00:00
|
|
|
return withDirectives(inputNode as VNode, [[antInputDirective]]);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
clearPasswordValueAttribute() {
|
|
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
|
|
this.removePasswordTimeout = setTimeout(() => {
|
|
|
|
if (
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input &&
|
|
|
|
this.input.getAttribute &&
|
|
|
|
this.input.getAttribute('type') === 'password' &&
|
|
|
|
this.input.hasAttribute('value')
|
2020-03-07 11:45:13 +00:00
|
|
|
) {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.input.removeAttribute('value');
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
handleChange(e: Event) {
|
|
|
|
const { value, composing, isComposing } = e.target as any;
|
2020-05-06 07:05:08 +00:00
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2203
|
2020-10-16 06:24:14 +00:00
|
|
|
if (((isComposing || composing) && this.lazy) || this.stateValue === value) return;
|
2020-03-07 11:45:13 +00:00
|
|
|
this.setValue(value, this.clearPasswordValueAttribute);
|
2020-07-30 10:41:38 +00:00
|
|
|
resolveOnChange(this.input, e, this.triggerChange);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
handleKeyDown(e: KeyboardEvent) {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
this.$emit('pressEnter', e);
|
|
|
|
}
|
|
|
|
this.$emit('keydown', e);
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-04-07 09:19:18 +00:00
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
2020-09-29 14:11:06 +00:00
|
|
|
const { stateValue, isFocused } = this.$data;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-07 09:19:18 +00:00
|
|
|
const prefixCls = getPrefixCls('input', customizePrefixCls);
|
2020-06-27 06:02:30 +00:00
|
|
|
const addonAfter = getComponent(this, 'addonAfter');
|
|
|
|
const addonBefore = getComponent(this, 'addonBefore');
|
|
|
|
const suffix = getComponent(this, 'suffix');
|
|
|
|
const prefix = getComponent(this, 'prefix');
|
2020-10-16 06:24:14 +00:00
|
|
|
const props: any = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...this.$attrs,
|
|
|
|
...getOptionProps(this),
|
|
|
|
prefixCls,
|
|
|
|
inputType: 'input',
|
|
|
|
value: fixControlledValue(stateValue),
|
|
|
|
element: this.renderInput(prefixCls, { addonAfter, addonBefore }),
|
|
|
|
handleReset: this.handleReset,
|
|
|
|
addonAfter,
|
|
|
|
addonBefore,
|
|
|
|
suffix,
|
|
|
|
prefix,
|
2020-09-29 14:11:06 +00:00
|
|
|
isFocused,
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
2020-06-27 06:02:30 +00:00
|
|
|
return <ClearableLabeledInput {...props} ref={this.saveClearableInput} />;
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2020-10-12 10:46:05 +00:00
|
|
|
});
|