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.

image

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

Most upvoted comments

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.

import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';

const App = () => (
  <SlSelect label="Select a Few" value={["option-1", "option-2", "option-3"]} multiple clearable>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
    {/* Additional options here */}
  </SlSelect>
);

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. Adding type="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 valueHasChanged private 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:

<sl-select value="doesnt-exist"></sl-select>

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 value of 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 updating value, 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