Skip to content

Commit ea621a5

Browse files
committed
added async components
1 parent c945e4c commit ea621a5

File tree

7 files changed

+94
-43
lines changed

7 files changed

+94
-43
lines changed

src/components/FormlyField.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
</template>
44

55
<script>
6+
import Vue from 'vue';
67
export default {
7-
props: ['field', 'model']
8+
props: ['field', 'model'],
9+
components: Vue.$formlyFields
810
}
911
</script>

src/components/FormlyForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</template>
66

77
<script>
8-
export default{
8+
export default {
99
props: ['fields', 'model']
1010
}
1111
</script>

src/components/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import FormlyForm from './FormlyForm.vue';
2-
import FormlyField from './FormlyField.vue';
32

43
export default function(Vue){
5-
Vue.component('FormlyForm', FormlyForm);
6-
Vue.component('FormlyField', FormlyField);
4+
Vue.component('formly-form', FormlyForm);
5+
Vue.component('formly-field', (resolve) => {
6+
/**
7+
* FormlyField must be loaded asyncronously so that any fields added in
8+
* via Formly.addType are available
9+
*/
10+
require(['./FormlyField.vue'], resolve);
11+
});
712
}

src/index.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
import Components from './components';
2-
3-
4-
let Formly = {};
5-
6-
Formly.install = function(Vue, options){
1+
import Components from './components/index';
2+
3+
4+
let Formly = {
5+
/**
6+
* Allow additional templates to be added
7+
* @param {String} id
8+
* @param {Object} options
9+
*/
10+
addType(id, options){
11+
Vue.$formlyFields[id] = options;
12+
},
13+
14+
install(Vue, options){
715

8-
if ( Formly.installed ){
9-
return;
10-
}
16+
if ( Formly.installed ){
17+
return;
18+
}
1119

12-
Components(Vue);
20+
Components(Vue);
1321

14-
Vue.$formly = {
22+
Vue.$formlyFields = {};
1523

16-
fields: {},
17-
18-
/**
19-
* Allow additional templates to be added
20-
* @param {String} id
21-
* @param {Object} options
22-
*/
23-
addType(id, options){
24-
//our base component
25-
let base = {
26-
props: ['model', 'schema']
27-
};
28-
29-
//merge
30-
Object.assign(options, base);
31-
Vue.$formly.fields[id] = Vue.extend(options);
32-
}
33-
};
34-
24+
}
3525
};
3626

3727
//auto install

test/unit/specs/FormlyField.spec.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import chai from 'chai';
22
const expect = chai.expect;
3-
import sinonChai from 'sinon-chai';
4-
import sinon from 'sinon';
53
import Vue from 'vue';
64
import FormlyField from 'src/components/FormlyField.vue';
7-
chai.use(sinonChai);
85

96
let el, vm;
107

test/unit/specs/FormlyForm.spec.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import sinonChai from 'sinon-chai';
44
import sinon from 'sinon';
55
import Vue from 'vue';
66
import FormlyForm from 'src/components/FormlyForm.vue';
7+
Vue.component('formly-form', FormlyForm);
78
chai.use(sinonChai);
89

910
//mock our formly-field component
@@ -12,17 +13,25 @@ let FormlyField = Vue.extend({
1213
props: ['field', 'model']
1314
});
1415

16+
//our formly specific field
17+
let FormlyRestrictedField = Vue.extend({
18+
template: '<div class="restricted-field"></div>',
19+
props: ['field', 'model']
20+
});
21+
22+
Vue.$formlyFields = {
23+
'restricted': FormlyRestrictedField
24+
};
25+
26+
1527
let el, vm;
1628

1729
function createForm(template, data){
1830
el = document.createElement('div');
1931
el.innerHTML = template;
2032
vm = new Vue({
2133
el: el,
22-
data: data,
23-
components: {
24-
'formly-form': FormlyForm
25-
}
34+
data: data
2635
});
2736

2837
return [el, vm];
@@ -65,5 +74,36 @@ describe('FormlyForm', () => {
6574
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.schema[1]);
6675

6776
});
77+
78+
it('should restrict some components to formly itself', () => {
79+
sinon.spy(console, 'error');
80+
81+
//re-create the formly field
82+
Vue.component('formly-field', (resolve) =>{
83+
resolve({
84+
props: ['field', 'model'],
85+
template: '<component :is="field.type"></component>',
86+
components: Vue.$formlyFields
87+
});
88+
});
89+
90+
let data = {
91+
schema: [
92+
{
93+
key: 'fname',
94+
type: 'restricted'
95+
}
96+
],
97+
model: {
98+
fname: ''
99+
}
100+
};
101+
createForm('<formly-form :fields="schema" :model="model"></formly-form><restricted></restricted>', data);
102+
103+
expect(console.error).to.be.called.once;
104+
expect(vm.$el.querySelectorAll('.restricted-field')).to.be.length(1);
105+
expect(vm.$el.querySelectorAll('fieldset .restricted-field')).to.be.length(1);
106+
107+
});
68108

69109
});

test/unit/specs/index.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ describe('module', () => {
77

88
it('module props', () => {
99
expect(VueFormly).to.exist;
10-
expect(VueFormly.install).to.be.a('function');
10+
expect(VueFormly.install).to.be.a('function');
11+
expect(VueFormly.addType).to.be.a('function');
12+
});
13+
14+
it('should add fields to Vue', () => {
15+
16+
//mock vue
17+
window.Vue = {
18+
component(){}
19+
};
20+
VueFormly.install(Vue);
21+
expect(Vue.$formlyFields).to.be.a('object');
22+
23+
let newField = {
24+
foo: 'bar'
25+
};
26+
VueFormly.addType('test', newField);
27+
expect(Vue.$formlyFields.test).to.deep.equal(newField);
1128
});
1229

1330
});

0 commit comments

Comments
 (0)