Last active
February 23, 2023 07:32
-
-
Save nikitapashinsky/46bd59f899cde25f1a95cb9f8cb2aace to your computer and use it in GitHub Desktop.
Svelte radio group rendered within the #each block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/* | |
This is an adapted example where radio buttons are rendered from an array. | |
I removed everything related to flavours for brevity (use {selectedScoops} instead of {scoops}) | |
https://svelte.dev/examples/group-inputs | |
*/ | |
let scoops = [ | |
{ value: 1, text: "one" }, | |
{ value: 2, text: "two" }, | |
{ value: 3, text: "three" } | |
] | |
// Make first radio button checked by default | |
let selectedScoops = scoops[0].value; | |
</script> | |
{#each scoops as scoop} | |
<label> | |
<input | |
type=radio | |
name="scoops" | |
bind:group={selectedScoops} | |
value={scoop.value} | |
> | |
{scoop.text} {scoop.value === 1 ? 'scoop' : 'scoops'} | |
</label> | |
{/each} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment