bootstrap-input-spinner 5.0.5 → 5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bootstrap-input-spinner",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "A Bootstrap 5 plugin to create input spinner elements for number input.",
5
5
  "browser": "./src/InputSpinner.js",
6
6
  "type": "module",
@@ -8,31 +8,44 @@
8
8
  const I18nEditor = function (props, element) {
9
9
  const locale = props.locale || "en-US"
10
10
 
11
+ let parseRegexes = null
11
12
  this.parse = function (customFormat) {
12
- const numberFormat = new Intl.NumberFormat(locale)
13
- const thousandSeparator = numberFormat.format(11111).replace(/1/g, '') || '.'
14
- const decimalSeparator = numberFormat.format(1.1).replace(/1/g, '')
13
+ if (!parseRegexes) {
14
+ const fmt = new Intl.NumberFormat(locale)
15
+ const thousandSeparator = fmt.format(11111).replace(/1/g, '') || '.'
16
+ const decimalSeparator = fmt.format(1.1).replace(/1/g, '')
17
+ parseRegexes = {
18
+ space: / /g,
19
+ thousand: new RegExp('\\' + thousandSeparator, 'g'),
20
+ decimal: new RegExp('\\' + decimalSeparator)
21
+ }
22
+ }
15
23
  return parseFloat(customFormat
16
- .replace(new RegExp(' ', 'g'), '')
17
- .replace(new RegExp('\\' + thousandSeparator, 'g'), '')
18
- .replace(new RegExp('\\' + decimalSeparator), '.')
24
+ .replace(parseRegexes.space, '')
25
+ .replace(parseRegexes.thousand, '')
26
+ .replace(parseRegexes.decimal, '.')
19
27
  )
20
28
  }
21
29
 
30
+ let renderFmt = null
31
+ let renderDecimals = -1
32
+ let renderGrouping = null
22
33
  this.render = function (number) {
23
34
  const decimals = parseInt(element.getAttribute("data-decimals")) || 0
24
35
  const digitGrouping = !(element.getAttribute("data-digit-grouping") === "false")
25
- const numberFormat = new Intl.NumberFormat(locale, {
26
- minimumFractionDigits: decimals,
27
- maximumFractionDigits: decimals,
28
- useGrouping: digitGrouping
29
- })
30
- return numberFormat.format(number)
36
+ if (!renderFmt || decimals !== renderDecimals || digitGrouping !== renderGrouping) {
37
+ renderFmt = new Intl.NumberFormat(locale, {
38
+ minimumFractionDigits: decimals,
39
+ maximumFractionDigits: decimals,
40
+ useGrouping: digitGrouping
41
+ })
42
+ renderDecimals = decimals
43
+ renderGrouping = digitGrouping
44
+ }
45
+ return renderFmt.format(number)
31
46
  }
32
47
  }
33
48
 
34
- let triggerKeyPressed = false
35
-
36
49
  function parseTemplate(html) {
37
50
  const tpl = document.createElement("template")
38
51
  tpl.innerHTML = html.trim()
@@ -144,6 +157,8 @@ export class InputSpinner {
144
157
  destroy()
145
158
  }
146
159
 
160
+ let triggerKeyPressed = false
161
+
147
162
  this.observer = new MutationObserver(function () {
148
163
  updateAttributes()
149
164
  setValue(self.value, true)
@@ -349,9 +364,9 @@ export class InputSpinner {
349
364
  }
350
365
  const originalClass = self.original.className || ""
351
366
  let groupClass = ""
352
- if (/form-control-sm/g.test(originalClass)) {
367
+ if (/form-control-sm/.test(originalClass)) {
353
368
  groupClass = "input-group-sm"
354
- } else if (/form-control-lg/g.test(originalClass)) {
369
+ } else if (/form-control-lg/.test(originalClass)) {
355
370
  groupClass = "input-group-lg"
356
371
  }
357
372
  const inputClass = originalClass.replace(/form-control(-(sm|lg))?/g, "")
@@ -95,6 +95,33 @@ describe("InputSpinner I18n rendering (default editor)", () => {
95
95
  assert.equal(group.querySelector("input").value, "1.234,5")
96
96
  clear()
97
97
  })
98
+ it("re-renders when data-decimals changes (formatter cache invalidates)", async () => {
99
+ const {el, group} = spin({value: "4.5", "data-decimals": "0"}, {locale: "en-US"})
100
+ assert.equal(group.querySelector("input").value, "5")
101
+ el.setAttribute("data-decimals", "2")
102
+ await wait()
103
+ assert.equal(group.querySelector("input").value, "4.50")
104
+ clear()
105
+ })
106
+ it("re-renders when data-digit-grouping changes (formatter cache invalidates)", async () => {
107
+ const {el, group} = spin({value: "12345"}, {locale: "en-US"})
108
+ assert.equal(group.querySelector("input").value, "12,345")
109
+ el.setAttribute("data-digit-grouping", "false")
110
+ await wait()
111
+ assert.equal(group.querySelector("input").value, "12345")
112
+ clear()
113
+ })
114
+ it("parses i18n input round-trips after multiple calls (separator cache stable)", () => {
115
+ const {el, group} = spin({value: "0", "data-decimals": "2"}, {locale: "de-DE"})
116
+ const input = group.querySelector("input")
117
+ input.value = "1.234,56"
118
+ input.dispatchEvent(new Event("input", {bubbles: true}))
119
+ assert.equal(parseFloat(el.value), 1234.56)
120
+ input.value = "9.876,54"
121
+ input.dispatchEvent(new Event("input", {bubbles: true}))
122
+ assert.equal(parseFloat(el.value), 9876.54)
123
+ clear()
124
+ })
98
125
  })
99
126
 
100
127
  describe("InputSpinner setValue", () => {
@@ -314,6 +341,28 @@ describe("InputSpinner instance isolation", () => {
314
341
  assert.equal(b, 0)
315
342
  clear()
316
343
  })
344
+ it("two spinners both respond to keyboard activation while the other is held", () => {
345
+ const elA = createInput({value: "0", min: "0", max: "10", step: "1"})
346
+ const elB = createInput({value: "0", min: "0", max: "10", step: "1"})
347
+ new InputSpinner(elA, {autoInterval: undefined})
348
+ new InputSpinner(elB, {autoInterval: undefined})
349
+ const btnA = elA.nextElementSibling.querySelector(".btn-increment")
350
+ const btnB = elB.nextElementSibling.querySelector(".btn-increment")
351
+ const make = (type) => {
352
+ const e = new KeyboardEvent(type, {key: " ", bubbles: true})
353
+ Object.defineProperty(e, "keyCode", {value: 32})
354
+ return e
355
+ }
356
+ const down = () => make("keydown")
357
+ const up = () => make("keyup")
358
+ btnA.dispatchEvent(down())
359
+ assert.equal(elA.value, "1")
360
+ // B's keydown must still register even though A's keyup hasn't fired.
361
+ btnB.dispatchEvent(down())
362
+ assert.equal(elB.value, "1")
363
+ document.body.dispatchEvent(up())
364
+ clear()
365
+ })
317
366
  })
318
367
 
319
368
  describe("InputSpinner attribute observation", () => {