Skip to content

Commit 4dd5491

Browse files
committed
better schema
1 parent 53dfd9a commit 4dd5491

File tree

7 files changed

+49
-38
lines changed

7 files changed

+49
-38
lines changed

src/components/FormlyField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<component :is="form.fields[key].type" :form.sync="form" :key="key"></component>
2+
<component :is="form[key].type" :form.sync="form" :key="key"></component>
33
</template>
44

55
<script>

src/components/FormlyForm.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<template>
22
<fieldset>
3-
<formly-field v-for="field in form.fields" :form.sync="form" :key="$key" ></formly-field>
3+
<formly-field v-for="field in form | formlyFields" :form.sync="form" :key="field" ></formly-field>
44
</fieldset>
55
</template>
66

77
<script>
88
export default {
99
props: ['form'],
1010
created(){
11+
//make sure that the 'value' is always set
12+
Object.keys(this.form).forEach((key) => {
13+
if ( typeof this.form[key].value == 'undefined' ) this.$set('form.'+key+'.value', '');
14+
});
15+
1116
//set our validation options
1217
this.$set('form.$errors', {});
1318
this.$set('form.$valid', false);
14-
15-
//make sure that the 'value' is always set
16-
Object.keys(this.form.fields).forEach((key) => {
17-
if ( typeof this.form.fields[key].value == 'undefined' ) this.$set('form.fields.'+key+'.value', '');
18-
});
1919
}
2020
}
2121
</script>

src/filters/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function(Vue){
2+
//filter valid formly fields.
3+
//this shouldn't pass any $ fields. eg $errors or $valid
4+
Vue.filter('formlyFields', (fields) => {
5+
let re = /^\$/;
6+
let valid = Object.keys(fields).filter( (key) => {
7+
return !re.test(key);
8+
});
9+
return valid;
10+
});
11+
}

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Components from './components/index';
2+
import Filters from './filters/index';
23
import Util,{getTypes, addType} from './util';
34

45

56
let Formly = {
6-
7+
getTypes,
8+
addType,
79
install(Vue, options){
810

911
if ( Formly.installed ){
@@ -12,7 +14,7 @@ let Formly = {
1214

1315
//install our components
1416
Components(Vue);
15-
17+
Filters(Vue);
1618
}
1719
};
1820

test/unit/specs/FormlyField.spec.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ describe('FormlyField', () => {
2525

2626
Vue.component('test', {
2727
props: ['form', 'key'],
28-
template: '<div id="testComponent">{{form.fields[key].type}}</div>'
28+
template: '<div id="testComponent">{{form[key].type}}</div>'
2929
});
3030

3131
let data = {
3232
form:{
33-
fields: {
34-
test: {
35-
type: 'test'
36-
}
33+
test: {
34+
type: 'test'
3735
}
3836
}
3937
};
@@ -42,24 +40,22 @@ describe('FormlyField', () => {
4240

4341
let innerElem = vm.$el.querySelector('#testComponent');
4442

45-
expect(innerElem.textContent).to.contain(data.form.fields.test.type);
43+
expect(innerElem.textContent).to.contain(data.form.test.type);
4644

4745
});
4846

4947
it('should mimic the model of the parent', (done) => {
5048

5149
Vue.component('test', {
5250
props: ['form', 'key'],
53-
template: '<input type="text" id="testInput" v-model="form.fields[key].value">'
51+
template: '<input type="text" id="testInput" v-model="form[key].value">'
5452
});
5553

5654
let data = {
5755
form: {
58-
fields: {
59-
search: {
60-
type: 'test',
61-
value: 'foo'
62-
}
56+
search: {
57+
type: 'test',
58+
value: 'foo'
6359
}
6460
}
6561
};
@@ -71,7 +67,7 @@ describe('FormlyField', () => {
7167
expect(input.value).to.contain('foo');
7268

7369
//change the value and expect a change
74-
vm.form.fields.search.value = 'bar';
70+
vm.form.search.value = 'bar';
7571

7672
setTimeout(() => {
7773
expect(input.value).to.equal('bar');

test/unit/specs/FormlyForm.spec.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import sinonChai from 'sinon-chai';
44
import sinon from 'sinon';
55
import Vue from 'vue';
66
import FormlyForm from 'src/components/FormlyForm.vue';
7+
import Filters from 'src/filters/index';
78
Vue.component('formly-form', FormlyForm);
89
chai.use(sinonChai);
10+
//install our filters
11+
Filters(Vue);
912

1013
//mock our formly-field component
1114
let FormlyField = Vue.extend({
12-
template: '<div class="formly-field"><pre id="{{key}}_field">{{form.fields[key] | json}}</pre><pre id="{{key}}_model">{{form.fields[key].value | json}}</pre></div>',
15+
template: '<div class="formly-field"><pre id="{{key}}_field">{{form[key] | json}}</pre><pre id="{{key}}_model">{{form[key].value | json}}</pre></div>',
1316
props: ['form', 'key']
1417
});
1518

@@ -43,14 +46,12 @@ describe('FormlyForm', () => {
4346

4447
let data = {
4548
form: {
46-
fields: {
47-
fname: {
48-
type: 'input'
49-
},
50-
lname: {
51-
type: 'input',
52-
value: 'smith'
53-
}
49+
fname: {
50+
type: 'input'
51+
},
52+
lname: {
53+
type: 'input',
54+
value: 'smith'
5455
}
5556
}
5657
};
@@ -63,7 +64,7 @@ describe('FormlyForm', () => {
6364

6465
//check their data
6566
expect(vm.$el.querySelector('#lname_model').textContent).to.contain('smith');
66-
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.form.fields.lname);
67+
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.form.lname);
6768
expect(JSON.parse(vm.$el.querySelector('#fname_field').textContent)).to.deep.equal({type: 'input', value: ''});
6869
expect(data.form.$errors).to.deep.equal({});
6970
expect(data.form.$valid).to.be.false;
@@ -77,7 +78,7 @@ describe('FormlyForm', () => {
7778
Vue.component('formly-field', (resolve) =>{
7879
resolve({
7980
props: ['form', 'key'],
80-
template: '<component :is="form.fields[key].type"></component>',
81+
template: '<component :is="form[key].type"></component>',
8182
components: Vue.$formlyFields
8283
});
8384
});
@@ -88,10 +89,8 @@ describe('FormlyForm', () => {
8889

8990
let data = {
9091
form: {
91-
fields: {
92-
fname: {
93-
type: 'restricted'
94-
}
92+
fname: {
93+
type: 'restricted'
9594
}
9695
}
9796
};

test/unit/specs/index.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe('module', () => {
99
it('module props', () => {
1010
expect(VueFormly).to.exist;
1111
expect(VueFormly.install).to.be.a('function');
12+
expect(VueFormly.addType).to.be.a('function');
13+
expect(VueFormly.getTypes).to.be.a('function');
1214
expect(addType).to.be.a('function');
1315
expect(getTypes).to.be.a('function');
1416
expect(Util.formlyFields).to.be.a('object');
@@ -18,7 +20,8 @@ describe('module', () => {
1820

1921
//mock vue
2022
window.Vue = {
21-
component(){}
23+
component(){},
24+
filter(){}
2225
};
2326
VueFormly.install(Vue);
2427

0 commit comments

Comments
 (0)