Skip to content

Commit b1d9a5a

Browse files
committed
added lists
1 parent 0f3c40f commit b1d9a5a

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/fields/fieldList.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import baseField from './baseField';
2+
export default {
3+
computed: {
4+
inputType(){
5+
return this.to.inputType || 'checkbox';
6+
}
7+
},
8+
created(){
9+
// set the default value to array
10+
if ( this.inputType === 'checkbox' && ( this.model[ this.field.key ].constructor !== Array || !this.model[ this.field.key ].constructor) ) this.$set( this.model, this.field.key, [] );
11+
},
12+
mixins: [baseField],
13+
render(h){
14+
const children = [];
15+
const self = this;
16+
const isArray = this.inputType === 'checkbox';
17+
18+
// add the label if it needs it
19+
if ( this.to.label ) children.push(
20+
h('label', this.to.label )
21+
);
22+
23+
// create each option
24+
if ( 'options' in this.field ){
25+
this.field.options.forEach( option => {
26+
const optionLabel = option.hasOwnProperty('label') ? option.label : option;
27+
const optionVal = option.hasOwnProperty('value') ? option.value : option;
28+
const optionChecked = isArray ? this.model[ this.field.key ].indexOf( optionVal ) > -1 : this.model[ this.field.key ] === optionVal;
29+
30+
children.push(
31+
// wrap each option in a label
32+
h('label', {
33+
'class': this.to.labelClasses
34+
}, [
35+
// create an input
36+
h('input', {
37+
attrs: {
38+
type: this.inputType,
39+
...this.to.atts
40+
},
41+
'class': {
42+
...this.to.classes
43+
},
44+
domProps: {
45+
value: optionVal,
46+
checked: optionChecked
47+
},
48+
on: {
49+
click: this.onClick,
50+
blur: this.onBlur,
51+
focus: this.onFocus,
52+
keyup: this.onKeyup,
53+
keydown: this.onKeydown,
54+
change(event){
55+
const isChecked = event.target.checked;
56+
// we need to add/remove differently depending on the type of input we're using
57+
if ( isArray ){
58+
if ( isChecked ){
59+
// if it's a checkbox, and hence an array, push it
60+
self.model[ self.field.key ].push( event.target.value );
61+
} else {
62+
// otherwise remove it
63+
const valueIdx = self.model[ self.field.key ].indexOf( event.target.value );
64+
if ( valueIdx > -1 ) self.model[ self.field.key ].splice( valueIdx, 1 );
65+
}
66+
} else {
67+
self.model[ self.field.key ] = isChecked ? event.target.value : null;
68+
}
69+
self.$emit('change', event.target.value);
70+
if ( typeof self.onChange === 'function' ) self.onChange(event);
71+
}
72+
}
73+
}),
74+
// display the label
75+
optionLabel
76+
])
77+
);
78+
});
79+
}
80+
81+
// add the error element
82+
children.push(
83+
h('error-display', {
84+
props: {
85+
form: this.form,
86+
field: this.field.key
87+
}
88+
})
89+
);
90+
91+
// create the wrapper element
92+
return h('div', {
93+
'class': [
94+
'form-group formly-list',
95+
this.to.wrapperClasses,
96+
{
97+
'has-error has-danger': this.hasError
98+
}
99+
]
100+
}, children);
101+
}
102+
}

test/unit/specs/index.spec.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ describe('Bootstrap Field Inputs', () => {
387387
expect(inputs).to.be.length(1);
388388
});
389389
});
390-
391-
return;
392390

393391
describe("List", () => {
394392
describe('checkbox functions', ()=>{
@@ -453,6 +451,23 @@ describe('Bootstrap Field Inputs', () => {
453451
expect(labels[2].textContent).to.contain('Bar');
454452
});
455453

454+
it('boolean options', done => {
455+
data.fields[0].type = 'list';
456+
data.fields[0].options = [
457+
{ label: 'Foo', value: true },
458+
{ label: 'Bar', value: false }
459+
];
460+
data.model.test = [false];
461+
createForm();
462+
expect(vm.$el.querySelectorAll('input')[1].checked).to.be.true;
463+
data.model.test.push(true);
464+
setTimeout(()=>{
465+
expect(vm.$el.querySelectorAll('input')[0].checked).to.be.true;
466+
expect(vm.$el.querySelectorAll('input')[1].checked).to.be.true;
467+
done();
468+
});
469+
});
470+
456471
it('sets defaults', () => {
457472
data.fields[0].type = 'list';
458473
data.fields[0].options = ['one', 'two'];

0 commit comments

Comments
 (0)