Skip to content

Commit ab94c09

Browse files
committed
2.0.0
1 parent 736b220 commit ab94c09

File tree

6 files changed

+130
-173
lines changed

6 files changed

+130
-173
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ By default the decimal value is disabled. To use decimals in the value, add the
135135
|-----|-----------|--------|----|-------|
136136
|currency|Currency prefix|false|String|-|
137137
|currency-symbol-position|Position of the symbol (accepted values: `prefix` or `suffix`)|false|String|`prefix`|
138-
|max|Maximum value allowed|false|Number, String|-|
139-
|min|Minimum value allowed|false|Number, String|0|
138+
|max|Maximum value allowed|false|Number|-|
139+
|min|Minimum value allowed|false|Number|0|
140140
|minus|Enable/disable negative values|false|Boolean|`false`|
141141
|placeholder|Input placeholder|false|String|-|
142142
|precision|Number of decimals|false|Number, String|-|

dist/vue-numeric.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-numeric",
3-
"version": "1.6.2",
3+
"version": "2.0.0",
44
"description": "Input field component to display currency value based on Vue.",
55
"author": "Kevin Ongko",
66
"main": "dist/vue-numeric.min.js",
@@ -44,7 +44,7 @@
4444
"clean-webpack-plugin": "^0.1.16",
4545
"codecov": "^2.3.0",
4646
"css-loader": "^0.28.7",
47-
"eslint": "^4.5.0",
47+
"eslint": "^4.6.0",
4848
"eslint-plugin-vue": "3.12.0",
4949
"karma": "^1.7.1",
5050
"karma-coverage": "^1.1.1",

src/vue-numeric.vue

