bootstrap-input-spinner 5.0.5 → 5.0.6
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 +1 -1
- package/src/InputSpinner.js +27 -12
- package/test/TestInputSpinner.js +27 -0
package/package.json
CHANGED
package/src/InputSpinner.js
CHANGED
|
@@ -8,26 +8,41 @@
|
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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(
|
|
17
|
-
.replace(
|
|
18
|
-
.replace(
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
package/test/TestInputSpinner.js
CHANGED
|
@@ -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", () => {
|