Skip to content

Commit 0545b26

Browse files
committed
added events
1 parent 5390345 commit 0545b26

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-formly-bootstrap",
3-
"version": "0.0.0",
3+
"version": "0.1.1",
44
"description": "A bootstrap based form input bundle for Vue Formly",
55
"main": "dist/vue-formly-bootstrap.js",
66
"scripts": {

src/fields/baseField.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@ export default
66
],
77
created: function(){
88
this.$set('form.'+this.key+'.$dirty', false);
9+
this.$set('form.'+this.key+'.$active', false);
910
},
1011
methods: {
11-
onBlur: function(event){
12-
console.log('hit');
12+
runFunction: function(action, e){
13+
if ( typeof this.form[this.key][action] == 'function' ) this.form[this.key][action].call(this, e);
14+
},
15+
onFocus: function(e){
16+
this.$set('form.'+this.key+'.$active', true);
17+
this.runFunction('onFocus', e);
18+
},
19+
onBlur: function(e){
1320
this.$set('form.'+this.key+'.$dirty', true);
14-
}
21+
this.$set('form.'+this.key+'.$active', false);
22+
this.runFunction('onBlur', e);
23+
},
24+
onClick: function(e){
25+
this.runFunction('onClick', e);
26+
},
27+
onChange: function(e){
28+
this.$set('form.'+this.key+'.$dirty', true);
29+
this.runFunction('onChange', e);
30+
},
31+
onKeyup: function(e){
32+
this.runFunction('onKeyup', e);
33+
},
34+
onKeydown: function(e){
35+
this.runFunction('onKeydown', e);
36+
}
1537
}
1638
};

src/fields/fieldInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="form-group">
33
<label v-if="form[key].label" :for="form[key].id ? form[key].id : null">{{form[key].label}}</label>
4-
<input class="form-control" :id="form[key].id ? form[key].id : null" :type="form[key].inputType" v-model="form[key].value" :placeholder="form[key].placeholder" @blur="onBlur">
4+
<input class="form-control" :id="form[key].id ? form[key].id : null" :type="form[key].inputType" v-model="form[key].value" :placeholder="form[key].placeholder" @blur="onBlur" @focus="onFocus" @click="onClick" @change="onChange" @keyup="onKeyup" @keydown="onKeydown">
55
</div>
66
</template>
77

test/unit/specs/index.spec.js

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import chai from 'chai';
12
import {expect} from 'chai';
3+
import sinonChai from 'sinon-chai';
4+
import sinon from 'sinon';
25
import Vue from 'vue';
36
import VueFormly from 'vue-formly';
47
import FormlyBootstrap from 'src/index';
58
Vue.use(VueFormly);
69
Vue.use(FormlyBootstrap);
10+
chai.use(sinonChai);
711

8-
let el, vm, data;
12+
let el, vm, data, spy;
913

1014
function createForm(){
1115
el = document.createElement('div');
@@ -19,10 +23,10 @@ function createForm(){
1923
}
2024

2125
function trigger (target, event, process) {
22-
var e = document.createEvent('HTMLEvents')
23-
e.initEvent(event, true, true)
24-
if (process) process(e)
25-
target.dispatchEvent(e)
26+
var e = document.createEvent('HTMLEvents');
27+
e.initEvent(event, true, true);
28+
if (process) process(e);
29+
target.dispatchEvent(e);
2630
}
2731

2832
describe('components', () => {
@@ -42,17 +46,64 @@ describe('components', () => {
4246
beforeEach(() => {
4347
data.form.test.type = 'input';
4448
data.form.test.inputType = 'text';
49+
spy = sinon.spy();
4550
});
4651

47-
it('dirty', () => {
52+
it('dirty/active', () => {
4853
createForm();
4954
expect(vm.form.test.$dirty).to.be.false;
55+
expect(vm.form.test.$active).to.be.false;
5056
});
5157

5258
it('blur', () => {
59+
let copy = {};
60+
data.form.test.onBlur = function(e){
61+
copy = this.form;
62+
};
5363
createForm();
5464
trigger(vm.$el.querySelectorAll('input')[0], 'blur');
65+
expect(vm.form.test.$active).to.be.false;
5566
expect(vm.form.test.$dirty).to.be.true;
67+
//check that "this" is the same
68+
//the deep equal of "this" seemed to time out
69+
expect(copy).to.deep.equal(vm.form);
70+
});
71+
72+
it('focus', () => {
73+
data.form.test.onFocus = spy;
74+
createForm();
75+
trigger(vm.$el.querySelectorAll('input')[0], 'focus');
76+
expect(spy.called).to.be.true;
77+
expect(vm.form.test.$active).to.be.true;
78+
});
79+
80+
it('click', () => {
81+
data.form.test.onClick = spy;
82+
createForm();
83+
trigger(vm.$el.querySelectorAll('input')[0], 'click');
84+
expect(spy.called).to.be.true;
85+
});
86+
87+
it('change', () => {
88+
data.form.test.onChange = spy;
89+
createForm();
90+
trigger(vm.$el.querySelectorAll('input')[0], 'change');
91+
expect(spy.called).to.be.true;
92+
expect(vm.form.test.$dirty).to.be.true;
93+
});
94+
95+
it('keyup', () => {
96+
data.form.test.onKeyup = spy;
97+
createForm();
98+
trigger(vm.$el.querySelectorAll('input')[0], 'keyup');
99+
expect(spy.called).to.be.true;
100+
});
101+
102+
it('keydown', () => {
103+
data.form.test.onKeydown = spy;
104+
createForm();
105+
trigger(vm.$el.querySelectorAll('input')[0], 'keydown');
106+
expect(spy.called).to.be.true;
56107
});
57108

58109
});

0 commit comments

Comments
 (0)