2019-01-12 03:33:27 +00:00
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import TextArea from './TextArea';
|
|
|
|
|
import omit from 'omit.js';
|
|
|
|
|
import inputProps from './inputProps';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
import { hasProp, getComponentFromProp, getListeners, 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';
|
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,
|
2017-12-07 02:28:17 +00:00
|
|
|
|
model: {
|
|
|
|
|
prop: 'value',
|
|
|
|
|
event: 'change.value',
|
|
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
|
props: {
|
|
|
|
|
...inputProps,
|
|
|
|
|
},
|
2019-04-07 09:19:18 +00:00
|
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
|
configProvider: { default: () => 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: {
|
|
|
|
|
value(val) {
|
2019-09-11 10:11:48 +00:00
|
|
|
|
this.stateValue = val;
|
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(() => {
|
|
|
|
|
if (this.autoFocus) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.focus();
|
2018-06-07 06:40:10 +00:00
|
|
|
|
}
|
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-03-07 11:45:13 +00:00
|
|
|
|
beforeDestroy() {
|
|
|
|
|
if (this.removePasswordTimeout) {
|
|
|
|
|
clearTimeout(this.removePasswordTimeout);
|
|
|
|
|
}
|
|
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
focus() {
|
|
|
|
|
this.$refs.input.focus();
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
blur() {
|
|
|
|
|
this.$refs.input.blur();
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
select() {
|
|
|
|
|
this.$refs.input.select();
|
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-05-08 06:03:07 +00:00
|
|
|
|
// 不在严格受控
|
|
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2207,modal 是 新 new 实例,更新队列和当前不在同一个更新队列中
|
|
|
|
|
// this.$forceUpdate();
|
2019-04-07 09:19:18 +00:00
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
},
|
|
|
|
|
onChange(e) {
|
|
|
|
|
this.$emit('change.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-03-07 11:45:13 +00:00
|
|
|
|
resolveOnChange(this.$refs.input, e, this.onChange);
|
2019-04-07 09:19:18 +00:00
|
|
|
|
},
|
|
|
|
|
renderInput(prefixCls) {
|
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
|
|
|
|
'value',
|
|
|
|
|
'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',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
]);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { stateValue, handleKeyDown, handleChange, size, disabled } = this;
|
2018-02-27 06:08:15 +00:00
|
|
|
|
const inputProps = {
|
2019-11-15 08:56:28 +00:00
|
|
|
|
directives: [{ name: 'ant-input' }],
|
2018-02-27 06:08:15 +00:00
|
|
|
|
domProps: {
|
2019-09-11 10:11:48 +00:00
|
|
|
|
value: fixControlledValue(stateValue),
|
2018-02-27 06:08:15 +00:00
|
|
|
|
},
|
2017-12-07 10:33:33 +00:00
|
|
|
|
attrs: { ...otherProps, ...this.$attrs },
|
2018-02-27 06:08:15 +00:00
|
|
|
|
on: {
|
2020-01-19 08:58:38 +00:00
|
|
|
|
...getListeners(this),
|
2018-02-27 06:08:15 +00:00
|
|
|
|
keydown: handleKeyDown,
|
|
|
|
|
input: handleChange,
|
2019-01-07 12:56:32 +00:00
|
|
|
|
change: noop,
|
2018-02-27 06:08:15 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
class: getInputClassName(prefixCls, size, disabled),
|
2018-02-27 06:08:15 +00:00
|
|
|
|
ref: 'input',
|
2019-09-11 10:11:48 +00:00
|
|
|
|
key: 'ant-input',
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
return <input {...inputProps} />;
|
|
|
|
|
},
|
|
|
|
|
clearPasswordValueAttribute() {
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
|
|
|
this.removePasswordTimeout = setTimeout(() => {
|
|
|
|
|
if (
|
|
|
|
|
this.$refs.input &&
|
2020-03-15 05:12:47 +00:00
|
|
|
|
this.$refs.input.getAttribute &&
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.$refs.input.getAttribute('type') === 'password' &&
|
|
|
|
|
this.$refs.input.hasAttribute('value')
|
|
|
|
|
) {
|
|
|
|
|
this.$refs.input.removeAttribute('value');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
handleChange(e) {
|
2020-05-08 06:24:44 +00:00
|
|
|
|
if (e.inputType === 'insertCompositionText') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { value, composing } = e.target;
|
2020-05-06 07:05:08 +00:00
|
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2203
|
|
|
|
|
if ((composing && this.lazy) || this.stateValue === value) return;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.setValue(value, this.clearPasswordValueAttribute);
|
|
|
|
|
resolveOnChange(this.$refs.input, e, this.onChange);
|
|
|
|
|
},
|
|
|
|
|
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() {
|
2017-12-07 04:31:12 +00:00
|
|
|
|
if (this.$props.type === 'textarea') {
|
|
|
|
|
const textareaProps = {
|
|
|
|
|
props: this.$props,
|
|
|
|
|
attrs: this.$attrs,
|
|
|
|
|
on: {
|
2020-01-19 08:58:38 +00:00
|
|
|
|
...getListeners(this),
|
2019-11-15 08:56:28 +00:00
|
|
|
|
input: this.handleChange,
|
2018-02-27 06:08:15 +00:00
|
|
|
|
keydown: this.handleKeyDown,
|
2019-11-15 08:56:28 +00:00
|
|
|
|
change: noop,
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
|
|
|
|
return <TextArea {...textareaProps} ref="input" />;
|
2017-12-07 04:31:12 +00:00
|
|
|
|
}
|
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-03-07 11:45:13 +00:00
|
|
|
|
const addonAfter = getComponentFromProp(this, 'addonAfter');
|
|
|
|
|
const addonBefore = getComponentFromProp(this, 'addonBefore');
|
|
|
|
|
const suffix = getComponentFromProp(this, 'suffix');
|
|
|
|
|
const prefix = getComponentFromProp(this, 'prefix');
|
|
|
|
|
const props = {
|
|
|
|
|
props: {
|
|
|
|
|
...getOptionProps(this),
|
|
|
|
|
prefixCls,
|
|
|
|
|
inputType: 'input',
|
|
|
|
|
value: fixControlledValue(stateValue),
|
|
|
|
|
element: this.renderInput(prefixCls),
|
|
|
|
|
handleReset: this.handleReset,
|
|
|
|
|
addonAfter,
|
|
|
|
|
addonBefore,
|
|
|
|
|
suffix,
|
|
|
|
|
prefix,
|
|
|
|
|
},
|
|
|
|
|
on: getListeners(this),
|
|
|
|
|
};
|
|
|
|
|
return <ClearableLabeledInput {...props} />;
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|