Skip to content

Commit f35d480

Browse files
authored
Merge pull request #9 from emilioeduardob/fix-watching-values
Fix watching values
2 parents cbdeb3e + 93d179c commit f35d480

File tree

6 files changed

+4497
-4
lines changed

6 files changed

+4497
-4
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

karma.conf.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://github.com/Nikku/karma-browserify
2+
3+
module.exports = function(config) {
4+
5+
config.set({
6+
browsers: [
7+
'PhantomJS',
8+
// 'Chrome'
9+
],
10+
frameworks: ['jasmine'],
11+
files: ['spec/**/*.js'],
12+
reporters: ['spec'],
13+
preprocessors: {
14+
'spec/**/*.js': ['webpack']
15+
},
16+
webpack: {
17+
resolve: {
18+
alias: {
19+
'vue$': 'vue/dist/vue'
20+
}
21+
},
22+
module: {
23+
rules: [{
24+
test: /\.vue$/,
25+
loader: 'vue-loader'
26+
}, {
27+
test: /\.js$/,
28+
loader: 'babel-loader',
29+
exclude: /node_modules/
30+
}]
31+
}
32+
},
33+
// if you want to continuously re-run tests on file-save,
34+
// replace the following line with `autoWatch: true`
35+
singleRun: true
36+
})
37+
}

package.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,29 @@
2626
"license": "MIT",
2727
"dependencies": {
2828
"accounting-js": "^1.1.1"
29-
}
29+
},
30+
"scripts": {
31+
"test": "karma start",
32+
"test:watch": "karma start --single-run=false"
33+
},
34+
"devDependencies": {
35+
"babel-core": "^6.21.0",
36+
"babel-loader": "^6.0.0",
37+
"babel-preset-es2015": "^6.18.0",
38+
"babel-register": "^6.23.0",
39+
"cross-env": "^4.0.0",
40+
"jasmine": "^2.5.3",
41+
"karma": "^1.4.0",
42+
"karma-chrome-launcher": "^2.0.0",
43+
"karma-jasmine": "^1.1.0",
44+
"karma-phantomjs-launcher": "^1.0.2",
45+
"karma-spec-reporter": "0.0.26",
46+
"karma-webpack": "^2.0.3",
47+
"vue": "^2.1.8",
48+
"vue-loader": "^11.1.4",
49+
"vue-router": "^2.4.0",
50+
"vue-template-compiler": "^2.2.1",
51+
"webpack": "^2.2.0",
52+
"webpack-dev-server": "^2.2.0"
53+
}
3054
}

