@v-c/textarea 0.0.6 → 0.0.8

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/TextArea.cjs CHANGED
@@ -5,209 +5,170 @@ let vue = require("vue");
5
5
  let __v_c_util = require("@v-c/util");
6
6
  let __v_c_util_dist_omit = require("@v-c/util/dist/omit");
7
7
  __v_c_util_dist_omit = require_rolldown_runtime.__toESM(__v_c_util_dist_omit);
8
+ let __v_c_util_dist_props_util = require("@v-c/util/dist/props-util");
8
9
  let __v_c_input = require("@v-c/input");
9
- var defaults = { prefixCls: "vc-textarea" };
10
- var TextArea_default = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs, expose, slots, emit }) => {
11
- const stateValue = (0, vue.shallowRef)(props.value ?? props.defaultValue);
12
- (0, vue.watch)(() => props.value, (newValue) => {
13
- if ("value" in props || {}) stateValue.value = newValue;
10
+ let __v_c_util_dist_KeyCode = require("@v-c/util/dist/KeyCode");
11
+ var TextArea = /* @__PURE__ */ (0, vue.defineComponent)((props, { expose, attrs }) => {
12
+ const { count, showCount } = (0, __v_c_util_dist_props_util.toPropsRefs)(props, "count", "showCount");
13
+ const value = (0, vue.shallowRef)(props?.value ?? props?.defaultValue ?? "");
14
+ (0, vue.watch)(() => props.value, () => {
15
+ value.value = props.value;
14
16
  });
15
- const setValue = (val, callback) => {
16
- if (stateValue.value === val) return;
17
- if (props.value === void 0) stateValue.value = val;
18
- (0, vue.nextTick)(() => {
19
- callback?.();
20
- });
21
- };
22
- const formatValue = (0, vue.computed)(() => stateValue.value === void 0 || stateValue.value === null ? "" : String(stateValue.value));
17
+ const formatValue = (0, vue.computed)(() => value.value === void 0 || value.value === null ? "" : String(value.value));
23
18
  const focused = (0, vue.shallowRef)(false);
24
19
  const compositionRef = (0, vue.shallowRef)(false);
25
- const textareaResized = (0, vue.shallowRef)(false);
20
+ const textareaResized = (0, vue.shallowRef)();
26
21
  const holderRef = (0, vue.shallowRef)();
27
22
  const resizableTextAreaRef = (0, vue.shallowRef)();
28
23
  const getTextArea = () => resizableTextAreaRef.value?.textArea;
29
24
  const focus = () => {
30
- getTextArea()?.focus();
25
+ getTextArea().focus();
31
26
  };
32
27
  expose({
33
28
  resizableTextArea: resizableTextAreaRef,
34
29
  focus,
35
30
  blur: () => {
36
- getTextArea()?.blur();
31
+ getTextArea().blur();
37
32
  },
38
- nativeElement: (0, vue.computed)(() => holderRef.value?.nativeElement || getTextArea() || null)
33
+ nativeElement: (0, vue.computed)(() => holderRef.value?.nativeElement || getTextArea())
39
34
  });
40
- (0, vue.watch)(() => props.disabled, (newDisabled) => {
41
- focused.value = !newDisabled && focused.value;
35
+ (0, vue.watch)(() => props.disabled, () => {
36
+ const prev = focused.value;
37
+ if (props.disabled && prev) focused.value = !props?.disabled && prev;
38
+ }, {
39
+ immediate: true,
40
+ flush: "post"
42
41
  });
43
42
  const selection = (0, vue.shallowRef)(null);
44
- (0, vue.watch)(selection, (newSelection) => {
45
- if (newSelection) getTextArea()?.setSelectionRange(...newSelection);
43
+ (0, vue.watch)(selection, () => {
44
+ if (selection.value) getTextArea().setSelectionRange(...selection.value);
46
45
  });
47
- const countConfig = (0, __v_c_input.useCount)((0, vue.computed)(() => props.count), (0, vue.computed)(() => props.showCount));
46
+ const countConfig = (0, __v_c_input.useCount)(count, showCount);
48
47
  const mergedMax = (0, vue.computed)(() => countConfig.value.max ?? props.maxLength);
49
48
  const hasMaxLength = (0, vue.computed)(() => Number(mergedMax.value) > 0);
50
49
  const valueLength = (0, vue.computed)(() => countConfig.value.strategy(formatValue.value));
51
50
  const isOutOfRange = (0, vue.computed)(() => !!mergedMax.value && valueLength.value > mergedMax.value);
52
- const onChange = (e) => {
53
- props.onChange?.(e);
54
- };
55
51
  const triggerChange = (e, currentValue) => {
56
52
  let cutValue = currentValue;
57
- const cfg = countConfig.value;
58
- if (!compositionRef.value && cfg.exceedFormatter && cfg.max && cfg.strategy(currentValue) > cfg.max) {
59
- cutValue = cfg.exceedFormatter(currentValue, { max: cfg.max });
60
- if (currentValue !== cutValue) selection.value = [getTextArea()?.selectionStart || 0, getTextArea()?.selectionEnd || 0];
53
+ if (!compositionRef.value && countConfig.value.exceedFormatter && countConfig.value.max && countConfig.value.strategy(currentValue) > countConfig.value.max) {
54
+ cutValue = countConfig.value.exceedFormatter(currentValue, { max: countConfig.value.max });
55
+ const textarea$1 = getTextArea();
56
+ if (currentValue !== cutValue) selection.value = [textarea$1.selectionStart || 0, textarea$1.selectionEnd || 0];
61
57
  }
62
- setValue(cutValue);
63
- emit("update:value", cutValue);
64
- (0, vue.nextTick)(() => {
65
- resizableTextAreaRef.value?.setValue?.(cutValue);
66
- });
67
- (0, __v_c_input.resolveOnChange)(e.currentTarget, e, onChange, cutValue);
58
+ const textarea = getTextArea();
59
+ if (!compositionRef.value && textarea && textarea.value !== cutValue) textarea.value = cutValue;
60
+ value.value = cutValue;
61
+ (0, __v_c_input.resolveOnChange)(e.currentTarget, e, props.onChange, cutValue);
68
62
  };
69
- const onInternalCompositionStart = (e) => {
63
+ const onInternalCompositionStart = () => {
70
64
  compositionRef.value = true;
71
- props.onCompositionStart?.(e);
72
65
  };
73
66
  const onInternalCompositionEnd = (e) => {
74
67
  compositionRef.value = false;
75
- triggerChange(e, e.target.value);
76
- props.onCompositionEnd?.(e);
68
+ triggerChange(e, e.currentTarget.value);
77
69
  };
78
70
  const onInternalChange = (e) => {
79
71
  triggerChange(e, e.target.value);
80
72
  };
81
73
  const handleKeyDown = (e) => {
82
- if (e.key === "Enter" && props.onPressEnter && !e.isComposing) props.onPressEnter?.(e);
83
- props.onKeyDown?.(e);
74
+ const { onPressEnter } = props;
75
+ if (e.key === __v_c_util_dist_KeyCode.KeyCodeStr.Enter && onPressEnter && !e.isComposing) onPressEnter(e);
76
+ props?.onKeydown?.(e);
84
77
  };
85
- const handleFocus = (e) => {
78
+ const handleFocus = () => {
86
79
  focused.value = true;
87
- props.onFocus?.(e);
88
80
  };
89
- const handleBlur = (e) => {
81
+ const handleBlur = () => {
90
82
  focused.value = false;
91
- props.onBlur?.(e);
92
83
  };
93
84
  const handleReset = (e) => {
94
- (0, __v_c_input.resolveOnChange)(getTextArea(), e, onChange);
95
- setValue("", () => focus());
96
- emit("update:value", "");
85
+ value.value = "";
86
+ focus();
87
+ (0, __v_c_input.resolveOnChange)(getTextArea(), e, props.onChange);
97
88
  };
98
89
  const handleResize = (size) => {
99
- props.onResize?.(size);
90
+ props?.onResize?.(size);
100
91
  if (getTextArea()?.style.height) textareaResized.value = true;
101
92
  };
102
- const mergedAllowClear = (0, vue.computed)(() => {
103
- if (!props.allowClear) return props.allowClear;
104
- const clearIcon = slots.clearIcon?.();
105
- if (clearIcon) return {
106
- ...typeof props.allowClear === "object" ? props.allowClear : {},
107
- clearIcon
108
- };
109
- return props.allowClear;
110
- });
111
93
  return () => {
112
- const { allowClear, maxLength, prefixCls = defaults.prefixCls, showCount, disabled, hidden, classNames, styles, onClear, readOnly, autoSize, suffix } = props;
113
- const { class: attrClass, style: attrStyle,...restAttrs } = attrs;
114
- const mergedClassName = props.className || attrClass;
115
- const mergedStyle = {
116
- ...props.style,
117
- ...attrStyle
118
- };
119
- let suffixNode = slots.suffix?.() ?? suffix;
94
+ const { suffix, classNames, styles, prefixCls = "vc-textarea", allowClear, autoSize, showCount: showCount$1, disabled, hidden, readOnly, onClear, maxLength } = props;
95
+ const { style, restAttrs, className } = (0, __v_c_util_dist_props_util.getAttrStyleAndClass)(attrs);
96
+ let suffixNode = suffix;
120
97
  let dataCount;
121
98
  if (countConfig.value.show) {
122
- if (countConfig.value.showFormatter) dataCount = countConfig.value.showFormatter({
99
+ if (countConfig.value.showFormatter) dataCount = countConfig.value.showFormatter?.({
123
100
  value: formatValue.value,
124
101
  count: valueLength.value,
125
102
  maxLength: mergedMax.value
126
103
  });
127
104
  else dataCount = `${valueLength.value}${hasMaxLength.value ? ` / ${mergedMax.value}` : ""}`;
128
105
  suffixNode = (0, vue.createVNode)(vue.Fragment, null, [suffixNode, (0, vue.createVNode)("span", {
129
- "class": (0, __v_c_util.classNames)(`${prefixCls}-data-count`, classNames?.count),
106
+ "class": (0, __v_c_util.clsx)(`${prefixCls}-data-count`, classNames?.count),
130
107
  "style": styles?.count
131
108
  }, [dataCount])]);
132
109
  }
133
- const isPureTextArea = !autoSize && !showCount && !allowClear;
134
- const baseInputClassNames = {
135
- ...classNames,
136
- affixWrapper: (0, __v_c_util.classNames)(classNames?.affixWrapper, {
137
- [`${prefixCls}-show-count`]: showCount,
138
- [`${prefixCls}-textarea-allow-clear`]: allowClear
139
- })
110
+ const isPureTextArea = !autoSize && !showCount$1 && !allowClear;
111
+ const textareaProps = {
112
+ onKeydown: handleKeyDown,
113
+ onFocus: handleFocus,
114
+ onBlur: handleBlur,
115
+ onCompositionstart: onInternalCompositionStart,
116
+ onCompositionend: onInternalCompositionEnd
140
117
  };
141
- const inputAttrs = (0, __v_c_util_dist_omit.default)(restAttrs, [
142
- "class",
143
- "style",
144
- "onFocus",
145
- "onBlur",
146
- "onChange",
147
- "onCompositionstart",
148
- "onCompositionend",
149
- "onKeydown",
150
- "onKeyup",
151
- "onInput"
152
- ]);
153
- const restProps = (0, __v_c_util_dist_omit.default)(props, [
154
- "class",
155
- "style",
156
- "onFocus",
157
- "onBlur",
158
- "onChange",
159
- "onCompositionEnd",
160
- "onCompositionStart",
161
- "onKeyDown",
162
- "allowClear",
163
- "maxLength",
164
- "prefixCls",
165
- "showCount",
166
- "disabled",
167
- "hidden",
168
- "classNames",
169
- "styles",
170
- "onClear",
171
- "readOnly",
172
- "autoSize",
173
- "suffix",
174
- "onResize"
175
- ]);
176
118
  return (0, vue.createVNode)(__v_c_input.BaseInput, {
177
119
  "ref": holderRef,
178
120
  "value": formatValue.value,
179
- "allowClear": mergedAllowClear.value,
121
+ "allowClear": allowClear,
180
122
  "handleReset": handleReset,
181
123
  "suffix": suffixNode,
182
124
  "prefixCls": prefixCls,
183
- "classNames": baseInputClassNames,
125
+ "classNames": {
126
+ ...classNames,
127
+ affixWrapper: (0, __v_c_util.clsx)(classNames?.affixWrapper, {
128
+ [`${prefixCls}-show-count`]: showCount$1,
129
+ [`${prefixCls}-textarea-allow-clear`]: allowClear
130
+ })
131
+ },
184
132
  "disabled": disabled,
185
133
  "focused": focused.value,
186
- "class": (0, __v_c_util.classNames)([mergedClassName], isOutOfRange.value && `${prefixCls}-out-of-range`),
134
+ "class": (0, __v_c_util.clsx)(className, isOutOfRange.value && `${prefixCls}-out-of-range`),
187
135
  "style": {
188
- ...mergedStyle,
136
+ ...style,
189
137
  ...textareaResized.value && !isPureTextArea ? { height: "auto" } : {}
190
138
  },
191
139
  "dataAttrs": { affixWrapper: { "data-count": typeof dataCount === "string" ? dataCount : void 0 } },
192
140
  "hidden": hidden,
193
141
  "readOnly": readOnly,
194
142
  "onClear": onClear
195
- }, { default: () => [(0, vue.createVNode)(require_ResizableTextArea.default, (0, vue.mergeProps)(inputAttrs, restProps, {
196
- "value": stateValue.value,
143
+ }, { default: () => [(0, vue.createVNode)(require_ResizableTextArea.default, (0, vue.mergeProps)(restAttrs, (0, __v_c_util_dist_omit.default)(props, [
144
+ "suffix",
145
+ "classNames",
146
+ "styles",
147
+ "prefixCls",
148
+ "allowClear",
149
+ "autoSize",
150
+ "showCount",
151
+ "disabled",
152
+ "hidden",
153
+ "readOnly",
154
+ "onClear",
155
+ "maxLength",
156
+ "onResize",
157
+ "onChange",
158
+ "onKeydown",
159
+ "onPressEnter"
160
+ ]), {
197
161
  "autoSize": autoSize,
198
162
  "maxLength": maxLength,
199
- "onKeydown": handleKeyDown,
200
- "onChange": onInternalChange,
201
- "onFocus": handleFocus,
202
- "onBlur": handleBlur,
203
- "onCompositionstart": onInternalCompositionStart,
204
- "onCompositionend": onInternalCompositionEnd,
205
- "class": (0, __v_c_util.classNames)(classNames?.textarea),
163
+ "onChange": onInternalChange
164
+ }, textareaProps, {
165
+ "class": (0, __v_c_util.clsx)(classNames?.textarea),
206
166
  "style": {
207
- ...styles?.textarea,
208
- resize: attrStyle?.resize
167
+ resize: style?.resize,
168
+ ...styles?.textarea
209
169
  },
210
170
  "disabled": disabled,
171
+ "value": value.value,
211
172
  "prefixCls": prefixCls,
212
173
  "onResize": handleResize,
213
174
  "ref": resizableTextAreaRef,
@@ -217,12 +178,12 @@ var TextArea_default = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs,
217
178
  }, {
218
179
  props: /* @__PURE__ */ (0, vue.mergeDefaults)({
219
180
  value: {
220
- type: [String, Number],
181
+ type: null,
221
182
  required: false,
222
183
  default: void 0
223
184
  },
224
185
  defaultValue: {
225
- type: [String, Number],
186
+ type: null,
226
187
  required: false,
227
188
  default: void 0
228
189
  },
@@ -231,6 +192,11 @@ var TextArea_default = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs,
231
192
  required: false,
232
193
  default: void 0
233
194
  },
195
+ disabled: {
196
+ type: Boolean,
197
+ required: false,
198
+ default: void 0
199
+ },
234
200
  autoSize: {
235
201
  type: [Boolean, Object],
236
202
  required: false,
@@ -246,16 +212,6 @@ var TextArea_default = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs,
246
212
  required: false,
247
213
  default: void 0
248
214
  },
249
- onClear: {
250
- type: Function,
251
- required: false,
252
- default: void 0
253
- },
254
- readOnly: {
255
- type: Boolean,
256
- required: false,
257
- default: void 0
258
- },
259
215
  classNames: {
260
216
  type: Object,
261
217
  required: false,
@@ -271,106 +227,69 @@ var TextArea_default = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs,
271
227
  required: false,
272
228
  default: void 0
273
229
  },
274
- showCount: {
275
- type: [Boolean, Object],
276
- required: false,
277
- default: void 0
278
- },
279
- count: {
280
- type: Object,
281
- required: false,
282
- default: void 0
283
- },
284
- maxLength: {
285
- type: Number,
286
- required: false,
287
- default: void 0
288
- },
289
230
  suffix: {
290
- type: [
291
- String,
292
- Number,
293
- null,
294
- Boolean,
295
- Array
296
- ],
231
+ type: null,
297
232
  required: false,
298
- skipCheck: true,
299
233
  default: void 0
300
234
  },
301
- disabled: {
302
- type: Boolean,
235
+ showCount: {
236
+ type: [Boolean, Object],
303
237
  required: false,
304
238
  default: void 0
305
239
  },
306
- hidden: {
307
- type: Boolean,
240
+ count: {
241
+ type: null,
308
242
  required: false,
309
243
  default: void 0
310
244
  },
311
- onChange: {
245
+ onClear: {
312
246
  type: Function,
313
247
  required: false,
314
248
  default: void 0
315
249
  },
316
- onFocus: {
250
+ onChange: {
317
251
  type: Function,
318
252
  required: false,
319
253
  default: void 0
320
254
  },
321
- onBlur: {
322
- type: Function,
255
+ maxLength: {
256
+ type: Number,
323
257
  required: false,
324
258
  default: void 0
325
259
  },
326
- onCompositionStart: {
327
- type: Function,
260
+ minLength: {
261
+ type: Number,
328
262
  required: false,
329
263
  default: void 0
330
264
  },
331
- onCompositionEnd: {
332
- type: Function,
265
+ hidden: {
266
+ type: Boolean,
333
267
  required: false,
334
268
  default: void 0
335
269
  },
336
- onKeyDown: {
337
- type: Function,
270
+ readOnly: {
271
+ type: Boolean,
338
272
  required: false,
339
273
  default: void 0
340
274
  },
341
- className: {
275
+ placeholder: {
342
276
  type: String,
343
277
  required: false,
344
278
  default: void 0
345
279
  },
346
- class: {
347
- type: null,
348
- required: false,
349
- default: void 0
350
- },
351
- style: {
352
- type: null,
280
+ autoFocus: {
281
+ type: Boolean,
353
282
  required: false,
354
283
  default: void 0
355
284
  },
356
- placeholder: {
357
- type: String,
285
+ onKeydown: {
286
+ type: Function,
358
287
  required: false,
359
288
  default: void 0
360
289
  }
361
- }, defaults),
290
+ }, { prefixCls: "vc-textarea" }),
362
291
  name: "TextArea",
363
- inheritAttrs: false,
364
- emits: [
365
- "update:value",
366
- "change",
367
- "compositionStart",
368
- "compositionEnd",
369
- "pressEnter",
370
- "keydown",
371
- "focus",
372
- "blur",
373
- "resize"
374
- ]
292
+ inheritAttrs: false
375
293
  });
294
+ var TextArea_default = TextArea;
376
295
  exports.default = TextArea_default;
@@ -1,3 +1,3 @@
1
1
  import { TextAreaProps } from './interface';
2
- declare const _default: import('vue').DefineSetupFnComponent<TextAreaProps, {}, {}, TextAreaProps & {}, import('vue').PublicProps>;
3
- export default _default;
2
+ declare const TextArea: import('vue').DefineSetupFnComponent<TextAreaProps, {}, {}, TextAreaProps & {}, import('vue').PublicProps>;
3
+ export default TextArea;