Skip to content

Commit cbdeb3e

Browse files
committed
Reflect value change based on other input #8
1 parent a296162 commit cbdeb3e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-numeric",
3-
"version": "1.5.1",
3+
"version": "1.5.2",
44
"description": "Input field component to display currency value based on Vue.",
55
"author": "Kevin Ongko",
66
"main": "src/vue-numeric.vue",

src/vue-numeric.vue

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<input
33
:placeholder="placeholder"
44
:value="value"
5-
@blur="formatValue(amountValue)"
5+
@blur="formatValue"
66
@input="processValue(amountValue)"
7-
@focus="convertToNumber"
7+
@focus="convertToNumber(numberValue)"
88
ref="numeric"
99
type="tel"
1010
v-model="amount"
@@ -102,6 +102,14 @@ export default {
102102
return this.formatToNumber(this.amount)
103103
},
104104
105+
/**
106+
* Number type from value props.
107+
* @return {Number}
108+
*/
109+
numberValue () {
110+
return this.formatToNumber(this.value)
111+
},
112+
105113
/**
106114
* Number formatted minimum value.
107115
* @return {Number}
@@ -204,7 +212,7 @@ export default {
204212
* Format value using symbol and separator.
205213
*/
206214
formatValue () {
207-
this.amount = accounting.formatMoney(this.value, {
215+
this.amount = accounting.formatMoney(this.numberValue, {
208216
symbol: this.currency + ' ',
209217
precision: Number(this.precision),
210218
decimal: this.decimalSeparator,
@@ -221,10 +229,11 @@ export default {
221229
},
222230
223231
/**
224-
* Remove symbol and separator on focus.
232+
* Remove symbol and separator.
233+
* @param {Number} value
225234
*/
226-
convertToNumber () {
227-
this.amount = accounting.formatMoney(this.value, {
235+
convertToNumber (value) {
236+
this.amount = accounting.formatMoney(value, {
228237
symbol: '',
229238
precision: Number(this.precision),
230239
decimal: this.decimalSeparator,
@@ -233,6 +242,19 @@ export default {
233242
}
234243
},
235244
245+
watch: {
246+
/**
247+
* Watch for value change from other input.
248+
* @param {Number} val
249+
* @param {Number} oldVal
250+
*/
251+
numberValue (val, oldVal) {
252+
if (this.amountValue !== val && this.amountValue === oldVal) {
253+
this.convertToNumber(val)
254+
}
255+
}
256+
},
257+
236258
mounted () {
237259
// Check default value from parent v-model.
238260
if (this.value) {

0 commit comments

Comments
 (0)