I have one Inputs.select
depending on the results of another in an Observable Framework markdown page and I can’t quite get it to always update. It seems to only do this half the time.
I thought maybe I needed to set up an onchange
handler to update the answerInput
dropdown explicitly, but that doesn’t seem to fire.
If I use something like const questionNr = Generators.input(questionNrInput)
instead of await, I’ll then get a ‘RuntimeError: element.addEventListener is not a function’.
const surveyQuestions = [
{
text: 'Question 1',
answers: [{ text: 'Q1 Answer 1' }, { text: 'Q1 Answer 2' }, { text: 'Q1 Answer 3' }],
},
{
text: 'Question 2',
answers: [{ text: 'Q2 Answer 1' }, { text: 'Q2 Answer 2' }, { text: 'Q2 Answer 3' }],
},
{
text: 'Question 3',
answers: [{ text: 'Q3 Answer 1' }, { text: 'Q3 Answer 2' }, { text: 'Q3 Answer 3' }],
},
];
const questionNrInput = view(
Inputs.select(new Map(surveyQuestions.map((q, i) => [`Q${i + 1}: ${q.text}`, i + 1])), {
label: 'Question',
value: 1,
})
);
questionNrInput.onchange = () => {
console.log('onchange');
// answerInput.value = ...
// answerInput.dispatchEvent(new CustomEvent('input'));
};
const questionNr = await questionNrInput;
// const questionNr = Generators.input(questionNrInput);
const answerInput = view(
Inputs.select(
surveyQuestions[questionNr - 1].answers.map((a) => a.text),
{
label: 'Answer',
value: surveyQuestions[questionNr - 1].answers[0].text,
}
)
);
const answer = await answerInput;
display(`questionNr: ${questionNr}`);
display(`answer: ${answer}`);