spec/vue_numeric.spec.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import Vue from 'vue';
2+
Vue.config.productionTip = false;
3+
import VueNumeric from './../src/vue-numeric.vue';
4+
5+
function getInput(Component, propsData) {
6+
const Ctor = Vue.extend(Component)
7+
const vm = new Ctor({ propsData: propsData }).$mount()
8+
return vm.$el;
9+
}
10+
11+
describe("vue-numeric", function() {
12+
13+
let el;
14+
15+
beforeEach(() => {
16+
el = document.createElement('div');
17+
18+
document.body.appendChild(el);
19+
});
20+
21+
it("Uses the default decimal separator ,", done => {
22+
23+
var input = getInput(VueNumeric, { value: '2000' });
24+
Vue.nextTick(() => {
25+
expect(input.value).toEqual(' 2,000')
26+
done()
27+
});
28+
29+
});
30+
31+
it('updates value with format if without focus', done => {
32+
const vm = new Vue({
33+
el,
34+
data: function() {
35+
return {
36+
total: 0
37+
}
38+
},
39+
template: '<div><vue-numeric v-model="total"></vue-numeric></div>',
40+
components: { VueNumeric }
41+
}).$mount()
42+
43+
vm.total = 3000;
44+
45+
Vue.nextTick(() => {
46+
expect(vm.$el.firstChild.value.trim()).toEqual('3,000')
47+
done()
48+
});
49+
50+
});
51+
52+
it('updates values decimals', done => {
53+
const vm = new Vue({
54+
el,
55+
data: function() {
56+
return {
57+
total: 0
58+
}
59+
},
60+
template: `<div>
61+
<vue-numeric v-model="total" separator="." precision="2"></vue-numeric>
62+
<span>{{total}}</span>
63+
</div>`,
64+
components: { VueNumeric }
65+
}).$mount()
66+
67+
vm.total = 3000;
68+
69+
Vue.nextTick(() => {
70+
const span = vm.$el.getElementsByTagName('span')[0];
71+
expect(span.textContent.trim()).toEqual('3000');
72+
expect(vm.$el.firstChild.value.trim()).toEqual('3.000,00')
73+
done()
74+
});
75+
76+
});
77+
78+
79+
it('accepts decimal values', done => {
80+
const vm = new Vue({
81+
el,
82+
data: function() {
83+
return {
84+
total: 200.22,
85+
subtotal: "110.98",
86+
large: "10.000,1"
87+
};
88+
},
89+
template: `<div>
90+
<vue-numeric v-model="large" separator="." precision="2"></vue-numeric>
91+
<vue-numeric v-model="total" separator="." precision="2"></vue-numeric>
92+
<vue-numeric v-model="subtotal" precision="2"></vue-numeric>
93+
</div>`,
94+
components: { VueNumeric }
95+
}).$mount()
96+
97+
Vue.nextTick(() => {
98+
expect(vm.$el.children[0].value.trim()).toEqual('10.000,10')
99+
expect(vm.$el.children[1].value.trim()).toEqual('200,22')
100+
expect(vm.$el.children[2].value.trim()).toEqual('110.98')
101+
done()
102+
});
103+
104+
});
105+
106+
107+
it('updates values with correct separator', done => {
108+
const vm = new Vue({
109+
el,
110+
data: function() {
111+
return {
112+
total: 0
113+
}
114+
},
115+
template: `<div>
116+
<vue-numeric v-model="total" separator="."></vue-numeric>
117+
<span>{{total}}</span>
118+
</div>`,
119+
components: { VueNumeric }
120+
}).$mount()
121+
122+
vm.total = 3000;
123+
124+
Vue.nextTick(() => {
125+
const span = vm.$el.getElementsByTagName('span')[0];
126+
expect(span.textContent.trim()).toEqual('3000');
127+
expect(vm.$el.firstChild.value.trim()).toEqual('3.000')
128+
done()
129+
});
130+
131+
});
132+
133+
134+
it('updates value without format', done => {
135+
const vm = new Vue({
136+
el,
137+
data: function() {
138+
return {
139+
total: 0
140+
}
141+
},
142+
template: '<div><vue-numeric v-model="total"></vue-numeric></div>',
143+
components: { VueNumeric }
144+
}).$mount()
145+
146+
vm.$el.firstChild.focus();
147+
vm.total = 3000;
148+
149+
Vue.nextTick(() => {
150+
expect(vm.$el.firstChild.value.trim()).toEqual('3000');
151+
done()
152+
});
153+
154+
});
155+
156+
157+
});

src/vue-numeric.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ export default {
183183
let number = 0
184184
185185
if (this.separator === '.') {
186-
number = Number(String(value).replace(/[^0-9-,]+/g, '').replace(',', '.'))
186+
let cleanValue = value;
187+
if (typeof value != 'string') {
188+
cleanValue = this.numberToString(value);
189+
}
190+
number = Number(String(cleanValue).replace(/[^0-9-,]+/g, '').replace(',', '.'))
187191
} else {
188192
number = Number(String(value).replace(/[^0-9-.]+/g, ''))
189193
}
@@ -232,13 +236,21 @@ export default {
232236
* Remove symbol and separator.
233237
* @param {Number} value
234238
*/
235-
convertToNumber (value) {
236-
this.amount = accounting.formatMoney(value, {
239+
numberToString (value) {
240+
return accounting.formatMoney(value, {
237241
symbol: '',
238242
precision: Number(this.precision),
239243
decimal: this.decimalSeparator,
240244
thousand: ''
241245
})
246+
},
247+
248+
/**
249+
* Remove symbol and separator.
250+
* @param {Number} value
251+
*/
252+
convertToNumber (value) {
253+
this.amount = this.numberToString(value);
242254
}
243255
},
244256
@@ -251,6 +263,9 @@ export default {
251263
numberValue (val, oldVal) {
252264
if (this.amountValue !== val && this.amountValue === oldVal) {
253265
this.convertToNumber(val)
266+
if (this.$el !== document.activeElement) {
267+
this.formatValue(val)
268+
}
254269
}
255270
}
256271
},

0 commit comments

Comments
 (0)