Closed
Description
The state of a <select>
element seems to be handled incorrectly when it's replaced by another <select>
element under certain conditions. I reproduced it in Chrome and Firefox so it's probably an issue with lighterhtml. Here is the test case code + codepen.
const { render, html } = lighterhtml
const Option = ({label, value, selected}) => html`
<option value=${value} selected="${selected}" >
${label}
</option>
`
const Select = (options, onChange) => html`
<select onchange=${(e) => onChange(e.target.value)}>
${options.map(Option)}
</select>
`
let showNumbers = true
let activeNumber = '2'
let activeLetter = 'a'
const numbers = ['1', '2', '3', '4']
const letters = ['a', 'b']
const toggle = () => {
showNumbers = !showNumbers
update()
}
const setNumber = (num) => {
activeNumber = num
update()
}
const setLetter = (letter) => {
activeLetter = letter
update()
}
const getOptions = (arr, active) => arr.map(item => ({
label: item,
value: item,
selected: item === active,
}))
const SelectNumber = () => {
const options = getOptions(numbers, activeNumber)
return Select(options, setNumber)
}
const SelectLetter = () => {
const options = getOptions(letters, activeLetter)
// return Select(options, setLetter)
return html`<span>${Select(options, setLetter)}</span>`
}
const App = () => html`
<button onclick=${toggle}>
toggle
</button>
${(showNumbers) ? SelectNumber() : SelectLetter()}
`
const update = () => render(document.body, App)
update()
Steps to reproduce: click on "toggle" button three times.
Expected result: selected options show 2 -> a -> 2 -> a . I see this result if I uncomment the first return statement in SelectLetter.
Observed results: selected options show 2 -> a -> 3 -> b .
It took me a while to zero in on the issue 😌
Metadata
Metadata
Assignees
Labels
No labels