Lines changed: 61 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<input
33
:placeholder="placeholder"
44
:value="value"
5-
@blur="formatValue"
6-
@input="processValue(amountValue)"
7-
@focus="convertToNumber(numberValue)"
5+
@blur="onBlurHandler"
6+
@input="onInputHandler"
7+
@focus="onFocusHandler"
88
ref="numeric"
99
type="tel"
1010
v-model="amount"
@@ -34,7 +34,7 @@ export default {
3434
*/
3535
max: {
3636
required: false,
37-
type: [Number, String]
37+
type: Number
3838
},
3939
4040
/**
@@ -43,7 +43,7 @@ export default {
4343
min: {
4444
default: 0,
4545
required: false,
46-
type: [Number, String]
46+
type: Number
4747
},
4848
4949
/**
@@ -69,7 +69,7 @@ export default {
6969
*/
7070
precision: {
7171
required: false,
72-
type: [Number, String]
72+
type: Number
7373
},
7474
7575
/**
@@ -86,6 +86,7 @@ export default {
8686
* v-model value.
8787
*/
8888
value: {
89+
default: 0,
8990
required: true,
9091
type: [Number, String]
9192
},
@@ -125,37 +126,19 @@ export default {
125126
126127
computed: {
127128
/**
128-
* Number formatted user typed value.
129-
* @return {Number}
130-
*/
131-
amountValue () {
132-
return this.formatToNumber(this.amount)
133-
},
134-
135-
/**
136-
* Number type from value props.
129+
* Number type of formatted value.
137130
* @return {Number}
138131
*/
139-
numberValue () {
140-
return this.formatToNumber(this.value)
132+
amountNumber () {
133+
return this.unformat(this.amount)
141134
},
142135
143136
/**
144-
* Number formatted minimum value.
137+
* Number type of value props.
145138
* @return {Number}
146139
*/
147-
minValue () {
148-
if (this.min) return this.formatToNumber(this.min)
149-
return 0
150-
},
151-
152-
/**
153-
* Number formatted maximum value.
154-
* @return {Number|undefined}
155-
*/
156-
maxValue () {
157-
if (this.max) return this.formatToNumber(this.max)
158-
return undefined
140+
valueNumber () {
141+
return this.unformat(this.value)
159142
},
160143
161144
/**
@@ -177,147 +160,103 @@ export default {
177160
},
178161
179162
/**
180-
* Define format for currency symbol and value.
163+
* Define format position for currency symbol and value.
181164
* @return {String} format
182165
*/
183-
formatString () {
166+
symbolPosition () {
184167
if (!this.currency) return '%v'
185168
return this.currencySymbolPosition === 'suffix' ? '%v %s' : '%s %v'
186169
}
187170
},
188171
189172
methods: {
190173
/**
191-
* Check provided value againts maximum allowed.
192-
* @param {Number} value
193-
* @return {Boolean}
174+
* Handle blur event.
194175
*/
195-
checkMaxValue (value) {
196-
if (this.max) {
197-
if (value <= this.maxValue) return false
198-
return true
199-
}
200-
return false
176+
onBlurHandler () {
177+
this.amount = this.format(this.valueNumber)
201178
},
202179
203180
/**
204-
* Check provided value againts minimum allowed.
205-
* @param {Number} value
206-
* @return {Boolean}
181+
* Handle focus event.
207182
*/
208-
checkMinValue (value) {
209-
if (this.min) {
210-
if (value >= this.minValue) return false
211-
return true
212-
}
213-
return false
183+
onFocusHandler () {
184+
this.amount = this.valueNumber
214185
},
215186
216187
/**
217-
* Format provided value to number type.
218-
* @param {String} value
219-
* @return {Number}
188+
* Handle input event.
220189
*/
221-
formatToNumber (value) {
222-
let number = 0
223-
224-
if (this.separator === '.') {
225-
let cleanValue = value
226-
if (typeof value !== 'string') {
227-
cleanValue = this.numberToString(value)
228-
}
229-
number = Number(String(cleanValue).replace(/[^0-9-,]+/g, '').replace(',', '.'))
230-
} else {
231-
number = Number(String(value).replace(/[^0-9-.]+/g, ''))
232-
}
233-
234-
if (!this.minus) return Math.abs(number)
235-
return number
190+
onInputHandler () {
191+
this.process(this.amountNumber)
236192
},
237193
238194
/**
239-
* Validate value before apply to the component.
195+
* Validate value before update the component.
240196
* @param {Number} value
241197
*/
242-
processValue (value) {
243-
if (isNaN(value)) {
244-
this.updateValue(this.minValue)
245-
} else if (this.checkMaxValue(value)) {
246-
this.updateValue(this.maxValue)
247-
} else if (this.checkMinValue(value)) {
248-
this.updateValue(this.minValue)
249-
} else {
250-
this.updateValue(value)
251-
}
252-
},
253-
254-
/**
255-
* Format value using symbol and separator.
256-
*/
257-
formatValue () {
258-
this.amount = accounting.formatMoney(this.numberValue, {
259-
symbol: this.currency,
260-
format: this.formatString,
261-
precision: Number(this.precision),
262-
decimal: this.decimalSeparator,
263-
thousand: this.thousandSeparator
264-
})
198+
process (value) {
199+
if (value >= this.max) this.update(this.max)
200+
if (value <= this.min) this.update(this.min)
201+
if (value > this.min && value < this.max) this.update(value)
202+
if (!this.minus && value < 0) this.update(0)
265203
},
266204
267205
/**
268206
* Update parent component model value.
269207
* @param {Number} value
270208
*/
271-
updateValue (value) {
209+
update (value) {
272210
this.$emit('input', Number(accounting.toFixed(value, this.precision)))
273211
},
274212
275213
/**
276-
* Remove symbol and separator.
214+
* Format value using symbol and separator.
277215
* @param {Number} value
216+
* @return {String}
278217
*/
279-
numberToString (value) {
218+
format (value) {
280219
return accounting.formatMoney(value, {
281-
symbol: '',
220+
symbol: this.currency,
221+
format: this.symbolPosition,
282222
precision: Number(this.precision),
283223
decimal: this.decimalSeparator,
284-
thousand: ''
224+
thousand: this.thousandSeparator
285225
})
286226
},
287227
288228
/**
289229
* Remove symbol and separator.
290230
* @param {Number} value
231+
* @return {Number}
291232
*/
292-
convertToNumber (value) {
293-
this.amount = this.numberToString(value)
233+
unformat (value) {
234+
return accounting.unformat(value, this.decimalSeparator)
294235
}
295236
},
296237
297238
watch: {
298239
/**
299-
* Watch for value change from other input.
300-
*
301-
* @param {Number} val
302-
* @param {Number} oldVal
240+
* Watch for value change from other input with same v-model.
241+
* @param {Number} newValue
242+
* @param {Number} oldValue
303243
*/
304-
numberValue (val, oldVal) {
305-
if (this.amountValue !== val && this.amountValue === oldVal) {
306-
this.convertToNumber(val)
244+
valueNumber (newValue, oldValue) {
245+
if (this.amountNumber !== newValue && this.amountNumber === oldValue) {
246+
this.amount = newValue
307247
if (this.$refs.numeric !== document.activeElement) {
308-
this.formatValue(val)
248+
this.amount = this.format(newValue)
309249
}
310250
}
311251
},
312252
313253
/**
314254
* When readOnly is true, replace the span tag class.
315-
*
316-
* @param {Boolean} val
317-
* @param {Boolean} oldVal
255+
* @param {Boolean} newValue
256+
* @param {Boolean} oldValue
318257
*/
319-
readOnly (val, oldVal) {
320-
if (oldVal === false && val === true) {
258+
readOnly (newValue, oldValue) {
259+
if (oldValue === false && newValue === true) {
321260
this.$nextTick(() => {
322261
this.$refs.readOnly.className = this.readOnlyClass
323262
})
@@ -326,22 +265,22 @@ export default {
326265
},
327266
328267
mounted () {
329-
// Check default value from parent v-model.
330-
if (this.value) {
331-
this.processValue(this.formatToNumber(this.value))
332-
this.formatValue(this.value)
268+
// Set default value props when placeholder undefined.
269+
if (!this.placeholder) {
270+
this.process(this.valueNumber)
271+
this.amount = this.format(this.valueNumber)
272+
273+
// In case of delayed props value.
274+
setTimeout(() => {
275+
this.process(this.valueNumber)
276+
this.amount = this.format(this.valueNumber)
277+
}, 500)
333278
}
334279
335280
// Set read-only span element's class
336281
if (this.readOnly) {
337282
this.$refs.readOnly.className = this.readOnlyClass
338283
}
339-
340-
// In case of delayed v-model new value.
341-
setTimeout(() => {
342-
this.processValue(this.formatToNumber(this.value))
343-
this.formatValue(this.value)
344-
}, 500)
345284
}
346285
}
347286
</script>

0 commit comments

Comments
 (0)