shoelace: `sl-select` does not honor its `value` when included in the response HTML
Describe the bug
When a sl-select element is included in a page’s HTML (as opposed to added afterwards via JS) with a value attribute and several nested sl-options, it does not render the initial value.
To Reproduce
I’ve created a minimal reproduction over at https://github.com/davidcornu/shoelace-bug, which renders the exact same sl-select twice: once in the page’s HTML and once by appending it to the DOM once the sl-option is defined.
You’ll notice the first sl-select does not show One as the initial value, whereas the second one does.
Browser / OS
- OS: macOS 13.5.2 (22G91)
- Browsers:
- Firefox 117.0.1
- Safari 16.6 (18615.3.12.11.2)
Additional information
After some time digging around in the debugger, I believe the source of the issue is that when selectionChanged first runs, this.getAllOptions returns [] as the various <sl-option> elements have not been initialized yet: https://github.com/shoelace-style/shoelace/blob/2ed5a4ff97ee891b546a46f858e20c8c4fb8ac76/src/components/select/select.component.ts#L554
This leads it to clear value https://github.com/shoelace-style/shoelace/blob/2ed5a4ff97ee891b546a46f858e20c8c4fb8ac76/src/components/select/select.component.ts#L567
which means that as its <sl-option>s are connected it can’t establish which option is selected.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (4 by maintainers)
Commits related to this issue
- Use Spanish translations in the UI ## Description Finally we're here: you can just set `language="es"` on the calculator element and it'll be in Spanish. Getting the strings out of the XLIFF file a... — committed to rewiringamerica/embed.rewiringamerica.org by oyamauchi 3 years ago
- Use Spanish translations in the UI ## Description Finally we're here: you can just set `language="es"` on the calculator element and it'll be in Spanish. Getting the strings out of the XLIFF file a... — committed to rewiringamerica/embed.rewiringamerica.org by oyamauchi 3 years ago
- Use Spanish translations in the UI (#66) ## Description Finally we're here: you can just set `lang="es"` on the calculator element (or any ancestor element!) and it'll be in Spanish. Getting t... — committed to rewiringamerica/embed.rewiringamerica.org by oyamauchi 3 years ago
Hi there! 👋
I’ve updated the documentation for multiple selection in Shoelace. Specifically, I’ve replaced the string-based ‘value’ prop with an array in the example code to accurately reflect the recommended usage for multiple selection.
I’ve also created a pull request with these changes. You can find it here
Thank for the report. I can confirm that the value is not set when Shoelace is bundled and loaded as a script in the
<head>of the document. Addingtype="module"defers loading and works around the bug.@KonnorRogers Seems like a lifecycle issue. Since we almost always load Shoelace as a module, we’ve never run into it. It would be worth checking other components for similar bugs. I wonder if we can create a test for this somehow. 🤔
🤔 this is probably because it never trips the
valueHasChangedprivate property. Something about the lifecycle if I had to guess.Reproduction:
https://codepen.io/paramagicdev/pen/LYwmLYO?editors=1000
Thanks for the report.
Reopening, as the docs update doesn’t really address lazy loaded options.
@davidcornu in what way?
So today if you do something like this:
its “value” will become
""if that’s what youre asking.I think if we track the “initial value attribute”, and only update the value when the lazy loaded option comes in, then it may be able to work. There’s definitely some kinks to work out.
So, as I’ve had time to sit with this, I think the best course of action would be to not change the
valueof the select until the user has interacted with the select. Basically, wait until a user has manually changed the value of the<sl-select>until we start updatingvalue, to allow for lazy loaded options, but also not change the value on a user while they’re using the control.@claviska to me this feels like it has the least amount of “surprise” to it