Skip to content

Commit a7bd35d

Browse files
committed
[Fix] Issue of prefix with decimal
1 parent dff1c6a commit a7bd35d

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

docs/.vuepress/components/PlayGround.vue

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<div class="grid">
55
<div class="font-medium mb-2">Component</div>
66
<number
7+
v-if="updated"
78
v-model="price"
89
v-bind="config"
910
class="shadow-sm rounded-md text-base transition-all disabled:cursor-not-allowed disabled:border-gray-300 disabled:text-gray-300 focus:border-primary focus:ring focus:ring-offset-0 focus:ring-primary focus:ring-opacity-50"

src/directive.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default {
1717
el = core.getInputElement(el)
1818
const option = el[CONFIG_KEY]
1919
const { config } = option
20+
2021
// prefer adding event listener to parent element to avoid Firefox bug which does not
2122
// execute `useCapture: true` event handlers before non-capturing event handlers
2223
const handlerOwner = el.parentElement || el
@@ -36,9 +37,12 @@ export default {
3637
// check decimal key and insert to current element
3738
// updated cursor position after format the value
3839
el.onkeydown = (e) => {
40+
const { target } = e
41+
const regExp = new RegExp(`${config.prefix}|${config.suffix}`, 'g')
42+
let newValue = target.value.replace(regExp, '')
3943
if (
4044
([110, 190].includes(e.keyCode) || e.key === config.decimal) &&
41-
el.value.includes(config.decimal)
45+
newValue.includes(config.decimal)
4246
) {
4347
e.preventDefault()
4448
} else if ([8].includes(e.keyCode)) {
@@ -72,9 +76,9 @@ export default {
7276

7377
updated: (el, { value, oldValue, modifiers }, vnode) => {
7478
el = core.getInputElement(el)
79+
const { config } = el[CONFIG_KEY]
80+
el[CONFIG_KEY].config = Object.assign({}, config, value, modifiers)
7581
if (value !== oldValue) {
76-
const { config } = el[CONFIG_KEY]
77-
el[CONFIG_KEY].config = Object.assign({}, config, value, modifiers)
7882
core.updateValue(el, vnode, { force: true, clean: true })
7983
} else {
8084
core.updateValue(el, vnode)

src/number-format.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ export default function NumberFormat(config = options) {
1010
this.number = ''
1111
this.isClean = true
1212

13+
const { prefix, suffix, decimal } = options
14+
15+
this.preSurRegExp = new RegExp(`${prefix}|${suffix}`, 'g')
16+
this.numberRegExp = new RegExp(`[^0-9\\${decimal}]+`, 'gi')
17+
this.cleanRegExp = new RegExp('[^0-9]+', 'gi')
18+
this.negativeRegExp = new RegExp('[^0-9\\-]+', 'gi')
19+
1320
this.isNull = (input = this.input) => {
1421
if (this.isClean) {
15-
return !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
22+
return !this.numberOnly(input, this.cleanRegExp)
1623
}
17-
return !this.numberOnly(input, new RegExp('[^0-9\\-]+', 'gi'))
24+
return !this.numberOnly(input, this.negativeRegExp)
1825
}
1926

2027
this.clean = (clean = false) => {
@@ -65,10 +72,8 @@ export default function NumberFormat(config = options) {
6572
'.'
6673
).join(this.options.decimal)
6774
} else {
68-
this.number = this.numberOnly(
69-
this.input,
70-
new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi')
71-
)
75+
const input = this.input.replace(this.preSurRegExp, '')
76+
this.number = this.numberOnly(input, this.numberRegExp)
7277
this.number = this.parts(this.number).join(this.options.decimal)
7378
}
7479
return this.number

0 commit comments

Comments
 (0)