Skip to content

Commit 4a866fe

Browse files
committed
adding select
1 parent d42eee9 commit 4a866fe

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/fields/fieldSelect.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import baseField from './baseField';
2+
export default {
3+
mixins: [baseField],
4+
render(h){
5+
const children = [];
6+
const self = this;
7+
8+
// add the label if it needs it
9+
if ( this.to.label ) children.push(
10+
h('label', {
11+
attrs: {
12+
'for': this.to.id
13+
}
14+
}, this.to.label )
15+
);
16+
17+
// create each option
18+
const options = [];
19+
if ( 'options' in this.field ){
20+
this.field.options.forEach( option => {
21+
const optionVal = option.hasOwnProperty('value') ? option.value : option;
22+
options.push(
23+
h('option', {
24+
domProps: {
25+
value: optionVal
26+
}
27+
}, option.label || option)
28+
);
29+
});
30+
}
31+
32+
// add the element
33+
children.push(
34+
h('select', {
35+
attrs: {
36+
id: this.to.id,
37+
...this.to.atts
38+
},
39+
'class': {
40+
'form-control': true,
41+
...this.to.classes
42+
},
43+
domProps: {
44+
value: this.model[this.field.key]
45+
},
46+
on: {
47+
click: this.onClick,
48+
blur: this.onBlur,
49+
focus: this.onFocus,
50+
keyup: this.onKeyup,
51+
keydown: this.onKeydown,
52+
change(event){
53+
self.model[ self.field.key ] = event.target.value;
54+
self.$emit('change', event.target.value);
55+
if ( typeof self.onChange === 'function' ) self.onChange(event);
56+
}
57+
}
58+
}, options)
59+
);
60+
61+
// add the error element
62+
children.push(
63+
h('error-display', {
64+
props: {
65+
form: this.form,
66+
field: this.field.key
67+
}
68+
})
69+
);
70+
71+
// create the wrapper element
72+
return h('div', {
73+
'class': [
74+
'form-group formly-select',
75+
this.to.wrapperClasses,
76+
{
77+
'has-error has-danger': this.hasError
78+
}
79+
]
80+
}, children);
81+
}
82+
}

test/unit/specs/index.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ describe('Bootstrap Field Inputs', () => {
300300
});
301301

302302
});
303-
304-
return;
305303

306304
describe('Select', () => {
307305
describe('functions', ()=>{
@@ -348,9 +346,28 @@ describe('Bootstrap Field Inputs', () => {
348346
expect(option.value).to.equal('bar');
349347
expect(option.innerHTML).to.equal('Foo');
350348
});
349+
350+
it('boolean options', done => {
351+
data.model.test = false;
352+
data.fields[0].type = 'select';
353+
data.fields[0].options = [
354+
{ label: 'Foo', value: true },
355+
{ label: 'Foo', value: false }
356+
];
357+
createForm();
358+
const select = vm.$el.querySelectorAll('select');
359+
expect(select[0].selectedIndex).to.equal(1);
360+
vm.model.test = true;
361+
setTimeout(() => {
362+
expect(select[0].selectedIndex).to.equal(0);
363+
done();
364+
});
365+
});
351366

352367
});
353368

369+
return;
370+
354371
describe('Textarea', () => {
355372
describe('functions', ()=>{
356373
describeFunctions('textarea', 'textarea');

0 commit comments

Comments
 (0)