@bjj/nuxt-layer-shadcn-ui 0.12.13 → 0.12.14

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.
@@ -12,12 +12,9 @@ const formatOptions = computed<Intl.NumberFormatOptions>(() => ({
12
12
  currencyDisplay: props.currencyDisplay,
13
13
  }))
14
14
 
15
- // Derive step from currency's minor unit (e.g. JPY→1, USD→0.01, BHD→0.001)
15
+ // Step matches the currency's minor unit (JPY→1, USD→0.01, BHD→0.001).
16
16
  const defaultStep = computed(() => {
17
- const { minimumFractionDigits } = new Intl.NumberFormat('en', {
18
- style: 'currency',
19
- currency: props.currency,
20
- }).resolvedOptions()
17
+ const { minimumFractionDigits } = new Intl.NumberFormat('en', formatOptions.value).resolvedOptions()
21
18
  return 1 / (10 ** (minimumFractionDigits ?? 0))
22
19
  })
23
20
  </script>
@@ -27,5 +24,6 @@ const defaultStep = computed(() => {
27
24
  :formatOptions="formatOptions"
28
25
  :min="0"
29
26
  :step="defaultStep"
27
+ @beforeinput="guardSymbolDeletion"
30
28
  />
31
29
  </template>
@@ -10,5 +10,6 @@ defineProps<InputPercentProps>()
10
10
  :step="0.01"
11
11
  :min="0"
12
12
  :max="1"
13
+ @beforeinput="guardSymbolDeletion"
13
14
  />
14
15
  </template>
@@ -0,0 +1,39 @@
1
+ // A symbol is letters / currency signs / percent; digits and separators (",.' "…)
2
+ // belong to the number and stay editable.
3
+ const LEADING_SYMBOL = /^[\p{L}\p{Sc}%]*/u
4
+ const TRAILING_SYMBOL = /[\p{L}\p{Sc}%]*$/u
5
+
6
+ /**
7
+ * `beforeinput` handler for InputNumber-based fields (currency, percent, unit …) that
8
+ * blocks deletions which would erase part of the value's symbol, keeping it intact.
9
+ * The symbol is the run of symbol characters at each edge of the displayed value, so
10
+ * it works for any currency, format, and locale. A full-selection delete that clears
11
+ * the field is allowed. Bind via `@beforeinput`; the event bubbles up from the inner input.
12
+ */
13
+ export function guardSymbolDeletion (event: Event) {
14
+ const e = event as InputEvent
15
+ const input = e.target as HTMLInputElement | null
16
+ if (!input || !e.inputType.startsWith('delete')) return
17
+
18
+ const { value } = input
19
+ const start = input.selectionStart ?? 0
20
+ const end = input.selectionEnd ?? start
21
+
22
+ // Clearing the whole field via a full selection is always allowed.
23
+ if (start !== end && start === 0 && end === value.length) return
24
+
25
+ const symbolEnd = value.match(LEADING_SYMBOL)![0].length
26
+ const symbolStart = value.length - value.match(TRAILING_SYMBOL)![0].length
27
+
28
+ // Expand a collapsed caret to the character the deletion would remove.
29
+ let from = start
30
+ let to = end
31
+ if (start === end) {
32
+ if (e.inputType.endsWith('Forward')) to = start + 1
33
+ else from = start - 1
34
+ }
35
+
36
+ const hitsPrefix = from < symbolEnd && to > 0
37
+ const hitsSuffix = from < value.length && to > symbolStart
38
+ if (hitsPrefix || hitsSuffix) e.preventDefault()
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bjj/nuxt-layer-shadcn-ui",
3
- "version": "0.12.13",
3
+ "version": "0.12.14",
4
4
  "description": "Nuxt layer providing shadcn-vue based UI components",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -42,5 +42,5 @@
42
42
  "vue-i18n": "^11",
43
43
  "vue-router": "^4 || ^5"
44
44
  },
45
- "gitHead": "ac169020da6ae2c20d53c89d7c3b628f52d806c9"
45
+ "gitHead": "cdedf91b5298fdb1f48097aebe86b9f87defa5ea"
46
46
  }