sveltestrap icon indicating copy to clipboard operation
sveltestrap copied to clipboard

Input type="radio" or "checkbox" group binding does not work

Open bestguy opened this issue 5 years ago • 10 comments

bind:group={...} does not work with Input. For example:

<script>
  let scoops = 1;
</script>

<Label>
  <Input type=radio bind:group={scoops} value={1} /> One scoop
</Label>
<Label>
  <Input type=radio bind:group={scoops} value={2} /> Two scoops
</Label>
<Label>
  <Input type=radio bind:group={scoops} value={3} /> Three scoops
</Label>

https://svelte.dev/examples#group-inputs

bestguy avatar Oct 06 '19 18:10 bestguy

Likewise, on:input="{...} does not work with <Input type= "number" but does with <input type="number" Here is what I am using:

<Row>
    <Col> <FormGroup>
    <Label for="Pounds">Pounds</Label>
    <Input value={l} on:input="{e => setBothFromL(e.target.value)}" min="0" 
      type="number"
      name="Pounds"
      id="Pounds"
      placeholder="lbs" />
  </FormGroup></Col>
    <Col> <FormGroup>
    <Label for="Kilogram">Kilogram</Label>
    <Input value={k} on:input="{e => setBothFromK(e.target.value)}" min="0"
      type="number"
      name="Kilogram"
      id="Kilogram"
      placeholder="kgs" />
  </FormGroup></Col>
  </Row>

macsupport avatar Oct 18 '19 20:10 macsupport

Thanks @macsupport , this is tracked in #36 and plan to support soon, especially for common ones like input, change, hover, etc.

However the "official" way to use Input is:

 <Input
      bind:value={k}
      min="0"
      type="number"
      name="Kilogram"
      id="Kilogram"
      placeholder="kgs"
/>

and you'd call setBothFromK when value changes. Let me know if this works for you, and will also fix #36 hopefully soon.

bestguy avatar Oct 19 '19 20:10 bestguy

Am addressing common Input events with: https://github.com/bestguy/sveltestrap/pull/72 Will publish soon after merge.

bestguy avatar Oct 20 '19 15:10 bestguy

@macsupport Your Input use case should be fixed in [email protected]

bestguy avatar Oct 20 '19 16:10 bestguy

Hi @bestguy first, thanks for this library!

I don't think this is fixed though, certainly, I tested and it doesn't work and further to that, in Input.d.ts, there is not a group option in

IInputProps

lovetoast avatar Jan 20 '21 16:01 lovetoast

Hi @lovetoast , Sorry my comment about "fixed in 3.2.7" was related to macsupport's side comment about on:input.

The group issue is related to https://github.com/sveltejs/svelte/issues/2308 where Svelte does not support bind:group with nested components like Sveltestrap.

Would love to know a workaround or fix if you find one!

bestguy avatar Jan 20 '21 16:01 bestguy

There is a workaround suggestion from community: https://github.com/sveltejs/svelte/issues/2308#issuecomment-548616383

I'll see if it's something we could add to sveltestrap.

bestguy avatar Jan 20 '21 16:01 bestguy

@bestguy thanks for the fast response, that was actually going to be my suggestion haha!

lovetoast avatar Jan 20 '21 16:01 lovetoast

@bestguy

Sorry to bump what looks like a dead thread, but I see this is still open and wonder where it is at. My experience is I am able to bind:group for radio buttons, but not check boxes and digging lead me here.

DenverEllis avatar Dec 02 '22 05:12 DenverEllis

I wonder if what @DenverEllis commented (and I also experienced) is related to missing bind:group here... I tried adding to my copy of the package, but no clue how to actually build that to get it into my project lol

Besides that, there's also a bad conflict of IDs; in absence of a given id, it uses the raw label, and having more than one input with the same label causes clashing. Since you're creating something that must be unique in the page, it's advised to add a random string to it, to avoid unintended clashes for the user.

igorsantos07 avatar Dec 09 '23 04:12 igorsantos07