Skip to content

Commit 7fad5f7

Browse files
tochoromerokevinongko
authored andcommitted
Allow to configure the value set when the input is cleared out (#48)
* Allow to configure the value when the input is empty * Add empty value tests
1 parent 23950a0 commit 7fad5f7

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ By default the decimal value is disabled. To use decimals in the value, add the
131131
<vue-numeric placeholder="only number allowed"></vue-numeric>
132132
```
133133

134+
### Value when empty
135+
By default, when you clean the input the value is set to `0`. You can change this value to fit your needs.
136+
```vue
137+
<vue-numeric :empty-value="1"></vue-numeric>
138+
```
139+
134140
## Props
135141
|Props|Description|Required|Type|Default|
136142
|-----|-----------|--------|----|-------|
@@ -140,6 +146,7 @@ By default the decimal value is disabled. To use decimals in the value, add the
140146
|min|Minimum value allowed|false|Number|-9007199254740991|
141147
|minus|Enable/disable negative values|false|Boolean|`false`|
142148
|placeholder|Input placeholder|false|String|-|
149+
|empty-value|Value when input is empty|false|Number|0|
143150
|precision|Number of decimals|false|Number|-|
144151
|separator|Thousand separator symbol (accepts `space`, `.` or `,`)|false|String|`,`|
145152
|read-only|Hide input field and show the value as text|false|Boolean|false|

src/vue-numeric.vue

+11-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ export default {
6363
type: String
6464
},
6565
66+
/**
67+
* Value when the input is empty
68+
*/
69+
emptyValue: {
70+
default: '',
71+
required: false,
72+
type: [Number, String]
73+
},
74+
6675
/**
6776
* Number of decimals.
6877
* Decimals symbol are the opposite of separator symbol.
@@ -246,7 +255,8 @@ export default {
246255
* @return {Number}
247256
*/
248257
unformat (value) {
249-
return accounting.unformat(value, this.decimalSeparator)
258+
const toUnformat = typeof value === 'string' && value === '' ? this.emptyValue : value
259+
return accounting.unformat(toUnformat, this.decimalSeparator)
250260
}
251261
},
252262

test/specs/vue-numeric.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ describe('vue-numeric.vue', () => {
202202
expect(wrapper.data().amount).to.equal('')
203203
})
204204

205+
it('sets the value to 0 when no empty value is provided and input is empty', () => {
206+
const wrapper = mount(VueNumeric, { propsData: { value: '' }})
207+
expect(wrapper.data().amount).to.equal('0')
208+
})
209+
210+
it('uses the provided empty value when input is empty', () => {
211+
const wrapper = mount(VueNumeric, { propsData: { value: '', emptyValue: 1 }})
212+
expect(wrapper.data().amount).to.equal('1')
213+
})
214+
205215
it('apply min props value if user input negative value when minus props disabled', () => {
206216
const component = Vue.extend({
207217
data: () => ({ total: -200 }),

0 commit comments

Comments
 (0)