forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizableTextArea.tsx
161 lines (150 loc) · 4.33 KB
/
ResizableTextArea.tsx
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
import type { VNode } from 'vue';
import {
onMounted,
getCurrentInstance,
watch,
onBeforeUnmount,
ref,
nextTick,
defineComponent,
withDirectives,
} from 'vue';
import ResizeObserver from '../vc-resize-observer';
import classNames from '../_util/classNames';
import calculateNodeHeight from './calculateNodeHeight';
import raf from '../_util/raf';
import warning from '../_util/warning';
import antInput from '../_util/antInputDirective';
import omit from '../_util/omit';
import { textAreaProps } from './inputProps';
const RESIZE_STATUS_NONE = 0;
const RESIZE_STATUS_RESIZING = 1;
const RESIZE_STATUS_RESIZED = 2;
const ResizableTextArea = defineComponent({
compatConfig: { MODE: 3 },
name: 'ResizableTextArea',
inheritAttrs: false,
props: textAreaProps(),
setup(props, { attrs, emit, expose }) {
let nextFrameActionId: any;
let resizeFrameId: any;
const textAreaRef = ref();
const textareaStyles = ref({});
const resizeStatus = ref(RESIZE_STATUS_NONE);
onBeforeUnmount(() => {
raf.cancel(nextFrameActionId);
raf.cancel(resizeFrameId);
});
// https://github.com/ant-design/ant-design/issues/21870
const fixFirefoxAutoScroll = () => {
try {
if (document.activeElement === textAreaRef.value) {
const currentStart = textAreaRef.value.selectionStart;
const currentEnd = textAreaRef.value.selectionEnd;
textAreaRef.value.setSelectionRange(currentStart, currentEnd);
}
} catch (e) {
// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
};
const resizeTextarea = () => {
const autoSize = props.autoSize || props.autosize;
if (!autoSize || !textAreaRef.value) {
return;
}
const { minRows, maxRows } = autoSize;
textareaStyles.value = calculateNodeHeight(textAreaRef.value, false, minRows, maxRows);
resizeStatus.value = RESIZE_STATUS_RESIZING;
raf.cancel(resizeFrameId);
resizeFrameId = raf(() => {
resizeStatus.value = RESIZE_STATUS_RESIZED;
resizeFrameId = raf(() => {
resizeStatus.value = RESIZE_STATUS_NONE;
fixFirefoxAutoScroll();
});
});
};
const resizeOnNextFrame = () => {
raf.cancel(nextFrameActionId);
nextFrameActionId = raf(resizeTextarea);
};
const handleResize = (size: { width: number; height: number }) => {
if (resizeStatus.value !== RESIZE_STATUS_NONE) {
return;
}
emit('resize', size);
const autoSize = props.autoSize || props.autosize;
if (autoSize) {
resizeOnNextFrame();
}
};
warning(
props.autosize === undefined,
'Input.TextArea',
'autosize is deprecated, please use autoSize instead.',
);
const renderTextArea = () => {
const { prefixCls, autoSize, autosize, disabled } = props;
const otherProps = omit(props, [
'prefixCls',
'onPressEnter',
'autoSize',
'autosize',
'defaultValue',
'allowClear',
'type',
'lazy',
'maxlength',
'valueModifiers',
]);
const cls = classNames(prefixCls, attrs.class, {
[`${prefixCls}-disabled`]: disabled,
});
const style = [attrs.style, textareaStyles.value];
const textareaProps: any = {
...otherProps,
...attrs,
style,
class: cls,
};
if (!textareaProps.autofocus) {
delete textareaProps.autofocus;
}
if (textareaProps.rows === 0) {
delete textareaProps.rows;
}
return (
<ResizeObserver onResize={handleResize} disabled={!(autoSize || autosize)}>
{withDirectives((<textarea {...textareaProps} ref={textAreaRef} />) as VNode, [
[antInput],
])}
</ResizeObserver>
);
};
watch(
() => props.value,
() => {
nextTick(() => {
resizeTextarea();
});
},
);
onMounted(() => {
nextTick(() => {
resizeTextarea();
});
});
const instance = getCurrentInstance();
expose({
resizeTextarea,
textArea: textAreaRef,
instance,
});
return () => {
return renderTextArea();
};
},
});
export default ResizableTextArea;