-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathResizableTextArea.jsx
136 lines (131 loc) · 3.86 KB
/
ResizableTextArea.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
135
136
import ResizeObserver from '../vc-resize-observer';
import omit from 'omit.js';
import classNames from 'classnames';
import calculateNodeHeight from './calculateNodeHeight';
import raf from '../_util/raf';
import warning from '../_util/warning';
import BaseMixin from '../_util/BaseMixin';
import inputProps from './inputProps';
import PropTypes from '../_util/vue-types';
import { getOptionProps, getListeners } from '../_util/props-util';
const TextAreaProps = {
...inputProps,
autosize: PropTypes.oneOfType([Object, Boolean]),
autoSize: PropTypes.oneOfType([Object, Boolean]),
};
const ResizableTextArea = {
name: 'ResizableTextArea',
props: TextAreaProps,
data() {
return {
textareaStyles: {},
resizing: false,
};
},
mixins: [BaseMixin],
mounted() {
this.resizeTextarea();
},
beforeDestroy() {
raf.cancel(this.nextFrameActionId);
raf.cancel(this.resizeFrameId);
},
watch: {
value() {
this.$nextTick(() => {
this.resizeTextarea();
});
},
},
methods: {
resizeOnNextFrame() {
raf.cancel(this.nextFrameActionId);
this.nextFrameActionId = raf(this.resizeTextarea);
},
resizeTextarea() {
const autoSize = this.$props.autoSize || this.$props.autosize;
if (!autoSize || !this.$refs.textArea) {
return;
}
const { minRows, maxRows } = autoSize;
const textareaStyles = calculateNodeHeight(this.$refs.textArea, false, minRows, maxRows);
setTimeout(() => {
this.setState({ textareaStyles, resizing: true }, () => {
raf.cancel(this.resizeFrameId);
this.resizeFrameId = raf(() => {
this.setState({ resizing: false });
this.fixFirefoxAutoScroll();
});
});
}, 0);
},
// https://github.com/ant-design/ant-design/issues/21870
fixFirefoxAutoScroll() {
try {
if (document.activeElement === this.$refs.textArea) {
const currentStart = this.$refs.textArea.selectionStart;
const currentEnd = this.$refs.textArea.selectionEnd;
this.$refs.textArea.setSelectionRange(currentStart, currentEnd);
}
} catch (e) {
// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
},
renderTextArea() {
const props = getOptionProps(this);
const { prefixCls, autoSize, autosize, disabled } = props;
const { textareaStyles, resizing } = this.$data;
warning(
autosize === undefined,
'Input.TextArea',
'autosize is deprecated, please use autoSize instead.',
);
const otherProps = omit(props, [
'prefixCls',
'autoSize',
'autosize',
'defaultValue',
'allowClear',
'type',
'lazy',
'value',
]);
const cls = classNames(prefixCls, {
[`${prefixCls}-disabled`]: disabled,
});
const domProps = {};
// Fix https://github.com/ant-design/ant-design/issues/6776
// Make sure it could be reset when using form.getFieldDecorator
if ('value' in props) {
domProps.value = props.value || '';
}
const style = {
...textareaStyles,
...(resizing ? { overflowX: 'hidden', overflowY: 'hidden' } : null),
};
const textareaProps = {
attrs: otherProps,
domProps,
style,
class: cls,
on: omit(getListeners(this), 'pressEnter'),
directives: [
{
name: 'ant-input',
},
],
};
return (
<ResizeObserver onResize={this.resizeOnNextFrame} disabled={!(autoSize || autosize)}>
<textarea {...textareaProps} ref="textArea" />
</ResizeObserver>
);
},
},
render() {
return this.renderTextArea();
},
};
export default ResizableTextArea;