Skip to content

Add formatting on input #13

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ By default the decimal value is disabled, to use decimal value add `precision` p
|separator|Thousand separator type ( accept either `.` or `,` )|false|String|,|
|read-only|Hide input field and show the value in text|false|Boolean|false|
|read-only-class|Class for read-only element|false|String|''|
|format-input|Enable/disable formatting on input|false|Boolean|false|

## License

Expand Down
60 changes: 60 additions & 0 deletions spec/vue_numeric.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,64 @@ describe('vue-numeric', () => {
done()
})
})

it('Updates value using format-input prop', done => {
const vm = new Vue({
el,
data () {
return {
total: 0
}
},
template: '<div><vue-numeric v-model="total" :format-input="true"></vue-numeric></div>',
components: { VueNumeric }
}).$mount()

vm.total = 3000

Vue.nextTick(() => {
expect(vm.$el.firstChild.value.trim()).toEqual('3,000')
done()
})
})

it('Updates value using format-input prop with decimal', done => {
const vm = new Vue({
el,
data () {
return {
total: 0
}
},
template: '<div><vue-numeric v-model="total" precision="2" :format-input="true"></vue-numeric></div>',
components: { VueNumeric }
}).$mount()

vm.total = 3000.50

Vue.nextTick(() => {
expect(vm.$el.firstChild.value.trim()).toEqual('3,000.50')
done()
})
})

it('Updates value using format-input prop with decimal separator \',\' and currency', done => {
const vm = new Vue({
el,
data () {
return {
total: 0
}
},
template: '<div><vue-numeric v-model="total" precision="2" separator="." currency="$" :format-input="true"></vue-numeric></div>',
components: { VueNumeric }
}).$mount()

vm.total = 3000.50

Vue.nextTick(() => {
expect(vm.$el.firstChild.value.trim()).toEqual('$ 3.000,50')
done()
})
})
})
46 changes: 40 additions & 6 deletions src/vue-numeric.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<input
:placeholder="placeholder"
:value="value"
@blur="formatValue"
@blur="formatValue(amountValue)"
@input="processValue(amountValue)"
@focus="convertToNumber(numberValue)"
@focus="focus"
ref="numeric"
type="tel"
v-model="amount"
Expand Down Expand Up @@ -106,6 +106,15 @@ export default {
default: '',
required: false,
type: String
},

/**
* Enable/Disable formatting on input.
*/
formatInput: {
default: false,
required: false,
type: Boolean
}
},

Expand All @@ -119,6 +128,17 @@ export default {
* @return {Number}
*/
amountValue () {
if (this.formatInput && this.precision > 0) {
let value = this.amount.replace(/[^0-9]/g, '')

if (value === '') {
return this.minValue
}

let decimalSeparatorPosition = value.length - this.precision
return Number(value.slice(0, decimalSeparatorPosition) + '.' + value.slice(decimalSeparatorPosition, value.length))
}

return this.formatToNumber(this.amount)
},

Expand Down Expand Up @@ -230,13 +250,18 @@ export default {
} else {
this.updateValue(value)
}

if (this.formatInput) {
this.formatValue(this.amountValue)
}
},

/**
* Format value using symbol and separator.
* @param {Number} value
*/
formatValue () {
this.amount = accounting.formatMoney(this.numberValue, {
formatValue (value) {
this.amount = accounting.formatMoney(value, {
symbol: this.currency + ' ',
precision: Number(this.precision),
decimal: this.decimalSeparator,
Expand Down Expand Up @@ -271,6 +296,15 @@ export default {
*/
convertToNumber (value) {
this.amount = this.numberToString(value)
},

/**
* Check the format-input props is enable or not
*/
focus () {
if (!this.formatInput) {
this.convertToNumber(this.numberValue)
}
}
},

Expand Down Expand Up @@ -309,7 +343,7 @@ export default {
// Check default value from parent v-model.
if (this.value) {
this.processValue(this.formatToNumber(this.value))
this.formatValue(this.value)
this.formatValue(this.formatToNumber(this.value))
}

// Set read-only span element's class
Expand All @@ -320,7 +354,7 @@ export default {
// In case of delayed v-model new value.
setTimeout(() => {
this.processValue(this.formatToNumber(this.value))
this.formatValue(this.value)
this.formatValue(this.formatToNumber(this.value))
}, 500)
}
}
Expand Down