2020-06-27 06:02:30 +00:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
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';
|
2019-04-07 09:19:18 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
2020-07-07 05:57:11 +00:00
|
|
|
import syncWatch from '../_util/syncWatch';
|
2017-12-06 10:54:20 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function noop() {}
|
2019-01-07 12:56:32 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
export function fixControlledValue(value) {
|
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-03-07 11:45:13 +00:00
|
|
|
export function resolveOnChange(target, e, onChange) {
|
|
|
|
if (onChange) {
|
|
|
|
let event = e;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInputClassName(prefixCls, size, disabled) {
|
|
|
|
return classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
});
|
2019-04-07 09:19:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 10:54:20 +00:00
|
|
|
export default {
|
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 {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
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,
|
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-07-07 05:57:11 +00:00
|
|
|
value: syncWatch(function(val) {
|
2019-09-11 10:11:48 +00:00
|
|
|
this.stateValue = val;
|
2020-07-07 05:57:11 +00:00
|
|
|
}),
|
2019-02-01 09:23:00 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-06-07 06:40:10 +00:00
|
|
|
this.$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: {
|
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();
|
|
|
|
},
|
|
|
|
|
|
|
|
saveClearableInput(input) {
|
|
|
|
this.clearableInput = input;
|
|
|
|
},
|
|
|
|
|
|
|
|
saveInput(input) {
|
|
|
|
this.input = input;
|
2018-12-09 03:43:27 +00:00
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
setValue(value, callback) {
|
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;
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
callback && callback();
|
|
|
|
});
|
2019-04-07 09:19:18 +00:00
|
|
|
} else {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.$forceUpdate();
|
2019-04-07 09:19:18 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
onChange(e) {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.$emit('update:value', e.target.value);
|
2019-04-07 09:19:18 +00:00
|
|
|
this.$emit('change', e);
|
|
|
|
this.$emit('input', e);
|
|
|
|
},
|
|
|
|
handleReset(e) {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.setValue('', () => {
|
2019-10-17 11:26:58 +00:00
|
|
|
this.focus();
|
|
|
|
});
|
2020-06-27 06:02:30 +00:00
|
|
|
resolveOnChange(this.input, e, this.onChange);
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
2020-06-27 06:02:30 +00:00
|
|
|
renderInput(prefixCls, { addonBefore, addonAfter }) {
|
2017-12-07 04:31:12 +00:00
|
|
|
const otherProps = omit(this.$props, [
|
|
|
|
'prefixCls',
|
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',
|
|
|
|
'inputType',
|
2020-04-15 12:38:58 +00:00
|
|
|
'className',
|
2020-06-27 06:02:30 +00:00
|
|
|
'inputPrefixCls',
|
|
|
|
'loading',
|
2019-01-12 03:33:27 +00:00
|
|
|
]);
|
2020-06-27 06:02:30 +00:00
|
|
|
const { handleKeyDown, handleChange, size, disabled, $attrs } = this;
|
|
|
|
|
2018-02-27 06:08:15 +00:00
|
|
|
const inputProps = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...otherProps,
|
|
|
|
...$attrs,
|
|
|
|
onKeydown: handleKeyDown,
|
|
|
|
class: classNames(getInputClassName(prefixCls, size, disabled), {
|
|
|
|
[$attrs.class]: $attrs.class && !addonBefore && !addonAfter,
|
|
|
|
}),
|
|
|
|
ref: this.saveInput,
|
2019-09-11 10:11:48 +00:00
|
|
|
key: 'ant-input',
|
2020-07-06 09:44:52 +00:00
|
|
|
onInput: handleChange,
|
|
|
|
onChange: noop,
|
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-07-06 09:44:52 +00:00
|
|
|
return <input {...inputProps} />;
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleChange(e) {
|
|
|
|
const { value, composing } = e.target;
|
2020-05-06 07:05:08 +00:00
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2203
|
2020-05-08 09:09:41 +00:00
|
|
|
if (((e.isComposing || composing) && this.lazy) || this.stateValue === value) return;
|
2020-03-07 11:45:13 +00:00
|
|
|
this.setValue(value, this.clearPasswordValueAttribute);
|
2020-06-27 06:02:30 +00:00
|
|
|
resolveOnChange(this.input, e, this.onChange);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
handleKeyDown(e) {
|
|
|
|
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() {
|
2020-06-27 06:02:30 +00:00
|
|
|
// if (this.$props.type === 'textarea') {
|
|
|
|
// const textareaProps = {
|
|
|
|
// ...this.$props,
|
|
|
|
// ...this.$attrs,
|
|
|
|
// onInput: this.handleChange,
|
|
|
|
// onKeydown: this.handleKeyDown,
|
|
|
|
// onChange: noop,
|
|
|
|
// };
|
|
|
|
// return <TextArea {...textareaProps} ref="input" />;
|
|
|
|
// }
|
2019-04-07 09:19:18 +00:00
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
2020-03-07 11:45:13 +00:00
|
|
|
const { stateValue } = 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-03-07 11:45:13 +00:00
|
|
|
const props = {
|
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-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
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|