I'm trying to add options to a previously created Vnode (select element). The element is returned before the options are available, since they come from an async function.
Although I try to set the children upon the callback, that does not change the rendered element. And the options are not displayed in the select element.
My code bellow:
const getSelect = function (field: FieldDef, key: string) {
const elem = m('div', { class: 'field' }, [
m('label', { class: 'label' }, field.label ? field.label : field.name),
m(
'div',
{
class: field.controlClass
? 'select ' + field.controlClass
: 'select is-small',
onchange: (val: number | string) => {
console.log(val);
},
},
[
m(
'select',
{
class: field.class ? field.class : '',
name: field.name ? field.name : key,
id: key,
value: field.value,
},
[],
),
],
),
]);
if (field.options) {
void field.options().then(function (items) {
elem.children = [];
elem.children.push(
items.map((i) => {
return m('option', { value: i.value }, i.text);
}),
);
console.log('the options are:');
console.log(elem.children);
});
}
return elem;
};