@v-c/input-number 1.0.5 → 1.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.
@@ -1,5 +1,5 @@
1
- import { ValueType } from '../../mini-decimal/src';
2
- import { InputFocusOptions } from '../../util/src/Dom/focus';
1
+ import { ValueType } from '@v-c/mini-decimal';
2
+ import { InputFocusOptions } from '@v-c/util/dist/Dom/focus';
3
3
  export type { ValueType };
4
4
  type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix';
5
5
  export interface InputNumberProps<T extends ValueType = ValueType> {
@@ -8,9 +8,24 @@ import { clsx } 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";
11
+ //#region src/InputNumber.tsx
11
12
  function _isSlot(s) {
12
13
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !isVNode(s);
13
14
  }
15
+ /** Accumulated wheel distance that adds up to a single step. */
16
+ var WHEEL_STEP_DISTANCE = 100;
17
+ var WHEEL_LINE_HEIGHT = 40;
18
+ var WHEEL_PAGE_HEIGHT = 800;
19
+ /** Idle gap after which a new wheel gesture starts from scratch. */
20
+ var WHEEL_DELTA_RESET_INTERVAL = 200;
21
+ /** Normalize `deltaY` to pixels — browsers may report it in lines or pages. */
22
+ function getWheelDeltaY(event) {
23
+ switch (event.deltaMode) {
24
+ case 1: return event.deltaY * WHEEL_LINE_HEIGHT;
25
+ case 2: return event.deltaY * WHEEL_PAGE_HEIGHT;
26
+ default: return event.deltaY;
27
+ }
28
+ }
14
29
  var defaults = {
15
30
  prefixCls: "vc-input-number",
16
31
  step: 1,
@@ -27,7 +42,7 @@ function getDecimalIfValidate(value) {
27
42
  const decimal = getMiniDecimal(value);
28
43
  return decimal.isInvalidate() ? null : decimal;
29
44
  }
30
- var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose, emit }) => {
45
+ var InputNumber = /* @__PURE__ */ defineComponent((props, { attrs, slots, expose, emit }) => {
31
46
  const focus = shallowRef(false);
32
47
  const userTypingRef = shallowRef(false);
33
48
  const compositionRef = shallowRef(false);
@@ -175,7 +190,8 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
175
190
  userTypingRef.value = false;
176
191
  let stepDecimal = getMiniDecimal(shiftKeyRef.value ? getDecupleSteps(props.step ?? 1) : props.step ?? 1);
177
192
  if (!up) stepDecimal = stepDecimal.negate();
178
- const updatedValue = triggerValueUpdate((decimalValue.value || getMiniDecimal(0)).add(stepDecimal.toString()), false);
193
+ const target = (decimalValue.value || getMiniDecimal(0)).add(stepDecimal.toString());
194
+ const updatedValue = triggerValueUpdate(target, false);
179
195
  const outValue = getDecimalValue(props.stringMode, updatedValue);
180
196
  props.onStep?.(outValue, {
181
197
  offset: shiftKeyRef.value ? getDecupleSteps(props.step ?? 1) : props.step ?? 1,
@@ -225,20 +241,43 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
225
241
  shiftKeyRef.value = false;
226
242
  props.onKeyUp?.(event);
227
243
  };
244
+ let wheelDelta = 0;
245
+ let wheelTimestamp = 0;
246
+ const resetWheel = () => {
247
+ wheelDelta = 0;
248
+ wheelTimestamp = 0;
249
+ };
250
+ const onInternalWheel = (event) => {
251
+ const delta = getWheelDeltaY(event);
252
+ if (!delta) return;
253
+ const eventTimestamp = event.timeStamp || Date.now();
254
+ if (eventTimestamp - wheelTimestamp > WHEEL_DELTA_RESET_INTERVAL) wheelDelta = 0;
255
+ wheelTimestamp = eventTimestamp;
256
+ if (wheelDelta && Math.sign(wheelDelta) !== Math.sign(delta)) wheelDelta = 0;
257
+ wheelDelta += delta;
258
+ if (Math.abs(wheelDelta) >= WHEEL_STEP_DISTANCE) {
259
+ onInternalStep(wheelDelta < 0, "wheel");
260
+ wheelDelta -= Math.sign(wheelDelta) * WHEEL_STEP_DISTANCE;
261
+ }
262
+ };
228
263
  watchEffect((onCleanup) => {
229
264
  if (props.changeOnWheel && focus.value && inputRef.value) {
230
265
  const onWheel = (event) => {
231
- onInternalStep(event.deltaY < 0, "wheel");
266
+ onInternalWheel(event);
232
267
  event.preventDefault();
233
268
  };
234
269
  inputRef.value.addEventListener("wheel", onWheel, { passive: false });
235
- onCleanup(() => inputRef.value?.removeEventListener("wheel", onWheel));
270
+ onCleanup(() => {
271
+ inputRef.value?.removeEventListener("wheel", onWheel);
272
+ resetWheel();
273
+ });
236
274
  }
237
275
  });
238
276
  const onBlur = (e) => {
239
277
  if (props.changeOnBlur ?? true) flushInputValue(false);
240
278
  focus.value = false;
241
279
  userTypingRef.value = false;
280
+ resetWheel();
242
281
  props.onBlur?.(e);
243
282
  };
244
283
  const onFocus = (e) => {
@@ -269,7 +308,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
269
308
  if (props.formatter) restoreCursor();
270
309
  }, { flush: "post" });
271
310
  return () => {
272
- const { prefixCls = defaults.prefixCls, classNames: classNames$1, styles, step = defaults.step, disabled, readOnly, controls = defaults.controls, mode = defaults.mode, placeholder } = props;
311
+ const { prefixCls = defaults.prefixCls, classNames, styles, step = defaults.step, disabled, readOnly, controls = defaults.controls, mode = defaults.mode, placeholder } = props;
273
312
  const mergedPrefixCls = prefixCls || defaults.prefixCls;
274
313
  const { class: className, style, ...restAttrs } = attrs;
275
314
  const mergedClassName = props.className || className;
@@ -287,7 +326,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
287
326
  "action": "up",
288
327
  "disabled": upDisabled.value,
289
328
  "onStep": onInternalStep,
290
- "className": classNames$1?.action,
329
+ "className": classNames?.action,
291
330
  "style": styles?.action
292
331
  }, _isSlot(upNode) ? upNode : { default: () => [upNode] });
293
332
  const downHandlerNode = createVNode(StepHandler_default, {
@@ -295,7 +334,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
295
334
  "action": "down",
296
335
  "disabled": downDisabled.value,
297
336
  "onStep": onInternalStep,
298
- "className": classNames$1?.action,
337
+ "className": classNames?.action,
299
338
  "style": styles?.action
300
339
  }, _isSlot(downNode) ? downNode : { default: () => [downNode] });
301
340
  const inputAttrs = omit({ ...restAttrs }, [
@@ -340,7 +379,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
340
379
  ]);
341
380
  return createVNode("div", {
342
381
  "ref": rootRef,
343
- "class": clsx(mergedPrefixCls, `${mergedPrefixCls}-mode-${mode}`, mergedClassName, classNames$1?.root, {
382
+ "class": clsx(mergedPrefixCls, `${mergedPrefixCls}-mode-${mode}`, mergedClassName, classNames?.root, {
344
383
  [`${mergedPrefixCls}-focused`]: focus.value,
345
384
  [`${mergedPrefixCls}-disabled`]: disabled,
346
385
  [`${mergedPrefixCls}-readonly`]: readOnly,
@@ -370,7 +409,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
370
409
  }, [
371
410
  mode === "spinner" && controls && downHandlerNode,
372
411
  !!prefixNode && createVNode("div", {
373
- "class": clsx(`${mergedPrefixCls}-prefix`, classNames$1?.prefix),
412
+ "class": clsx(`${mergedPrefixCls}-prefix`, classNames?.prefix),
374
413
  "style": styles?.prefix
375
414
  }, [prefixNode]),
376
415
  createVNode("input", mergeProps({
@@ -381,7 +420,7 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
381
420
  "aria-valuenow": decimalValue.value.isInvalidate() ? null : decimalValue.value.toString(),
382
421
  "step": step,
383
422
  "ref": inputRef,
384
- "class": clsx(`${mergedPrefixCls}-input`, classNames$1?.input),
423
+ "class": clsx(`${mergedPrefixCls}-input`, classNames?.input),
385
424
  "style": styles?.input,
386
425
  "value": inputValue.value,
387
426
  "onInput": onInternalInput,
@@ -397,18 +436,18 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
397
436
  "onBeforeinput": onBeforeInput
398
437
  }, inputAttrs), null),
399
438
  !!suffixNode && createVNode("div", {
400
- "class": clsx(`${mergedPrefixCls}-suffix`, classNames$1?.suffix),
439
+ "class": clsx(`${mergedPrefixCls}-suffix`, classNames?.suffix),
401
440
  "style": styles?.suffix
402
441
  }, [suffixNode]),
403
442
  mode === "spinner" && controls && upHandlerNode,
404
443
  mode === "input" && controls && createVNode("div", {
405
- "class": clsx(`${mergedPrefixCls}-actions`, classNames$1?.actions),
444
+ "class": clsx(`${mergedPrefixCls}-actions`, classNames?.actions),
406
445
  "style": styles?.actions
407
446
  }, [upHandlerNode, downHandlerNode])
408
447
  ]);
409
448
  };
410
449
  }, {
411
- props: /* @__PURE__ */ mergeDefaults({
450
+ props: /*@__PURE__*/ mergeDefaults({
412
451
  mode: {
413
452
  type: String,
414
453
  required: false,
@@ -638,4 +677,5 @@ var InputNumber_default = /* @__PURE__ */ defineComponent((props, { attrs, slots
638
677
  inheritAttrs: false,
639
678
  emits: ["update:value"]
640
679
  });
641
- export { InputNumber_default as default };
680
+ //#endregion
681
+ export { InputNumber as default };
@@ -2,7 +2,14 @@ import { createVNode, defineComponent, onUnmounted, ref } from "vue";
2
2
  import { classNames } from "@v-c/util";
3
3
  import raf from "@v-c/util/dist/raf";
4
4
  import { filterEmpty } from "@v-c/util/dist/props-util";
5
+ //#region src/StepHandler.tsx
6
+ /**
7
+ * When click and hold on a button - the speed of auto changing the value.
8
+ */
5
9
  var STEP_INTERVAL = 200;
10
+ /**
11
+ * When click and hold on a button - the delay before auto changing the value.
12
+ */
6
13
  var STEP_DELAY = 600;
7
14
  var StepHandler_default = /* @__PURE__ */ defineComponent({
8
15
  name: "StepHandler",
@@ -72,4 +79,5 @@ var StepHandler_default = /* @__PURE__ */ defineComponent({
72
79
  };
73
80
  }
74
81
  });
82
+ //#endregion
75
83
  export { StepHandler_default as default };
@@ -1,19 +1,31 @@
1
1
  import { ref } from "vue";
2
2
  import warning from "@v-c/util/dist/warning";
3
+ //#region src/hooks/useCursor.ts
4
+ /**
5
+ * Keep input cursor in the correct position if possible.
6
+ * Is this necessary since we have `formatter` which may mass the content?
7
+ */
3
8
  function useCursor(input, focused) {
4
9
  const selectionRef = ref(null);
5
10
  function recordCursor() {
6
11
  try {
7
12
  const { selectionStart: start, selectionEnd: end, value } = input;
13
+ const beforeTxt = value.substring(0, start);
14
+ const afterTxt = value.substring(end);
8
15
  selectionRef.value = {
9
16
  start,
10
17
  end,
11
18
  value,
12
- beforeTxt: value.substring(0, start),
13
- afterTxt: value.substring(end)
19
+ beforeTxt,
20
+ afterTxt
14
21
  };
15
22
  } catch (e) {}
16
23
  }
24
+ /**
25
+ * Restore logic:
26
+ * 1. back string same
27
+ * 2. start string same
28
+ */
17
29
  function restoreCursor() {
18
30
  if (input && selectionRef.value && focused) try {
19
31
  const { value } = input;
@@ -33,4 +45,5 @@ function useCursor(input, focused) {
33
45
  }
34
46
  return [recordCursor, restoreCursor];
35
47
  }
48
+ //#endregion
36
49
  export { useCursor as default };
@@ -1,5 +1,9 @@
1
1
  import { onUnmounted, ref } from "vue";
2
2
  import raf from "@v-c/util/dist/raf";
3
+ //#region src/hooks/useFrame.ts
4
+ /**
5
+ * Always trigger latest once when call multiple time
6
+ */
3
7
  var useFrame_default = () => {
4
8
  const idRef = ref(0);
5
9
  const cleanUp = () => {
@@ -13,4 +17,5 @@ var useFrame_default = () => {
13
17
  });
14
18
  };
15
19
  };
20
+ //#endregion
16
21
  export { useFrame_default as default };
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
- import InputNumber_default from "./InputNumber.js";
2
- var src_default = InputNumber_default;
1
+ import InputNumber from "./InputNumber.js";
2
+ //#region src/index.ts
3
+ var src_default = InputNumber;
4
+ //#endregion
3
5
  export { src_default as default };
@@ -1,7 +1,9 @@
1
1
  import { num2str, trimNumber } from "@v-c/mini-decimal";
2
+ //#region src/utils/numberUtil.ts
2
3
  function getDecupleSteps(step) {
3
4
  const stepStr = typeof step === "number" ? num2str(step) : trimNumber(step).fullStr;
4
5
  if (!stepStr.includes(".")) return `${step}0`;
5
6
  return trimNumber(stepStr.replace(/(\d)\.(\d)/g, "$1$2.")).fullStr;
6
7
  }
8
+ //#endregion
7
9
  export { getDecupleSteps };
package/package.json CHANGED
@@ -1,8 +1,14 @@
1
1
  {
2
2
  "name": "@v-c/input-number",
3
3
  "type": "module",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "description": "",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/antdv-next/vue-components/tree/next/packages/input-number",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/antdv-next/vue-components.git"
11
+ },
6
12
  "publishConfig": {
7
13
  "access": "public"
8
14
  },
@@ -24,9 +30,9 @@
24
30
  "vue": "^3.0.0"
25
31
  },
26
32
  "dependencies": {
33
+ "@v-c/input": "^1.1.1",
27
34
  "@v-c/mini-decimal": "^1.0.1",
28
- "@v-c/input": "^1.0.2",
29
- "@v-c/util": "^1.0.18"
35
+ "@v-c/util": "^1.1.0"
30
36
  },
31
37
  "scripts": {
32
38
  "build": "vite build",