2020-03-07 11:45:13 +00:00
|
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
|
|
|
|
import ResizableTextArea from './ResizableTextArea';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import inputProps from './inputProps';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
import hasProp, { 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 { fixControlledValue, resolveOnChange } from './Input';
|
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2018-03-19 02:16:27 +00:00
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const TextAreaProps = {
|
|
|
|
|
...inputProps,
|
|
|
|
|
autosize: PropTypes.oneOfType([Object, Boolean]),
|
|
|
|
|
autoSize: PropTypes.oneOfType([Object, Boolean]),
|
|
|
|
|
};
|
2017-12-07 04:31:12 +00:00
|
|
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
|
name: 'ATextarea',
|
2020-01-18 09:06:09 +00:00
|
|
|
|
inheritAttrs: false,
|
2017-12-07 04:31:12 +00:00
|
|
|
|
model: {
|
|
|
|
|
prop: 'value',
|
|
|
|
|
event: 'change.value',
|
|
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
|
props: {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
...TextAreaProps,
|
2019-02-01 09:23:00 +00:00
|
|
|
|
},
|
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 value = typeof this.value === 'undefined' ? this.defaultValue : this.value;
|
2017-12-07 04:31:12 +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-07 04:31:12 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
computed: {},
|
2017-12-07 04:31:12 +00:00
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
value(val) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.stateValue = val;
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
mounted() {
|
2018-06-10 02:26:03 +00:00
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (this.autoFocus) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.focus();
|
2018-06-10 02:26:03 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
setValue(value, callback) {
|
|
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
|
this.stateValue = value;
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
callback && callback();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2020-05-08 06:03:07 +00:00
|
|
|
|
// 不在严格受控
|
|
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/2207,modal 是 新 new 实例,更新队列和当前不在同一个更新队列中
|
|
|
|
|
// this.$forceUpdate();
|
2019-01-03 12:51:56 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
handleKeyDown(e) {
|
2017-12-07 04:31:12 +00:00
|
|
|
|
if (e.keyCode === 13) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('pressEnter', e);
|
2017-12-07 04:31:12 +00:00
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
this.$emit('keydown', e);
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
onChange(e) {
|
|
|
|
|
this.$emit('change.value', e.target.value);
|
|
|
|
|
this.$emit('change', e);
|
|
|
|
|
this.$emit('input', e);
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
handleChange(e) {
|
2019-11-15 10:21:50 +00:00
|
|
|
|
const { value, composing } = e.target;
|
2020-05-08 09:09:41 +00:00
|
|
|
|
if (((e.isComposing || composing) && this.lazy) || this.stateValue === value) return;
|
2019-11-15 10:21:50 +00:00
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.setValue(e.target.value, () => {
|
|
|
|
|
this.$refs.resizableTextArea.resizeTextarea();
|
|
|
|
|
});
|
|
|
|
|
resolveOnChange(this.$refs.resizableTextArea.$refs.textArea, e, this.onChange);
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
focus() {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.$refs.resizableTextArea.$refs.textArea.focus();
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
blur() {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
this.$refs.resizableTextArea.$refs.textArea.blur();
|
|
|
|
|
},
|
|
|
|
|
handleReset(e) {
|
|
|
|
|
this.setValue('', () => {
|
|
|
|
|
this.$refs.resizableTextArea.renderTextArea();
|
|
|
|
|
this.focus();
|
|
|
|
|
});
|
|
|
|
|
resolveOnChange(this.$refs.resizableTextArea.$refs.textArea, e, this.onChange);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
renderTextArea(prefixCls) {
|
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
|
const resizeProps = {
|
|
|
|
|
props: {
|
|
|
|
|
...props,
|
|
|
|
|
prefixCls,
|
|
|
|
|
},
|
|
|
|
|
on: {
|
|
|
|
|
...getListeners(this),
|
|
|
|
|
input: this.handleChange,
|
|
|
|
|
keydown: this.handleKeyDown,
|
|
|
|
|
},
|
|
|
|
|
attrs: this.$attrs,
|
|
|
|
|
};
|
|
|
|
|
return <ResizableTextArea {...resizeProps} ref="resizableTextArea" />;
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
render() {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { stateValue, prefixCls: customizePrefixCls } = this;
|
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 props = {
|
|
|
|
|
props: {
|
|
|
|
|
...getOptionProps(this),
|
|
|
|
|
prefixCls,
|
|
|
|
|
inputType: 'text',
|
|
|
|
|
value: fixControlledValue(stateValue),
|
|
|
|
|
element: this.renderTextArea(prefixCls),
|
|
|
|
|
handleReset: this.handleReset,
|
2018-02-28 11:07:04 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
on: getListeners(this),
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
return <ClearableLabeledInput {...props} />;
|
2017-12-07 04:31:12 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|