-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
fix(multi-select): avoid cyclic dependency for Svelte 5 compatibility #2034
fix(multi-select): avoid cyclic dependency for Svelte 5 compatibility #2034
Conversation
Refactor the MultiSelect component to remove circular dependencies which would cause infinite loops in Svelte 5 (carbon-design-system#1986).
src/MultiSelect/MultiSelect.svelte
Outdated
if (!open) { | ||
if (!initialSorted || selectionFeedback !== "fixed") { | ||
sortedItems = sort(); | ||
$: if (!open) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside afterUpdate
, this block would cause an infinite loop.
src/MultiSelect/MultiSelect.svelte
Outdated
sortedItems = sort(); | ||
$: if (!open) { | ||
if (!initialSorted || selectionFeedback !== "fixed") { | ||
tick().then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tick
ensures that the variables are instantiated when the sorting happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to get this going. You're spot on in identifying the root issue of the infinite update loop in Svelte 5. There's a cyclical items <> sortedItems
dependency in afterUpdate
.
However, one thing I noticed is that this current change does not support initially set selectedIds
.
<MultiSelect
selectedIds={["0", "1"]}
titleText="Contact"
label="Select contact methods..."
items={[
{ id: "0", text: "Slack" },
{ id: "1", text: "Email" },
{ id: "2", text: "Fax" },
]}
/>
I've pushed a 6fbedab that does the following:
I've so far tested with both Svelte 4 and Svelte 5. |
6fbedab
to
fe7790b
Compare
Thank for reviewing and catching the |
Arguably that was a bug; the property was not marked as being reactive and who really wants this? |
It is marked as reactive in the docs (https://svelte.carbondesignsystem.com/components/MultiSelect#props), though, as you say, maybe it’s not useful for this prop to be reactive. |
Right, I forgot that the tags are generated based on usage, so they don't appear in the source code JSDoc. |
Nice catch – |
Released in v0.86.0 |
Fixes #1986
Here’s an attempt to fix the issues in the
MultiSelect
component that caused incompatibility with Svelte 5 by creating infinite loops (#1986). It seems like the core issue was the circular dependency between theitems
andsortedItems
variables:items
assigned tosortedItems
in anafterUpdate
block:carbon-components-svelte/src/MultiSelect/MultiSelect.svelte
Line 261 in ac38e9d
sortedItems
depends onitems
:The commit removes the
sortedItems
variable, which served an intermediate role, and changes are made toitems
directly.