-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
TextArea.jsx
134 lines (127 loc) · 3.58 KB
/
TextArea.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import ClearableLabeledInput from './ClearableLabeledInput';
import ResizableTextArea from './ResizableTextArea';
import inputProps from './inputProps';
import hasProp, { getListeners, getOptionProps } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
import { fixControlledValue, resolveOnChange } from './Input';
import PropTypes from '../_util/vue-types';
const TextAreaProps = {
...inputProps,
autosize: PropTypes.oneOfType([Object, Boolean]),
autoSize: PropTypes.oneOfType([Object, Boolean]),
};
export default {
name: 'ATextarea',
inheritAttrs: false,
model: {
prop: 'value',
event: 'change.value',
},
props: {
...TextAreaProps,
},
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
data() {
const value = typeof this.value === 'undefined' ? this.defaultValue : this.value;
return {
stateValue: typeof value === 'undefined' ? '' : value,
};
},
computed: {},
watch: {
value(val) {
this.stateValue = val;
},
},
mounted() {
this.$nextTick(() => {
if (this.autoFocus) {
this.focus();
}
});
},
methods: {
setValue(value, callback) {
if (!hasProp(this, 'value')) {
this.stateValue = value;
this.$nextTick(() => {
callback && callback();
});
} else {
// 不在严格受控
// https://github.com/vueComponent/ant-design-vue/issues/2207,modal 是 新 new 实例,更新队列和当前不在同一个更新队列中
// this.$forceUpdate();
}
},
handleKeyDown(e) {
if (e.keyCode === 13) {
this.$emit('pressEnter', e);
}
this.$emit('keydown', e);
},
onChange(e) {
this.$emit('change.value', e.target.value);
this.$emit('change', e);
this.$emit('input', e);
},
handleChange(e) {
if (e.inputType === 'insertCompositionText') {
return;
}
const { value, composing } = e.target;
if ((composing && this.lazy) || this.stateValue === value) return;
this.setValue(e.target.value, () => {
this.$refs.resizableTextArea.resizeTextarea();
});
resolveOnChange(this.$refs.resizableTextArea.$refs.textArea, e, this.onChange);
},
focus() {
this.$refs.resizableTextArea.$refs.textArea.focus();
},
blur() {
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" />;
},
},
render() {
const { stateValue, prefixCls: customizePrefixCls } = this;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('input', customizePrefixCls);
const props = {
props: {
...getOptionProps(this),
prefixCls,
inputType: 'text',
value: fixControlledValue(stateValue),
element: this.renderTextArea(prefixCls),
handleReset: this.handleReset,
},
on: getListeners(this),
};
return <ClearableLabeledInput {...props} />;
},
};