vueless 0.0.786 → 0.0.787

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.786",
3
+ "version": "0.0.787",
4
4
  "license": "MIT",
5
5
  "description": "Vue Styleless UI Component Library, powered by Tailwind CSS.",
6
6
  "keywords": [
@@ -17,12 +17,12 @@ export interface Props {
17
17
  /**
18
18
  * Input value.
19
19
  */
20
- modelValue?: string | number;
20
+ modelValue?: number | string;
21
21
 
22
22
  /**
23
23
  * Set type of the modelValue.
24
24
  */
25
- valueType?: "string" | "number";
25
+ valueType?: "number" | "string";
26
26
 
27
27
  /**
28
28
  * Input label.
@@ -24,11 +24,15 @@ export default function useFormatCurrency(
24
24
 
25
25
  watch(
26
26
  () => options,
27
- () => setValue(formattedValue.value),
27
+ () => {
28
+ validateOptions();
29
+ setValue(formattedValue.value);
30
+ },
28
31
  { deep: true },
29
32
  );
30
33
 
31
34
  onMounted(() => {
35
+ validateOptions();
32
36
  inputElement = document.getElementById(elementId) as HTMLInputElement;
33
37
 
34
38
  if (inputElement) {
@@ -43,6 +47,25 @@ export default function useFormatCurrency(
43
47
  }
44
48
  });
45
49
 
50
+ function validateOptions() {
51
+ const warnMessages = [];
52
+
53
+ if (options.value.decimalSeparator.length > 1) {
54
+ warnMessages.push(
55
+ "[VUELESS/UInputMoney]: DecimalSeparator should not contain more than one symbol.",
56
+ );
57
+ }
58
+
59
+ if (options.value.thousandsSeparator.length > 1) {
60
+ warnMessages.push(
61
+ "[VUELESS/UInputMoney]: ThousandsSeparator should not contain more than one symbol.",
62
+ );
63
+ }
64
+
65
+ // eslint-disable-next-line no-console
66
+ warnMessages.forEach((message) => console.warn(message));
67
+ }
68
+
46
69
  /**
47
70
  * Set input value manually.
48
71
  * @param {Intl.StringNumericLiteral} value
@@ -193,7 +216,7 @@ export default function useFormatCurrency(
193
216
 
194
217
  inputElement.value = formattedValue.value;
195
218
 
196
- await setInputCursor(newFormattedValue, inputElement, cursorStart, cursorEnd);
219
+ await setInputCursor(newFormattedValue, inputElement, cursorStart, cursorEnd, eventData);
197
220
 
198
221
  prevValue.value = formattedValue.value;
199
222
  }
@@ -201,8 +224,9 @@ export default function useFormatCurrency(
201
224
  async function setInputCursor(
202
225
  newValue: string,
203
226
  inputElement: HTMLInputElement,
204
- prevCursorStart: number | null,
205
- prevCursorEnd: number | null,
227
+ prevCursorStart: number,
228
+ prevCursorEnd: number,
229
+ eventData: string,
206
230
  ) {
207
231
  const hasValueInputValue = prevCursorStart === 1 && prevCursorEnd === 1;
208
232
 
@@ -219,16 +243,33 @@ export default function useFormatCurrency(
219
243
 
220
244
  await nextTick();
221
245
 
222
- if (offset < 0 && inputElement) {
246
+ if (newValue.length <= 1) return;
247
+
248
+ // Move cursor after decimal mark
249
+ if (newValue.length < prevValue.value.length && eventData) {
250
+ const newChar = newValue[prevCursorEnd - 1];
251
+
252
+ prevCursorEnd -= newChar === options.value.decimalSeparator ? 0 : 1;
253
+ prevCursorStart -= newChar === options.value.decimalSeparator ? 0 : 1;
254
+ }
255
+
256
+ if (offset < 0 && inputElement && eventData) {
223
257
  inputElement.setSelectionRange(prevCursorStart, prevCursorEnd);
224
258
 
225
259
  return;
226
260
  }
227
261
 
262
+ // Move cursor step back on backspace.
263
+ if (offset < 0 && inputElement && !eventData) {
264
+ inputElement.setSelectionRange(prevCursorStart - 1, prevCursorEnd - 1);
265
+
266
+ return;
267
+ }
268
+
228
269
  if (newValue.length === prevCursorEnd || !prevCursorStart || !prevCursorEnd) return;
229
270
 
230
- let newCursorStart = prevCursorStart;
231
- let newCursorEnd = prevCursorEnd;
271
+ let newCursorStart = prevCursorStart + offset;
272
+ let newCursorEnd = prevCursorEnd + offset;
232
273
 
233
274
  if (hasValueInputValue && prefixLength) {
234
275
  newCursorStart += prefixLength;
@@ -236,7 +277,7 @@ export default function useFormatCurrency(
236
277
  }
237
278
 
238
279
  if (inputElement) {
239
- inputElement.setSelectionRange(newCursorStart + offset, newCursorEnd + offset);
280
+ inputElement.setSelectionRange(newCursorStart, newCursorEnd);
240
281
  }
241
282
  }
242
283