@v-c/input-number 1.0.7 → 1.1.0
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/InputNumber.d.ts +7 -1
- package/dist/InputNumber.js +59 -5
- package/package.json +3 -3
package/dist/InputNumber.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValueType } from '@v-c/mini-decimal';
|
|
2
2
|
import { InputFocusOptions } from '@v-c/util/dist/Dom/focus';
|
|
3
3
|
export type { ValueType };
|
|
4
|
-
type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix';
|
|
4
|
+
type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix' | 'clear';
|
|
5
5
|
export interface InputNumberProps<T extends ValueType = ValueType> {
|
|
6
6
|
mode?: 'input' | 'spinner';
|
|
7
7
|
prefixCls?: string;
|
|
@@ -19,6 +19,11 @@ export interface InputNumberProps<T extends ValueType = ValueType> {
|
|
|
19
19
|
readOnly?: boolean;
|
|
20
20
|
prefix?: any;
|
|
21
21
|
suffix?: any;
|
|
22
|
+
allowClear?: boolean | {
|
|
23
|
+
clearIcon?: any;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
label?: string;
|
|
26
|
+
};
|
|
22
27
|
upHandler?: any;
|
|
23
28
|
downHandler?: any;
|
|
24
29
|
keyboard?: boolean;
|
|
@@ -33,6 +38,7 @@ export interface InputNumberProps<T extends ValueType = ValueType> {
|
|
|
33
38
|
decimalSeparator?: string;
|
|
34
39
|
onInput?: (text: string) => void;
|
|
35
40
|
onChange?: (value: T | null) => void;
|
|
41
|
+
onClear?: () => void;
|
|
36
42
|
onPressEnter?: (e: KeyboardEvent) => void;
|
|
37
43
|
onStep?: (value: T, info: {
|
|
38
44
|
offset: ValueType;
|
package/dist/InputNumber.js
CHANGED
|
@@ -4,7 +4,7 @@ import StepHandler_default from "./StepHandler.js";
|
|
|
4
4
|
import { getDecupleSteps } from "./utils/numberUtil.js";
|
|
5
5
|
import { computed, createVNode, defineComponent, isVNode, mergeDefaults, mergeProps, shallowRef, watch, watchEffect } from "vue";
|
|
6
6
|
import getMiniDecimal, { getNumberPrecision, num2str, toFixed, validateNumber } from "@v-c/mini-decimal";
|
|
7
|
-
import { clsx } from "@v-c/util";
|
|
7
|
+
import { clsx, isVueRenderable } from "@v-c/util";
|
|
8
8
|
import { triggerFocus } from "@v-c/util/dist/Dom/focus";
|
|
9
9
|
import { KeyCodeStr } from "@v-c/util/dist/KeyCode";
|
|
10
10
|
import omit from "@v-c/util/dist/omit";
|
|
@@ -77,6 +77,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
77
77
|
};
|
|
78
78
|
const inputValue = shallowRef("");
|
|
79
79
|
const inputValueRef = shallowRef("");
|
|
80
|
+
let inputValueUpdateId = 0;
|
|
80
81
|
const mergedFormatter = (number, userTyping) => {
|
|
81
82
|
if (props.formatter) return props.formatter(number, {
|
|
82
83
|
userTyping,
|
|
@@ -159,6 +160,8 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
159
160
|
};
|
|
160
161
|
const onNextPromise = useFrame_default();
|
|
161
162
|
const collectInputValue = (inputStr) => {
|
|
163
|
+
inputValueUpdateId += 1;
|
|
164
|
+
const currentUpdateId = inputValueUpdateId;
|
|
162
165
|
recordCursor();
|
|
163
166
|
inputValueRef.value = inputStr;
|
|
164
167
|
inputValue.value = inputStr;
|
|
@@ -168,6 +171,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
168
171
|
}
|
|
169
172
|
props.onInput?.(inputStr);
|
|
170
173
|
onNextPromise(() => {
|
|
174
|
+
if (currentUpdateId !== inputValueUpdateId) return;
|
|
171
175
|
let nextInputStr = inputStr;
|
|
172
176
|
if (!props.parser) nextInputStr = inputStr.replace(/。/g, ".");
|
|
173
177
|
if (nextInputStr !== inputStr) collectInputValue(nextInputStr);
|
|
@@ -274,6 +278,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
274
278
|
}
|
|
275
279
|
});
|
|
276
280
|
const onBlur = (e) => {
|
|
281
|
+
if (e.relatedTarget instanceof Node && rootRef.value?.contains(e.relatedTarget)) return;
|
|
277
282
|
if (props.changeOnBlur ?? true) flushInputValue(false);
|
|
278
283
|
focus.value = false;
|
|
279
284
|
userTypingRef.value = false;
|
|
@@ -291,6 +296,26 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
291
296
|
}
|
|
292
297
|
props.onMouseDown?.(event);
|
|
293
298
|
};
|
|
299
|
+
const onClearKeyDown = (event) => {
|
|
300
|
+
const isStepKey = [
|
|
301
|
+
"Up",
|
|
302
|
+
"ArrowUp",
|
|
303
|
+
"Down",
|
|
304
|
+
"ArrowDown"
|
|
305
|
+
].includes(event.key);
|
|
306
|
+
if (event.key === KeyCodeStr.Enter || props.keyboard !== false && isStepKey) event.stopPropagation();
|
|
307
|
+
};
|
|
308
|
+
const onClearClick = () => {
|
|
309
|
+
userTypingRef.value = false;
|
|
310
|
+
inputValueRef.value = "";
|
|
311
|
+
inputValueUpdateId += 1;
|
|
312
|
+
const emptyValue = getMiniDecimal(null);
|
|
313
|
+
if (props.value !== void 0) setInputValue(decimalValue.value, false);
|
|
314
|
+
else if (decimalValue.value.isEmpty()) setInputValue(emptyValue, false);
|
|
315
|
+
inputRef.value?.focus();
|
|
316
|
+
triggerValueUpdate(emptyValue, false);
|
|
317
|
+
props.onClear?.();
|
|
318
|
+
};
|
|
294
319
|
watch([
|
|
295
320
|
() => props.precision,
|
|
296
321
|
() => props.formatter,
|
|
@@ -308,7 +333,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
308
333
|
if (props.formatter) restoreCursor();
|
|
309
334
|
}, { flush: "post" });
|
|
310
335
|
return () => {
|
|
311
|
-
const { prefixCls = defaults.prefixCls, classNames, styles, step = defaults.step, disabled, readOnly, controls = defaults.controls, mode = defaults.mode, placeholder } = props;
|
|
336
|
+
const { prefixCls = defaults.prefixCls, classNames, styles, step = defaults.step, disabled, readOnly, controls = defaults.controls, mode = defaults.mode, placeholder, allowClear } = props;
|
|
312
337
|
const mergedPrefixCls = prefixCls || defaults.prefixCls;
|
|
313
338
|
const { class: className, style, ...restAttrs } = attrs;
|
|
314
339
|
const mergedClassName = props.className || className;
|
|
@@ -321,6 +346,23 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
321
346
|
const suffixNode = slots.suffix?.() ?? props.suffix;
|
|
322
347
|
const upNode = slots.upHandler?.() ?? props.upHandler;
|
|
323
348
|
const downNode = slots.downHandler?.() ?? props.downHandler;
|
|
349
|
+
const clearConfig = allowClear && typeof allowClear === "object" ? allowClear : { disabled: allowClear !== true };
|
|
350
|
+
const showClear = !disabled && !readOnly && clearConfig.disabled !== true && String(inputValue.value).length > 0;
|
|
351
|
+
const hasSuffix = isVueRenderable(suffixNode);
|
|
352
|
+
const clearIconCls = `${mergedPrefixCls}-clear-icon`;
|
|
353
|
+
const clearNode = allowClear && createVNode("button", {
|
|
354
|
+
"type": "button",
|
|
355
|
+
"aria-label": clearConfig.label ?? "Clear",
|
|
356
|
+
"disabled": !showClear,
|
|
357
|
+
"class": clsx(clearIconCls, {
|
|
358
|
+
[`${clearIconCls}-hidden`]: !showClear,
|
|
359
|
+
[`${clearIconCls}-has-suffix`]: hasSuffix
|
|
360
|
+
}, classNames?.clear),
|
|
361
|
+
"style": styles?.clear,
|
|
362
|
+
"onMousedown": (event) => event.preventDefault(),
|
|
363
|
+
"onKeydown": onClearKeyDown,
|
|
364
|
+
"onClick": onClearClick
|
|
365
|
+
}, [slots.clearIcon?.() ?? clearConfig.clearIcon ?? "✖"]);
|
|
324
366
|
const upHandlerNode = createVNode(StepHandler_default, {
|
|
325
367
|
"prefixCls": mergedPrefixCls,
|
|
326
368
|
"action": "up",
|
|
@@ -345,6 +387,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
345
387
|
"value",
|
|
346
388
|
"prefix",
|
|
347
389
|
"suffix",
|
|
390
|
+
"allowClear",
|
|
348
391
|
"upHandler",
|
|
349
392
|
"downHandler",
|
|
350
393
|
"keyboard",
|
|
@@ -356,6 +399,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
356
399
|
"precision",
|
|
357
400
|
"decimalSeparator",
|
|
358
401
|
"onChange",
|
|
402
|
+
"onClear",
|
|
359
403
|
"onInput",
|
|
360
404
|
"onPressEnter",
|
|
361
405
|
"onStep",
|
|
@@ -408,7 +452,7 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
408
452
|
}
|
|
409
453
|
}, [
|
|
410
454
|
mode === "spinner" && controls && downHandlerNode,
|
|
411
|
-
|
|
455
|
+
isVueRenderable(prefixNode) && createVNode("div", {
|
|
412
456
|
"class": clsx(`${mergedPrefixCls}-prefix`, classNames?.prefix),
|
|
413
457
|
"style": styles?.prefix
|
|
414
458
|
}, [prefixNode]),
|
|
@@ -435,10 +479,10 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
435
479
|
"onCompositionend": onCompositionEnd,
|
|
436
480
|
"onBeforeinput": onBeforeInput
|
|
437
481
|
}, inputAttrs), null),
|
|
438
|
-
|
|
482
|
+
(allowClear || hasSuffix) && createVNode("div", {
|
|
439
483
|
"class": clsx(`${mergedPrefixCls}-suffix`, classNames?.suffix),
|
|
440
484
|
"style": styles?.suffix
|
|
441
|
-
}, [suffixNode]),
|
|
485
|
+
}, [clearNode, suffixNode]),
|
|
442
486
|
mode === "spinner" && controls && upHandlerNode,
|
|
443
487
|
mode === "input" && controls && createVNode("div", {
|
|
444
488
|
"class": clsx(`${mergedPrefixCls}-actions`, classNames?.actions),
|
|
@@ -519,6 +563,11 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
519
563
|
required: false,
|
|
520
564
|
default: void 0
|
|
521
565
|
},
|
|
566
|
+
allowClear: {
|
|
567
|
+
type: [Boolean, Object],
|
|
568
|
+
required: false,
|
|
569
|
+
default: void 0
|
|
570
|
+
},
|
|
522
571
|
upHandler: {
|
|
523
572
|
required: false,
|
|
524
573
|
default: void 0
|
|
@@ -572,6 +621,11 @@ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose
|
|
|
572
621
|
required: false,
|
|
573
622
|
default: void 0
|
|
574
623
|
},
|
|
624
|
+
onClear: {
|
|
625
|
+
type: Function,
|
|
626
|
+
required: false,
|
|
627
|
+
default: void 0
|
|
628
|
+
},
|
|
575
629
|
onPressEnter: {
|
|
576
630
|
type: Function,
|
|
577
631
|
required: false,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@v-c/input-number",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/antdv-next/vue-components/tree/next/packages/input-number",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"vue": "^3.0.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@v-c/input": "^1.1.
|
|
33
|
+
"@v-c/input": "^1.1.2",
|
|
34
34
|
"@v-c/mini-decimal": "^1.0.1",
|
|
35
|
-
"@v-c/util": "^1.
|
|
35
|
+
"@v-c/util": "^1.2.0"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "vite build",
|