My case is that I have multiple objects in an array. The user should (via radio button) select which item should be used.
However, the user shall also be able to change the name of object (obj.name).
The value shall be stored elsewhere, but it must be the obj.name of the selected object.
Here is a sample code (works out of the box in Svelte REPL):
<script>
let selectedName = "";
let arr = [];
let obj1 = {};
obj1.id = 1;
obj1.name = "One";
let obj2 = {};
obj2.id = 2;
obj2.name = "Two";
let obj3 = {};
obj3.id = 3;
obj3.name = "Three";
arr.push(obj1);
arr.push(obj2);
arr.push(obj3);
</script>
<input type="text" bind:value={selectedName}/>
<hr>
{#each arr as row}
<input type="radio" bind:group={selectedName} value={row.name}/> {row.id}
<input type="text" bind:value={row.name}/>
<br/>
{/each}
In this example, all renders nicely, you can select 1, 2 or 3, and the textbox above gets updated with the name of the object ("One", "Two" or "Three").
If you select 2 and then change the name of 1 from "One" to "Uno", and then select 1 - the value is changed to "Uno" in the textbox above. BUT! If you now change the text from "Uno" to "Ein" - the textbox doesn't update.
The natural thing would be to have the radio button bind the value, like this:
<input type="radio" bind:group={selectedName} bind:value={row.name}/> {row.id}
... but then everything just gets messed up completely! :-)
Is there anyone that can shine a light over this, I'd be very happy!
And to clarify; the solution of storing the id instead of name is not the solution to my problem. Above example is just a simplified version to illustrate the issue.