From 3ecf61d5a741956352dcd0573c0edce29cb2c08f Mon Sep 17 00:00:00 2001 From: hromero Date: Fri, 24 Nov 2017 14:40:27 -0700 Subject: [PATCH 1/2] Allow to configure the value when the input is empty --- README.md | 7 +++++++ src/vue-numeric.vue | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d33ef4..14c91ca 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,12 @@ By default the decimal value is disabled. To use decimals in the value, add the ``` +### Value when empty +By default, when you clean the input the value is set to `0`. You can change this value to fit your needs. +```vue + +``` + ## Props |Props|Description|Required|Type|Default| |-----|-----------|--------|----|-------| @@ -140,6 +146,7 @@ By default the decimal value is disabled. To use decimals in the value, add the |min|Minimum value allowed|false|Number|-9007199254740991| |minus|Enable/disable negative values|false|Boolean|`false`| |placeholder|Input placeholder|false|String|-| +|empty-value|Value when input is empty|false|Number|0| |precision|Number of decimals|false|Number|-| |separator|Thousand separator symbol (accepts `space`, `.` or `,`)|false|String|`,`| |read-only|Hide input field and show the value as text|false|Boolean|false| diff --git a/src/vue-numeric.vue b/src/vue-numeric.vue index beef6f5..e1f6031 100644 --- a/src/vue-numeric.vue +++ b/src/vue-numeric.vue @@ -63,6 +63,15 @@ export default { type: String }, + /** + * Value when the input is empty + */ + emptyValue: { + default: '', + required: false, + type: [Number, String] + }, + /** * Number of decimals. * Decimals symbol are the opposite of separator symbol. @@ -246,7 +255,8 @@ export default { * @return {Number} */ unformat (value) { - return accounting.unformat(value, this.decimalSeparator) + const toUnformat = typeof value === 'string' && value === '' ? this.emptyValue : value + return accounting.unformat(toUnformat, this.decimalSeparator) } }, From ffff8011db515218ed2f897a928a767a838a467e Mon Sep 17 00:00:00 2001 From: hromero Date: Fri, 24 Nov 2017 14:52:23 -0700 Subject: [PATCH 2/2] Add empty value tests --- test/specs/vue-numeric.spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/specs/vue-numeric.spec.js b/test/specs/vue-numeric.spec.js index 2aeb1cd..179fbf2 100644 --- a/test/specs/vue-numeric.spec.js +++ b/test/specs/vue-numeric.spec.js @@ -202,6 +202,16 @@ describe('vue-numeric.vue', () => { expect(wrapper.data().amount).to.equal('') }) + it('sets the value to 0 when no empty value is provided and input is empty', () => { + const wrapper = mount(VueNumeric, { propsData: { value: '' }}) + expect(wrapper.data().amount).to.equal('0') + }) + + it('uses the provided empty value when input is empty', () => { + const wrapper = mount(VueNumeric, { propsData: { value: '', emptyValue: 1 }}) + expect(wrapper.data().amount).to.equal('1') + }) + it('apply min props value if user input negative value when minus props disabled', () => { const component = Vue.extend({ data: () => ({ total: -200 }),