vueless 0.0.767 → 0.0.769

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vueless",
3
- "version": "0.0.767",
3
+ "version": "0.0.769",
4
4
  "license": "MIT",
5
5
  "description": "Vue Styleless UI Component Library, powered by Tailwind CSS.",
6
6
  "keywords": [
@@ -56,19 +56,21 @@ watch(
56
56
  () => props.modelValue,
57
57
  () => {
58
58
  if (String(localValue.value) !== String(rawValue.value)) {
59
- setValue(localValue.value);
59
+ setValue(String(localValue.value));
60
60
  }
61
61
  },
62
62
  );
63
63
 
64
64
  onMounted(() => {
65
65
  if (localValue.value) {
66
- setValue(localValue.value);
66
+ setValue(String(localValue.value));
67
67
  }
68
68
  });
69
69
 
70
70
  function onKeyup(event: KeyboardEvent) {
71
- localValue.value = rawValue.value || "";
71
+ const numberValue = !Number.isNaN(parseFloat(rawValue.value)) ? parseFloat(rawValue.value) : "";
72
+
73
+ localValue.value = props.valueType === "number" ? numberValue : rawValue.value || "";
72
74
 
73
75
  nextTick(() => emit("keyup", event));
74
76
  }
@@ -9,6 +9,7 @@ export default /*tw*/ {
9
9
  thousandsSeparator: " ",
10
10
  labelAlign: "topInside",
11
11
  prefix: "",
12
+ valueType: "number",
12
13
  positiveOnly: false,
13
14
  readonly: false,
14
15
  disabled: false,
@@ -17,7 +17,13 @@ export interface Props {
17
17
  /**
18
18
  * Input value.
19
19
  */
20
- modelValue?: string;
20
+ modelValue?: string | number;
21
+
22
+ /**
23
+ * Set type of the modelValue.
24
+ */
25
+ valueType?: "string" | "number";
26
+
21
27
  /**
22
28
  * Input label.
23
29
  */
@@ -7,7 +7,6 @@ import type { FormatOptions } from "./types.ts";
7
7
  const digitSet = ["1", "2", "3", "4", "5", "6", "7", "9", "0"];
8
8
  const rawDecimalMark = ".";
9
9
  const comma = ",";
10
- const arrowKeys = ["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"];
11
10
  const minus = "-";
12
11
 
13
12
  export default function useFormatCurrency(
@@ -62,8 +61,6 @@ export default function useFormatCurrency(
62
61
 
63
62
  const cursorStart = inputElement.selectionStart || 0;
64
63
  const cursorEnd = inputElement.selectionEnd || 0;
65
- const isEndOfValue = cursorEnd === formattedValue.value.length;
66
- const isKeyCombination = event.ctrlKey || event.shiftKey || event.metaKey || event.altKey;
67
64
  const isSelection = cursorEnd !== cursorStart;
68
65
 
69
66
  if (event.key === "Backspace" && !isSelection) {
@@ -85,25 +82,12 @@ export default function useFormatCurrency(
85
82
  }
86
83
 
87
84
  const endsWithDecimal = formattedValue.value.endsWith(options.value.decimalSeparator);
88
- const includesDecimalMark =
89
- formattedValue.value.includes(options.value.decimalSeparator) && !endsWithDecimal;
90
- const isCharKey = !arrowKeys.includes(event.key) && !isKeyCombination;
91
85
 
92
86
  if ((event.key === comma || event.key === rawDecimalMark) && endsWithDecimal) {
93
87
  event.preventDefault();
94
88
 
95
89
  return;
96
90
  }
97
-
98
- if (isEndOfValue && includesDecimalMark && isCharKey && !isSelection) {
99
- const fraction = prevValue.value.split(options.value.decimalSeparator).at(-1) || "";
100
-
101
- if (fraction.length >= options.value.maxFractionDigits) {
102
- event.preventDefault();
103
- }
104
-
105
- return;
106
- }
107
91
  }
108
92
 
109
93
  async function onInput(event: Event) {
@@ -123,7 +107,9 @@ export default function useFormatCurrency(
123
107
 
124
108
  if (value === minus) {
125
109
  formattedValue.value = minus;
126
- rawValue.value = minus;
110
+ rawValue.value = "";
111
+
112
+ return;
127
113
  }
128
114
 
129
115
  if (!value || value.startsWith(`${options.value.decimalSeparator}0`)) {
@@ -146,13 +132,25 @@ export default function useFormatCurrency(
146
132
  value = value.split("").with(value.lastIndexOf(options.value.decimalSeparator), "").join("");
147
133
  }
148
134
 
149
- if (value.endsWith(options.value.decimalSeparator)) {
135
+ const decimalSeparatorIndex = value.indexOf(options.value.decimalSeparator);
136
+ const newRawValue = getRawValue(value, options.value);
137
+
138
+ const isEventDataDecimal =
139
+ value.endsWith(options.value.decimalSeparator) ||
140
+ value.endsWith(`${options.value.decimalSeparator}0`);
141
+
142
+ if (
143
+ isEventDataDecimal &&
144
+ cursorStart > decimalSeparatorIndex &&
145
+ !options.value.minFractionDigits
146
+ ) {
150
147
  formattedValue.value = value;
151
148
 
149
+ rawValue.value = newRawValue;
150
+
152
151
  return;
153
152
  }
154
153
 
155
- const newRawValue = getRawValue(value, options.value);
156
154
  const isNumericValue = eventData && digitSet.includes(eventData);
157
155
  const isMinus = cursorEnd === 1 && cursorStart === 1 && eventData === minus;
158
156
  const isDoubleMinus = isMinus && prevValue.value.startsWith(minus);