Skip to content

Commit 53dfd9a

Browse files
committed
added the new schema type
1 parent efba427 commit 53dfd9a

File tree

4 files changed

+61
-46
lines changed

4 files changed

+61
-46
lines changed

src/components/FormlyField.vue

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

55
<script>
66
const Vue = require('vue');
77
import Util, {getTypes} from '../util';
88
export default {
9-
props: ['field', 'model'],
9+
props: ['form', 'key'],
1010
components: getTypes()
1111
}
1212
</script>

src/components/FormlyForm.vue

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

77
<script>
88
export default {
9-
props: ['fields', 'model']
9+
props: ['form'],
10+
created(){
11+
//set our validation options
12+
this.$set('form.$errors', {});
13+
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+
});
19+
}
1020
}
1121
</script>

test/unit/specs/FormlyField.spec.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,54 @@ describe('FormlyField', () => {
2424
it('should take on the type of another component', () => {
2525

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

3131
let data = {
32-
schema:{
33-
type: 'test'
32+
form:{
33+
fields: {
34+
test: {
35+
type: 'test'
36+
}
37+
}
3438
}
3539
};
3640

37-
createForm('<formly-field :field="schema"></formly-field>', data);
41+
createForm('<formly-field :form="form" key="test"></formly-field>', data);
3842

3943
let innerElem = vm.$el.querySelector('#testComponent');
4044

41-
expect(innerElem.textContent).to.contain(data.schema.type);
45+
expect(innerElem.textContent).to.contain(data.form.fields.test.type);
4246

4347
});
4448

4549
it('should mimic the model of the parent', (done) => {
4650

4751
Vue.component('test', {
48-
props: ['model'],
49-
template: '<input type="text" id="testInput" v-model="model">'
52+
props: ['form', 'key'],
53+
template: '<input type="text" id="testInput" v-model="form.fields[key].value">'
5054
});
5155

5256
let data = {
53-
schema: {
54-
type: 'test'
55-
},
56-
testModel: 'foo'
57+
form: {
58+
fields: {
59+
search: {
60+
type: 'test',
61+
value: 'foo'
62+
}
63+
}
64+
}
5765
};
5866

59-
createForm('<formly-field :field="schema" :model.sync="testModel"></formly-field>', data);
67+
createForm('<formly-field :form.sync="form" key="search"></formly-field>', data);
6068

6169
let input = vm.$el.querySelector('#testInput');
6270

6371
expect(input.value).to.contain('foo');
6472

6573
//change the value and expect a change
66-
vm.testModel = 'bar';
74+
vm.form.fields.search.value = 'bar';
6775

6876
setTimeout(() => {
6977
expect(input.value).to.equal('bar');

test/unit/specs/FormlyForm.spec.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ chai.use(sinonChai);
99

1010
//mock our formly-field component
1111
let FormlyField = Vue.extend({
12-
template: '<div class="formly-field"><pre id="{{field.key}}_field">{{field | json}}</pre><pre id="{{field.key}}_model">{{model | json}}</pre></div>',
13-
props: ['field', 'model']
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>',
13+
props: ['form', 'key']
1414
});
1515

1616
//our formly specific field
1717
let FormlyRestrictedField = Vue.extend({
18-
template: '<div class="restricted-field"></div>',
19-
props: ['field', 'model']
18+
template: '<div class="restricted-field"></div>'
2019
});
2120

2221

@@ -43,31 +42,31 @@ describe('FormlyForm', () => {
4342
it('should create a subset of components with the right data', () => {
4443

4544
let data = {
46-
schema: [
47-
{
48-
key: 'fname',
49-
type: 'input'
50-
},
51-
{
52-
key: 'lname',
53-
type: 'input'
45+
form: {
46+
fields: {
47+
fname: {
48+
type: 'input'
49+
},
50+
lname: {
51+
type: 'input',
52+
value: 'smith'
53+
}
5454
}
55-
],
56-
model: {
57-
fname: '',
58-
lname: 'smith'
5955
}
6056
};
6157

62-
createForm('<formly-form :fields="schema" :model="model"></formly-form>', data);
58+
createForm('<formly-form :form="form"></formly-form>', data);
6359

6460
//check the elements have been created
6561
expect(vm.$el.querySelectorAll('fieldset')).to.be.length(1);
6662
expect(vm.$el.querySelectorAll('.formly-field')).to.be.length(2);
6763

6864
//check their data
6965
expect(vm.$el.querySelector('#lname_model').textContent).to.contain('smith');
70-
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.schema[1]);
66+
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.form.fields.lname);
67+
expect(JSON.parse(vm.$el.querySelector('#fname_field').textContent)).to.deep.equal({type: 'input', value: ''});
68+
expect(data.form.$errors).to.deep.equal({});
69+
expect(data.form.$valid).to.be.false;
7170

7271
});
7372

@@ -77,8 +76,8 @@ describe('FormlyForm', () => {
7776
//re-create the formly field
7877
Vue.component('formly-field', (resolve) =>{
7978
resolve({
80-
props: ['field', 'model'],
81-
template: '<component :is="field.type"></component>',
79+
props: ['form', 'key'],
80+
template: '<component :is="form.fields[key].type"></component>',
8281
components: Vue.$formlyFields
8382
});
8483
});
@@ -88,17 +87,15 @@ describe('FormlyForm', () => {
8887
};
8988

9089
let data = {
91-
schema: [
92-
{
93-
key: 'fname',
94-
type: 'restricted'
90+
form: {
91+
fields: {
92+
fname: {
93+
type: 'restricted'
94+
}
9595
}
96-
],
97-
model: {
98-
fname: ''
9996
}
10097
};
101-
createForm('<formly-form :fields="schema" :model="model"></formly-form><restricted></restricted>', data);
98+
createForm('<formly-form :form="form"></formly-form><restricted></restricted>', data);
10299

103100
expect(console.error).to.be.called.once;
104101
expect(vm.$el.querySelectorAll('.restricted-field')).to.be.length(1);

0 commit comments

Comments
 (0)