@v-c/textarea 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ResizableTextArea.cjs +190 -124
- package/dist/ResizableTextArea.d.ts +3 -41
- package/dist/ResizableTextArea.js +191 -125
- package/dist/TextArea.cjs +93 -208
- package/dist/TextArea.d.ts +2 -2
- package/dist/TextArea.js +94 -209
- package/dist/calculateNodeHeight.cjs +1 -1
- package/dist/calculateNodeHeight.js +1 -1
- package/dist/interface.d.ts +21 -32
- package/package.json +2 -2
|
@@ -1,135 +1,201 @@
|
|
|
1
1
|
import calculateAutoSizeStyle from "./calculateNodeHeight.js";
|
|
2
|
-
import { computed, createVNode, defineComponent, mergeProps,
|
|
2
|
+
import { computed, createVNode, defineComponent, mergeProps, nextTick, onUnmounted, ref, shallowRef, watch } from "vue";
|
|
3
3
|
import ResizeObserver from "@v-c/resize-observer";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import { clsx } from "@v-c/util";
|
|
5
|
+
import { getAttrStyleAndClass } from "@v-c/util/dist/props-util";
|
|
6
6
|
import raf from "@v-c/util/dist/raf";
|
|
7
7
|
var RESIZE_START = 0;
|
|
8
8
|
var RESIZE_MEASURING = 1;
|
|
9
9
|
var RESIZE_STABLE = 2;
|
|
10
|
-
var ResizableTextArea_default = /* @__PURE__ */ defineComponent({
|
|
11
|
-
|
|
12
|
-
props
|
|
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
|
-
|
|
10
|
+
var ResizableTextArea_default = /* @__PURE__ */ defineComponent((props, { expose, attrs }) => {
|
|
11
|
+
const internalValue = ref(props?.value ?? props?.defaultValue ?? "");
|
|
12
|
+
watch(() => props.value, () => {
|
|
13
|
+
internalValue.value = props.value;
|
|
14
|
+
});
|
|
15
|
+
const mergedValue = computed(() => internalValue.value ?? "");
|
|
16
|
+
const onInternalChange = (e) => {
|
|
17
|
+
internalValue.value = e.target.value;
|
|
18
|
+
props?.onChange?.(e);
|
|
19
|
+
};
|
|
20
|
+
const textareaRef = shallowRef();
|
|
21
|
+
expose({ textArea: textareaRef });
|
|
22
|
+
const autoSizeData = computed(() => {
|
|
23
|
+
const autoSize = props.autoSize;
|
|
24
|
+
if (autoSize && typeof autoSize === "object") return [autoSize.minRows, autoSize.maxRows];
|
|
25
|
+
return [];
|
|
26
|
+
});
|
|
27
|
+
const minRows = computed(() => autoSizeData?.value?.[0]);
|
|
28
|
+
const maxRows = computed(() => autoSizeData?.value?.[1]);
|
|
29
|
+
const resizeState = ref(RESIZE_STABLE);
|
|
30
|
+
const autoSizeStyle = shallowRef({});
|
|
31
|
+
const startResize = () => {
|
|
32
|
+
resizeState.value = RESIZE_START;
|
|
33
|
+
};
|
|
34
|
+
const needAutoSize = computed(() => !!props.autoSize);
|
|
35
|
+
watch([
|
|
36
|
+
() => props.value,
|
|
37
|
+
minRows,
|
|
38
|
+
maxRows,
|
|
39
|
+
needAutoSize
|
|
40
|
+
], async () => {
|
|
41
|
+
await nextTick();
|
|
42
|
+
if (needAutoSize.value) startResize();
|
|
43
|
+
}, { immediate: true });
|
|
44
|
+
watch(resizeState, async () => {
|
|
45
|
+
await nextTick();
|
|
46
|
+
if (resizeState.value === RESIZE_START) resizeState.value = RESIZE_MEASURING;
|
|
47
|
+
else if (resizeState.value === RESIZE_MEASURING) {
|
|
48
|
+
const textareaStyles = calculateAutoSizeStyle(textareaRef.value, false, minRows.value, maxRows.value);
|
|
49
|
+
resizeState.value = RESIZE_STABLE;
|
|
50
|
+
autoSizeStyle.value = textareaStyles;
|
|
51
|
+
}
|
|
52
|
+
}, { immediate: true });
|
|
53
|
+
const resizeRafRef = shallowRef();
|
|
54
|
+
const cleanRaf = () => {
|
|
55
|
+
raf.cancel(resizeRafRef.value);
|
|
56
|
+
};
|
|
57
|
+
const onInternalResize = (size) => {
|
|
58
|
+
if (resizeState.value === RESIZE_STABLE) {
|
|
59
|
+
props?.onResize?.(size);
|
|
60
|
+
if (props.autoSize) {
|
|
61
|
+
cleanRaf();
|
|
62
|
+
resizeRafRef.value = raf(() => {
|
|
63
|
+
startResize();
|
|
64
|
+
});
|
|
48
65
|
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}, { immediate: true });
|
|
62
|
-
const needAutoSize = computed(() => !!props.autoSize);
|
|
63
|
-
const fixFirefoxAutoScroll = () => {
|
|
64
|
-
try {
|
|
65
|
-
if (document.activeElement === textareaRef.value) {
|
|
66
|
-
const { selectionStart, selectionEnd, scrollTop } = textareaRef.value;
|
|
67
|
-
textareaRef.value.setSelectionRange(selectionStart, selectionEnd);
|
|
68
|
-
textareaRef.value.scrollTop = scrollTop;
|
|
69
|
-
}
|
|
70
|
-
} catch (e) {}
|
|
71
|
-
};
|
|
72
|
-
const resizeState = ref(RESIZE_STABLE);
|
|
73
|
-
const autoSizeStyle = ref();
|
|
74
|
-
const startResize = () => {
|
|
75
|
-
resizeState.value = RESIZE_START;
|
|
76
|
-
if (process.env.NODE_ENV === "test") emit("internalAutoSize");
|
|
77
|
-
};
|
|
78
|
-
watch([
|
|
79
|
-
() => props.value,
|
|
80
|
-
minRows,
|
|
81
|
-
maxRows,
|
|
82
|
-
needAutoSize
|
|
83
|
-
], () => {
|
|
84
|
-
if (needAutoSize.value) startResize();
|
|
85
|
-
});
|
|
86
|
-
watch(resizeState, () => {
|
|
87
|
-
if (resizeState.value === RESIZE_START) resizeState.value = RESIZE_MEASURING;
|
|
88
|
-
else if (resizeState.value === RESIZE_MEASURING) {
|
|
89
|
-
const textareaStyles = calculateAutoSizeStyle(textareaRef.value, false, minRows.value, maxRows.value);
|
|
90
|
-
resizeState.value = RESIZE_STABLE;
|
|
91
|
-
autoSizeStyle.value = textareaStyles;
|
|
92
|
-
} else fixFirefoxAutoScroll();
|
|
93
|
-
});
|
|
94
|
-
const resizeRafRef = ref();
|
|
95
|
-
const cleanRaf = () => {
|
|
96
|
-
raf.cancel(resizeRafRef.value);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
onUnmounted(() => {
|
|
69
|
+
cleanRaf();
|
|
70
|
+
});
|
|
71
|
+
return () => {
|
|
72
|
+
const { autoSize, onResize, prefixCls, disabled, className } = props;
|
|
73
|
+
const { style, restAttrs } = getAttrStyleAndClass(attrs, { omits: ["onKeydown"] });
|
|
74
|
+
const mergedAutoSizeStyle = needAutoSize.value ? autoSizeStyle.value : null;
|
|
75
|
+
const mergedStyle = {
|
|
76
|
+
...style,
|
|
77
|
+
...mergedAutoSizeStyle
|
|
97
78
|
};
|
|
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
|
-
|
|
79
|
+
if (resizeState.value === RESIZE_START || resizeState.value === RESIZE_MEASURING) {
|
|
80
|
+
mergedStyle.overflowY = "hidden";
|
|
81
|
+
mergedStyle.overflowX = "hidden";
|
|
82
|
+
}
|
|
83
|
+
return createVNode(ResizeObserver, {
|
|
84
|
+
"onResize": onInternalResize,
|
|
85
|
+
"disabled": !(autoSize || onResize)
|
|
86
|
+
}, { default: () => [createVNode("textarea", mergeProps(restAttrs, {
|
|
87
|
+
"ref": textareaRef,
|
|
88
|
+
"style": mergedStyle,
|
|
89
|
+
"class": clsx(prefixCls, className, { [`${prefixCls}-disabled`]: disabled }),
|
|
90
|
+
"disabled": disabled,
|
|
91
|
+
"value": mergedValue.value,
|
|
92
|
+
"onChange": onInternalChange
|
|
93
|
+
}), null)] });
|
|
94
|
+
};
|
|
95
|
+
}, {
|
|
96
|
+
props: {
|
|
97
|
+
value: {
|
|
98
|
+
type: null,
|
|
99
|
+
required: false,
|
|
100
|
+
default: void 0
|
|
101
|
+
},
|
|
102
|
+
defaultValue: {
|
|
103
|
+
type: null,
|
|
104
|
+
required: false,
|
|
105
|
+
default: void 0
|
|
106
|
+
},
|
|
107
|
+
prefixCls: {
|
|
108
|
+
type: String,
|
|
109
|
+
required: false,
|
|
110
|
+
default: void 0
|
|
111
|
+
},
|
|
112
|
+
disabled: {
|
|
113
|
+
type: Boolean,
|
|
114
|
+
required: false,
|
|
115
|
+
default: void 0
|
|
116
|
+
},
|
|
117
|
+
className: {
|
|
118
|
+
type: String,
|
|
119
|
+
required: false,
|
|
120
|
+
default: void 0
|
|
121
|
+
},
|
|
122
|
+
autoSize: {
|
|
123
|
+
type: [Boolean, Object],
|
|
124
|
+
required: false,
|
|
125
|
+
default: void 0
|
|
126
|
+
},
|
|
127
|
+
onPressEnter: {
|
|
128
|
+
type: Function,
|
|
129
|
+
required: false,
|
|
130
|
+
default: void 0
|
|
131
|
+
},
|
|
132
|
+
onResize: {
|
|
133
|
+
type: Function,
|
|
134
|
+
required: false,
|
|
135
|
+
default: void 0
|
|
136
|
+
},
|
|
137
|
+
classNames: {
|
|
138
|
+
type: Object,
|
|
139
|
+
required: false,
|
|
140
|
+
default: void 0
|
|
141
|
+
},
|
|
142
|
+
styles: {
|
|
143
|
+
type: Object,
|
|
144
|
+
required: false,
|
|
145
|
+
default: void 0
|
|
146
|
+
},
|
|
147
|
+
allowClear: {
|
|
148
|
+
type: [Boolean, Object],
|
|
149
|
+
required: false,
|
|
150
|
+
default: void 0
|
|
151
|
+
},
|
|
152
|
+
suffix: {
|
|
153
|
+
type: null,
|
|
154
|
+
required: false,
|
|
155
|
+
default: void 0
|
|
156
|
+
},
|
|
157
|
+
showCount: {
|
|
158
|
+
type: [Boolean, Object],
|
|
159
|
+
required: false,
|
|
160
|
+
default: void 0
|
|
161
|
+
},
|
|
162
|
+
count: {
|
|
163
|
+
type: null,
|
|
164
|
+
required: false,
|
|
165
|
+
default: void 0
|
|
166
|
+
},
|
|
167
|
+
onClear: {
|
|
168
|
+
type: Function,
|
|
169
|
+
required: false,
|
|
170
|
+
default: void 0
|
|
171
|
+
},
|
|
172
|
+
onChange: {
|
|
173
|
+
type: Function,
|
|
174
|
+
required: false,
|
|
175
|
+
default: void 0
|
|
176
|
+
},
|
|
177
|
+
maxLength: {
|
|
178
|
+
type: Number,
|
|
179
|
+
required: false,
|
|
180
|
+
default: void 0
|
|
181
|
+
},
|
|
182
|
+
minLength: {
|
|
183
|
+
type: Number,
|
|
184
|
+
required: false,
|
|
185
|
+
default: void 0
|
|
186
|
+
},
|
|
187
|
+
hidden: {
|
|
188
|
+
type: Boolean,
|
|
189
|
+
required: false,
|
|
190
|
+
default: void 0
|
|
191
|
+
},
|
|
192
|
+
readOnly: {
|
|
193
|
+
type: Boolean,
|
|
194
|
+
required: false,
|
|
195
|
+
default: void 0
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
name: "ResizableTextArea",
|
|
199
|
+
inheritAttrs: false
|
|
134
200
|
});
|
|
135
201
|
export { ResizableTextArea_default as default };
|