Skip to content
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

svelte: enable two-way binding for ListInput component #213

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dfkdream
Copy link

This PR enables two-way binding for ListInput svelte component.
Fixes issue #206

2024-07-29.13-08-30.mp4

@QuiescentTrip
Copy link

i don't get why this is not merged

@o-in25
Copy link

o-in25 commented Sep 5, 2024

Having this issue as well, would like to see this get merged.

@ilyakamens
Copy link
Contributor

I'd also like this merged.

@dfkdream
Copy link
Author

Hi everyone,
Thank you all so much for your kind words!
@nolimits4web, I was wondering if there have been any updates regarding this PR, or if you have any feedback to share. I would really appreciate hearing your thoughts!

@ilyakamens
Copy link
Contributor

@dfkdream, I took a closer look at your code. It's not immediately clear to me which of the changes are necessary to enable two-way binding for ListInput components. Are all of those changes necessary?

@dfkdream
Copy link
Author

dfkdream commented Oct 2, 2024

@dfkdream, I took a closer look at your code. It's not immediately clear to me which of the changes are necessary to enable two-way binding for ListInput components. Are all of those changes necessary?

@ilyakamens, Yes, all the changes are necessary. The most important change was replacing {value} with bind:value, but I had to make additional changes to ensure everything worked. Here’s a breakdown:

  • svelte:element replaced with select/input: This resolves the error 'bind:value' can only be used with <input>, <textarea>, <select>.

  • {multiple} replaced with {...{multiple}}: This fixes the issue where the 'multiple' attribute must be static if select uses two-way binding.

  • type={...} replaced with {...{type}}: This corrects the error 'type' attribute cannot be dynamic if input uses two-way binding.

The last two changes feel a bit hacky, but they work. Reference: https://stackoverflow.com/questions/57392773

While addressing your question, I noticed that the textarea input isn’t working as expected. I'll be adding more commits to fix that. Thanks!

@@ -193,7 +193,7 @@
{:else}
<!-- svelte-ignore a11y-autofocus -->
{#if type === 'select'}
<svelte:element
<select
this={InputComponent}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in the other cases, is this still needed? I thought this is what determined what tag was used when using svelte:element.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It's not necessary, but I left it there to minimize code changes. I'll remove it (along with some other unnecessary lines), as I've already made more changes than I initially expected.

on:input={onInput}
on:change={onChange}
on:focus={onFocusInternal}
on:blur={onBlurInternal}
>
<slot />
</svelte:element>
</select>
{:else if type !== 'textarea'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but I'd do:

Suggested change
{:else if type !== 'textarea'}
{:else if type === 'textarea'}
<textarea ... />
{:else}
<input ... />

@@ -217,28 +217,63 @@
{min}
{minlength}
{step}
{multiple}
{...{ multiple }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the value of multiple on the select element won't change if the parent changes the multiple value it's passing in? I.e., the parent can set it, but then can't change it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the component will update when the parent changes the value. This line just bypasses Svelte's static check.

@ilyakamens
Copy link
Contributor

@dfkdream, thanks for the explanation! I left a couple questions now that I had that additional context.

  • svelte:element replaced with select/input: This resolves the error 'bind:value' can only be used with <input>, <textarea>, <select>.

Looks like the docs explicitly mention this re bind:value.

@dfkdream
Copy link
Author

dfkdream commented Oct 5, 2024

@ilyakamens, I’ve added a cleanup commit based on your suggestion. Thank you for the review!

@ilyakamens
Copy link
Contributor

@dfkdream, looks great! Now we just have to wait and see what happens with the PR. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants