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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import omit from 'omit.js';
import ResizeObserver from 'resize-observer-polyfill';
import inputProps from './inputProps';
import calculateNodeHeight from './calculateNodeHeight';
import hasProp from '../_util/props-util';
function onNextFrame(cb) {
if (window.requestAnimationFrame) {
return window.requestAnimationFrame(cb);
}
return window.setTimeout(cb, 1);
}
function clearNextFrameAction(nextFrameId) {
if (window.cancelAnimationFrame) {
window.cancelAnimationFrame(nextFrameId);
} else {
window.clearTimeout(nextFrameId);
}
}
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
function noop() {}
export default {
name: 'ATextarea',
model: {
prop: 'value',
event: 'change.value'
},
props: _extends({}, inputProps, {
autosize: [Object, Boolean]
}),
data: function data() {
var _$props = this.$props,
value = _$props.value,
defaultValue = _$props.defaultValue;
return {
stateValue: fixControlledValue(!hasProp(this, 'value') ? defaultValue : value),
nextFrameActionId: undefined,
textareaStyles: {}
};
},
computed: {},
watch: {
value: function value(val) {
var _this = this;
this.$nextTick(function () {
_this.resizeOnNextFrame();
});
this.stateValue = fixControlledValue(val);
},
autosize: function autosize(val) {
if (!val && this.$refs.textArea) {
this.textareaStyles = omit(this.textareaStyles, ['overflowY']);
}
}
},
mounted: function mounted() {
var _this2 = this;
this.$nextTick(function () {
_this2.resizeTextarea();
_this2.updateResizeObserverHook();
if (_this2.autoFocus) {
_this2.focus();
}
});
},
updated: function updated() {
this.updateResizeObserverHook();
},
beforeDestroy: function beforeDestroy() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
}
},
methods: {
resizeOnNextFrame: function resizeOnNextFrame() {
if (this.nextFrameActionId) {
clearNextFrameAction(this.nextFrameActionId);
}
this.nextFrameActionId = onNextFrame(this.resizeTextarea);
},
// We will update hooks if `autosize` prop change
updateResizeObserverHook: function updateResizeObserverHook() {
if (!this.resizeObserver && this.$props.autosize) {
// Add resize observer
this.resizeObserver = new ResizeObserver(this.resizeOnNextFrame);
this.resizeObserver.observe(this.$refs.textArea);
} else if (this.resizeObserver && !this.$props.autosize) {
// Remove resize observer
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
},
handleKeyDown: function handleKeyDown(e) {
if (e.keyCode === 13) {
this.$emit('pressEnter', e);
}
this.$emit('keydown', e);
},
resizeTextarea: function resizeTextarea() {
var autosize = this.$props.autosize;
if (!autosize || !this.$refs.textArea) {
return;
}
var minRows = autosize ? autosize.minRows : null;
var maxRows = autosize ? autosize.maxRows : null;
var textareaStyles = calculateNodeHeight(this.$refs.textArea, false, minRows, maxRows);
this.textareaStyles = textareaStyles;
},
getTextAreaClassName: function getTextAreaClassName() {
var _ref;
var _$props2 = this.$props,
prefixCls = _$props2.prefixCls,
disabled = _$props2.disabled;
return _ref = {}, _defineProperty(_ref, prefixCls, true), _defineProperty(_ref, prefixCls + '-disabled', disabled), _ref;
},
handleTextareaChange: function handleTextareaChange(e) {
if (!hasProp(this, 'value')) {
this.stateValue = e.target.value;
this.resizeTextarea();
} else {
this.$forceUpdate();
}
if (!e.target.composing) {
this.$emit('change.value', e.target.value);
}
this.$emit('change', e);
this.$emit('input', e);
},
focus: function focus() {
this.$refs.textArea.focus();
},
blur: function blur() {
this.$refs.textArea.blur();
}
},
render: function render() {
var h = arguments[0];
var stateValue = this.stateValue,
getTextAreaClassName = this.getTextAreaClassName,
handleKeyDown = this.handleKeyDown,
handleTextareaChange = this.handleTextareaChange,
textareaStyles = this.textareaStyles,
$attrs = this.$attrs,
$listeners = this.$listeners;
var otherProps = omit(this.$props, ['prefixCls', 'autosize', 'type', 'value', 'defaultValue']);
var textareaProps = {
attrs: _extends({}, otherProps, $attrs),
on: _extends({}, $listeners, {
keydown: handleKeyDown,
input: handleTextareaChange,
change: noop
})
};
if ($listeners['change.value']) {
textareaProps.directives = [{ name: 'ant-input' }];
}
return h('textarea', _mergeJSXProps([textareaProps, {
domProps: {
'value': stateValue
},
'class': getTextAreaClassName(),
style: textareaStyles,
ref: 'textArea'
}]));
}
};