I've created a custom select-component with LitElement:
import { LitElement, html } from 'lit-element';
class CustomSelect extends LitElement {
static get properties() {
return {
options: { type: Array },
selected: { type: String },
onChange: { type: Function }
};
}
constructor() {
super();
this.options = [];
}
render() {
return html`
<select @change="${this.onChange}">
${this.options.map(option => html`
<option value="${option.value}" ?selected=${this.selected === option.value}>${option.text}</option>
`)}
</select>
`;
}
createRenderRoot() {
return this;
}
}
customElements.define('custom-select', CustomSelect);
I pass in options
, selected
and onChange
as properties when I create the element. On the first render, everything works fine. All options are rendered and the selected value is reflected in the select. However, if I change selected
it doesn't seem to update the selected option. If I inspect the element with dev-tools, the selected
attribute is set correctly, but if I start querying the element for its value, it returns the wrong value.
One thing I tried is to add an id
attribute to the element via dev-tools after the select has been rendered. If I then change the selected
property on CustomSelect
, the id
attribute persists in the DOM, which says to me that the select is not re-rendered, which is what causing the issue, and why it's working on the first render.
I've tried setting the value
and selectedIndex
properties on the select-element, but it doesn't seem to affect anything in a meaningful way.
I've logged everywhere (beginning in render() and in the options-map) and all input values are correct.