Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds onPaste validation, fix adds with decimal and toPresicion bug #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/vue-numeric-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@change="onChange"
@blur="onBlur"
@focus="onFocus"
@paste="onPaste"
@drop.prevent
:autofocus="autofocus"
:disabled="disabled"
:readonly="readonly"
Expand Down Expand Up @@ -46,6 +48,16 @@
<script>
const timeInterval = 100

const isNaN = Number.isNaN || window.isNaN
const REGEXP_NUMBER = /^-?(?:\d+|\d+\.\d+|\.\d+)(?:[eE][-+]?\d+)?$/
const REGEXP_DECIMALS = /\.\d*(?:0|9){10}\d*$/
const normalizeDecimalNumber = (value, times = 100000000000) =>
REGEXP_DECIMALS.test(String(value)) ? Math.round(value * times) / times : value

const round = (number, decimals = 2) => {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals)
}

export default {
name: 'vue-numeric-input',
props: {
Expand Down Expand Up @@ -151,19 +163,23 @@ export default {
* @returns {number | Number}
*/
toPrecision (val, precision) {
return precision !== undefined ? parseFloat(val.toFixed(precision)) : val
return precision !== undefined ? round(val, precision) : val
},
/**
* Increment the current numeric value
*/
increment () {
if (!this.readonly) this.updateValue(this.toNumber(this.numericValue) + this.step)
if (this.readonly || this.disabled) return
const newValue = this.toNumber(this.internalValue) + this.step
this.updateValue(normalizeDecimalNumber(newValue))
},
/**
* Decrement the current numeric value
*/
decrement () {
if (!this.readonly) this.updateValue(this.toNumber(this.numericValue) - this.step)
if (this.readonly || this.disabled) return
const newValue = this.toNumber(this.internalValue) - this.step
this.updateValue(normalizeDecimalNumber(newValue))
},
/**
* Handle value on Input
Expand All @@ -188,6 +204,9 @@ export default {
this.$refs.input.value = strVal && val === this.toNumber(strVal) ? strVal : val
return
}
if (isNaN(val)) {
return
}
this.numericValue = val
this.$emit('input', val)
},
Expand Down Expand Up @@ -217,6 +236,13 @@ export default {
this.startTime = null
if (this.value !== this.numericValue) this.$emit('change', this.numericValue)
},

onPaste(event) {
const clipboardData = event.clipboardData || window.clipboardData
if (clipboardData && !REGEXP_NUMBER.test(clipboardData.getData("text"))) {
event.preventDefault()
}
},
/**
* On blur event trigger
* @param event - blur event on input
Expand Down