Skip to content

Commit b85e883

Browse files
committed
added input field
1 parent 2008941 commit b85e883

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

src/fields/baseField.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default
2+
{
3+
props: [
4+
'form',
5+
'key'
6+
]
7+
};

src/fields/fieldInput.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div class="form-group">
3+
<label v-if="form[key].label">{{form[key].label}}</label>
4+
<input class="form-control" :type="form[key].inputType" v-model="form[key].value" :placeholder="form[key].placeholder">
5+
</div>
6+
</template>
7+
8+
<script>
9+
import baseField from './baseField';
10+
export default {
11+
mixins: [baseField]
12+
}
13+
</script>

src/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import VueFormly from 'vue-formly';
2+
3+
let Fields = require.context("./fields/", false, /^\.\/field([\w-_]+)\.vue$/);
4+
let FormlyBootstrap = {
5+
install(Vue, options){
6+
Fields.keys().forEach((key) => {
7+
//remove all the .vue crap
8+
let component = key
9+
.replace(/^\.\//, "")
10+
.replace(/\.vue/, "")
11+
.replace(/^field/, "");
12+
13+
//convert the first letter to lc
14+
component = component.charAt(0).toLowerCase() + component.slice(1);
15+
16+
Vue.$formly.addType(component, Fields(key));
17+
});
18+
}
19+
};
20+
21+
//auto install
22+
if (typeof window !== 'undefined' && window.Vue) {
23+
window.Vue.use(FormlyBootstrap);
24+
}
25+
26+
export default FormlyBootstrap;

test/unit/specs/index.spec.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,74 @@
11
import {expect} from 'chai';
2+
import Vue from 'vue';
3+
import VueFormly from 'vue-formly';
4+
import FormlyBootstrap from 'src/index';
5+
Vue.use(VueFormly);
6+
Vue.use(FormlyBootstrap);
27

8+
let el, vm;
39

4-
describe('module', () => {
10+
function createForm(){
11+
el = document.createElement('div');
12+
el.innerHTML = '<formly-form :form="form"></formly-form>';
13+
vm = new Vue({
14+
el: el,
15+
data: data
16+
});
17+
18+
return [el, vm];
19+
}
20+
21+
let data;
22+
23+
24+
describe('components', () => {
25+
26+
beforeEach(() => {
27+
data = {
28+
form: {
29+
test: {
30+
label: 'Test'
31+
}
32+
}
33+
};
34+
});
35+
36+
describe('input', () => {
37+
38+
it('basic functions', (done) => {
39+
data.form.test.type = 'input';
40+
data.form.test.inputType = 'text';
41+
data.form.test.placeholder = 'holding';
42+
createForm(data);
43+
44+
let inputs = vm.$el.querySelectorAll('input');
45+
let input = inputs[0];
46+
47+
expect(inputs).to.be.length(1);
48+
expect(input.type).to.equal('text');
49+
expect(vm.$el.querySelectorAll('label')).to.be.length(1);
50+
expect(vm.$el.querySelectorAll('label')[0].textContent).to.equal(data.form.test.label);
51+
expect(vm.$el.querySelectorAll('input.form-control')).to.be.length(1);
52+
expect(input.placeholder).to.equal('holding');
53+
54+
vm.form.test.value = 'testing';
55+
56+
setTimeout(() => {
57+
expect(vm.$el.querySelectorAll('input')[0].value).to.equal('testing');
58+
done();
59+
}, 0);
60+
61+
});
62+
63+
it('conditional elements', () => {
64+
data.form.test.type = 'input';
65+
data.form.test.inputType = 'text';
66+
data.form.test.label = '';
67+
delete data.form.test.placeholder;
68+
createForm(data);
569

6-
it('', () => {
70+
expect(vm.$el.querySelectorAll('label')).to.be.length(0);
71+
});
772
});
873

974
});

0 commit comments

Comments
 (0)