Initial value binding to select multiple in Vuejs not working #141
Replies: 4 comments 1 reply
-
I notice you're binding on the options, but the Shoelace docs for Vue show two-way binding a bit differently: <!-- This doesn't work -->
<sl-input v-model="name"></sl-input>
<!-- This works, but it's a bit longer -->
<sl-input :value="name" @input="name = $event.target.value"></sl-input> Unlike the native <wa-select :value="name" @input="name = $event.target.value"></wa-select> For As for why things work differently, it's likely due to the form associated custom element integration we added to Web Awesome to make its form controls first-class citizens via ElementInternals, which wasn't available when Shoelace was created. |
Beta Was this translation helpful? Give feedback.
-
Many thanks for your quick response. I'm not binding to the
Does your answer mean that the
That's also why I assumed that |
Beta Was this translation helpful? Give feedback.
-
After some further digging I realized what the problem is with Vuejs. It is actually the same for the issue described in #164. What I am doing in both cases is setting the data in the
This will NOT work. The most likely cause is that the WA component is not yet ready to be called by Javascript, which is what Vuejs does when setting props (setting attributes would work but has other side effects). I played around a bit trying to find the right timing when the WA component is ready and I came up with the following tests from the Vuejs side:
So, delaying the setting of the data on the WA component does the trick (even a timeout of 0 does work). The easiest variant is to work with @claviska Can you explain how the lifecycle of the WA components works? When is a component ready to be called by Javascript? And would there technically be an option that the WA components make their properties settable earlier in their lifecycle? If there is no way that WA can support this better then it would be good if there was a section in the documentation where solutions to this problem are mentioned. Note that if you load data in the |
Beta Was this translation helpful? Give feedback.
-
It depends on how you load them. If you use the autoloader, it kicks in after the document loads and when wa-* elements are added dynamically (via Mutation Observer). Then they're fetched via If you use WA through the upcoming npm module, Vite will be able to bundle things in the way Vue expects, so the components will be registered before Vue mounts, avoiding all of these timing issues. |
Beta Was this translation helpful? Give feedback.
-
I'm having a strange issue when trying to bind a value to a
<wa-select multiple>
. What I'm doing is this:What I observe is that when I initialize the selection after my Vue component has mounted, then the values of the selection are not displayed in the
<wa-select>
. If afterwards theselection
changes, then the<wa-select>
immediately shows all the selected options.When I do the same with an
<sl-select>
like in the following code, then it works and the selection is displayed after the initialization:Also, when I explicitly use
v-bind:value.attr
on the<wa-select>
and set the values as space-delimited strings, then it works too (this also works with sl-select):I don't understand why there is this difference between Shoelace and Webawesome and whether there is something I'm doing wrong with Webawesome or whether this is a bug.
I tried to create an example demonstrating the problem but I didn't get it to run in Codepen or Vue Playground with the required setup (isCustomElement).
Beta Was this translation helpful? Give feedback.
All reactions