New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sl-select
does not honor its value
when included in the response HTML
#1570
Comments
Thank for the report. I can confirm that the value is not set when Shoelace is bundled and loaded as a script in the @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. 🤔 |
Oh that's good to know. I should be able to split the Shoelace bits into a separate deferred bundle.
While looking into this I also tested |
@claviska definitely seems to be a life cycle issue. We could either emit a EDIT: As for tests, this is a little more difficult because it requires a separate file due to how WTR loads every test file in the same window. But the test itself would be relatively straightforward. EDIT 2: This can actually be reproduced in a regular file if you append an // Passes
it('Should set option to selected when options are lazily loaded', async () => {
const select = await fixture<SlSelect>(html`
<sl-select value="option-1">
<sl-option value="option-1">Option 1</sl-option>
<sl-option value="option-2">Option 2</sl-option>
<sl-option value="option-3">Option 3</sl-option>
</sl-select>
`)
await select.updateComplete
expect((select.querySelector("[value='option-1']") as SlOption).selected).to.be.true
})
// Fails
it('Should set option to selected when options are lazily loaded', async () => {
const select = await fixture<SlSelect>(html`
<sl-select value="option-1">
</sl-select>
`)
select.innerHTML = `
<sl-option value="option-1">Option 1</sl-option>
<sl-option value="option-2">Option 2</sl-option>
<sl-option value="option-3">Option 3</sl-option>
`
await select.updateComplete
expect((select.querySelector("[value='option-1']") as SlOption).selected).to.be.true
}) |
## 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 and into code requires running `lit-localize build`. Rather than making people do that manually, I wrote a Parcel resolver that runs the command behind the scenes. Overengineered? Maybe! But I learned a bunch about Parcel! Note about switching `language` dynamically. Apart from the problem I noted in the comment on the `language` attribute (text that came back from the API will not change language until the next API fetch), there's another problem, with the Shoelace select elements: the text shown in them won't change until you make a new selection in the element. I _think_ it may be related to [this](shoelace-style/shoelace#1570); in any case, once all the immediate i18n work is wrapped up I may try to isolate the issue and file an issue with them if it's not the same bug. With this bug, dynamically setting the attribute _on page load_ will leave a couple of untranslated strings, and I think that's a use case we do want to support. ## Test Plan Add a `language="es"` attribute to the main element in `rhode-island.html`, and make sure the UI shows up in Spanish. Query for incentives; make sure the program names of federal incentives show up in Spanish. (Those are the only thing localized on the backend right now.)
## 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 and into code requires running `lit-localize build`. Rather than making people do that manually, I wrote a Parcel resolver that runs the command behind the scenes. Overengineered? Maybe! But I learned a bunch about Parcel! Note about switching `language` dynamically. Apart from the problem I noted in the comment on the `language` attribute (text that came back from the API will not change language until the next API fetch), there's another problem, with the Shoelace select elements: the text shown in them won't change until you make a new selection in the element. I _think_ it may be related to [this](shoelace-style/shoelace#1570); in any case, once all the immediate i18n work is wrapped up I may try to isolate the issue and file an issue with them if it's not the same bug. With this bug, dynamically setting the attribute _on page load_ will leave a couple of untranslated strings, and I think that's a use case we do want to support. ## Test Plan Add a `language="es"` attribute to the main element in `rhode-island.html`, and make sure the UI shows up in Spanish. Query for incentives; make sure the program names of federal incentives show up in Spanish. (Those are the only thing localized on the backend right now.)
## 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 the strings out of the XLIFF file and into code requires running `lit-localize build`. Rather than making people do that manually, I wrote a Parcel resolver that runs the command behind the scenes. Overengineered? Maybe! But I learned a bunch about Parcel! Note about switching `lang` dynamically. Apart from the problem I noted in the comment on the `lang` attribute (text that came back from the API will not change language until the next API fetch), there's another problem, with the Shoelace select elements: the text shown in them won't change until you make a new selection in the element. I _think_ it may be related to [this](shoelace-style/shoelace#1570); in any case, once all the immediate i18n work is wrapped up I may try to isolate the issue and file an issue with them if it's not the same bug. With this bug, dynamically setting the attribute _on page load_ will leave a couple of untranslated strings, and I think that's a use case we do want to support. ## Test Plan Add a `lang="es"` attribute to the main element in `rhode-island.html`, and make sure the UI shows up in Spanish. Query for incentives; make sure the program names of federal incentives show up in Spanish. (Those are the only thing localized on the backend right now.)
Any update on this? Stuck with the same issue. |
No update yet. Contributions are welcome, otherwise we'll try to get to it as soon as we can! |
What's interesting is that the native https://codepen.io/paramagicdev/pen/JjxmzxW if when it initially connects, there are no |
So, as I've had time to sit with this, I think the best course of action would be to not change the @claviska to me this feels like it has the least amount of "surprise" to it |
Would that impact form submissions (i.e. I submit the |
@davidcornu in what way? So today if you do something like this: <sl-select value="doesnt-exist"></sl-select> its "value" will become 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. |
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 |
Nothing great I can think of beyond manually updating |
Fixed in #1785 |
Reopening, as the docs update doesn't really address lazy loaded options. |
Describe the bug
When a
sl-select
element is included in a page's HTML (as opposed to added afterwards via JS) with avalue
attribute and several nestedsl-option
s, 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 thesl-option
is defined.You'll notice the first
sl-select
does not showOne
as the initial value, whereas the second one does.Browser / OS
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:shoelace/src/components/select/select.component.ts
Line 554 in 2ed5a4f
This leads it to clear
value
shoelace/src/components/select/select.component.ts
Line 567 in 2ed5a4f
which means that as its
<sl-option>
s are connected it can't establish which option is selected.The text was updated successfully, but these errors were encountered: