Skip to content

Commit 91b06f5

Browse files
tochoromerokevinongko
authored andcommitted
Allow to specify the output type for the input event. (#52)
Supported values are String and Number (default)
1 parent f3f88db commit 91b06f5

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ By default, when you clean the input the value is set to `0`. You can change thi
137137
<vue-numeric :empty-value="1"></vue-numeric>
138138
```
139139

140+
### Output Type
141+
By default the value emitted for the input event is of type `Number`. However you may choose to get a `String` instead
142+
by setting the property `output-type` to `String`.
143+
```vue
144+
<vue-numeric output-type="String"></vue-numeric>
145+
```
146+
140147
## Props
141148
|Props|Description|Required|Type|Default|
142149
|-----|-----------|--------|----|-------|
@@ -147,6 +154,7 @@ By default, when you clean the input the value is set to `0`. You can change thi
147154
|minus|Enable/disable negative values|false|Boolean|`false`|
148155
|placeholder|Input placeholder|false|String|-|
149156
|empty-value|Value when input is empty|false|Number|0|
157+
|output-type|Output Type for input event|false|String|`String`|
150158
|precision|Number of decimals|false|Number|-|
151159
|separator|Thousand separator symbol (accepts `space`, `.` or `,`)|false|String|`,`|
152160
|decimal-separator|Custom decimal separator|false|String|-|

src/vue-numeric.vue

+13-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ export default {
116116
type: String
117117
},
118118
119+
/**
120+
* The output type used for v-model.
121+
* It can either be String or Number (default).
122+
*/
123+
outputType: {
124+
required: false,
125+
type: String,
126+
default: 'Number'
127+
},
128+
119129
/**
120130
* v-model value.
121131
*/
@@ -324,7 +334,9 @@ export default {
324334
* @param {Number} value
325335
*/
326336
update (value) {
327-
this.$emit('input', Number(accounting.toFixed(value, this.precision)))
337+
const fixedValue = accounting.toFixed(value, this.precision)
338+
const output = this.outputType.toLowerCase() === 'string' ? fixedValue : Number(fixedValue)
339+
this.$emit('input', output)
328340
},
329341
330342
/**

test/specs/vue-numeric.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ describe('vue-numeric.vue', () => {
5656
expect(wrapper.data().amount).to.equal('$ 20.000,4')
5757
})
5858

59+
it('outputs Number type by default', () => {
60+
const component = Vue.extend({
61+
data: () => ({ total: 100 }),
62+
template: '<div><vue-numeric v-model="total" :min="1" :max="100"></vue-numeric></div>',
63+
components: { VueNumeric }
64+
})
65+
66+
const wrapper = mount(component)
67+
expect(typeof wrapper.data().total).to.equal('number')
68+
})
69+
70+
it('outputs String if specified', () => {
71+
const component = Vue.extend({
72+
data: () => ({ total: 100 }),
73+
template: '<div><vue-numeric v-model="total" outputType="String" :min="1" :max="100"></vue-numeric></div>',
74+
components: { VueNumeric }
75+
})
76+
77+
const wrapper = mount(component)
78+
expect(typeof wrapper.data().total).to.equal('string')
79+
})
80+
5981
it('is <span> tag in read-only mode', () => {
6082
const wrapper = mount(VueNumeric, { propsData: { value: 2000, currency: '$', readOnly: true, readOnlyClass: 'test-class' }})
6183
wrapper.update()

0 commit comments

Comments
 (0)