2020-06-27 06:02:30 +00:00
|
|
|
import { inject } from 'vue';
|
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-06-27 06:02:30 +00:00
|
|
|
import { hasProp, 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';
|
2020-07-07 05:57:11 +00:00
|
|
|
import syncWatch from '../_util/syncWatch';
|
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,
|
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
|
|
|
},
|
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 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
|
|
|
},
|
|
|
|
watch: {
|
2020-07-07 05:57:11 +00:00
|
|
|
value: syncWatch(function(val) {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.stateValue = val;
|
2020-07-07 05:57:11 +00:00
|
|
|
}),
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
2020-07-31 04:43:01 +00:00
|
|
|
mounted() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
if (this.autofocus) {
|
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
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;
|
|
|
|
} else {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.$forceUpdate();
|
2019-01-03 12:51:56 +00:00
|
|
|
}
|
2020-07-30 10:41:38 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
callback && callback();
|
|
|
|
});
|
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-07-30 10:41:38 +00:00
|
|
|
triggerChange(e) {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.$emit('update:value', e.target.value);
|
2020-03-07 11:45:13 +00:00
|
|
|
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, () => {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.resizableTextArea.resizeTextarea();
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
2020-07-30 10:41:38 +00:00
|
|
|
resolveOnChange(this.resizableTextArea.textArea, e, this.triggerChange);
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.resizableTextArea.textArea.focus();
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.resizableTextArea.textArea.blur();
|
|
|
|
},
|
|
|
|
saveTextArea(resizableTextArea) {
|
|
|
|
this.resizableTextArea = resizableTextArea;
|
|
|
|
},
|
|
|
|
|
|
|
|
saveClearableInput(clearableInput) {
|
|
|
|
this.clearableInput = clearableInput;
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
handleReset(e) {
|
|
|
|
this.setValue('', () => {
|
2020-06-27 06:02:30 +00:00
|
|
|
this.resizableTextArea.renderTextArea();
|
2020-03-07 11:45:13 +00:00
|
|
|
this.focus();
|
|
|
|
});
|
2020-07-30 10:41:38 +00:00
|
|
|
resolveOnChange(this.resizableTextArea.textArea, e, this.triggerChange);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
renderTextArea(prefixCls) {
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
const resizeProps = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...props,
|
|
|
|
...this.$attrs,
|
|
|
|
prefixCls,
|
|
|
|
onInput: this.handleChange,
|
|
|
|
onKeydown: this.handleKeyDown,
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
2020-06-27 06:02:30 +00:00
|
|
|
return <ResizableTextArea {...resizeProps} ref={this.saveTextArea} />;
|
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 = {
|
2020-06-27 06:02:30 +00:00
|
|
|
...getOptionProps(this),
|
|
|
|
...this.$attrs,
|
|
|
|
prefixCls,
|
|
|
|
inputType: 'text',
|
|
|
|
value: fixControlledValue(stateValue),
|
|
|
|
element: this.renderTextArea(prefixCls),
|
|
|
|
handleReset: this.handleReset,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-27 06:02:30 +00:00
|
|
|
return <ClearableLabeledInput {...props} ref={this.saveClearableInput} />;
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|