components icon indicating copy to clipboard operation
components copied to clipboard

sl-change event fired unexpectedly

Open fransdeboer opened this issue 1 year ago โ€ข 1 comments

Provide a general summary of the issue here

if a sl-checkbox-group and sl-radio-group are used in the same component. A change in sl-radio-group triggers the sl-change of the sl-checkbox-group

๐Ÿค” Expected Behavior?

change event of sl-checkbox-group and sl-radio-group should not interact with each other.

๐Ÿ˜ฏ Current Behavior

see general summary

๐Ÿ’ Possible Solution

No response

๐Ÿ–ฅ๏ธ Steps to Reproduce

clone the repository at https://github.com/fransdeboer/sl-change-poc

install dependencies npm ci

run the example npm run dev

start a browser and open http://localhost:5173/

check both or one of the check boxes

change the radio button to Yes

the sl-change event of the sl-checkbox-group is fired and the selection of the check boxes is erased.

image

What browsers are you seeing the problem on? (only for bugs in code)

Chrome

If other, please specify.

No response

What operating system are you using?

WSL

๐Ÿงข Your product/team

Magister

๐ŸŽจ Your Theme(s)

Magister

fransdeboer avatar Jul 26 '24 12:07 fransdeboer

The problem that occurred was not (directly) because the checkbox group and radio group events got mixed up. The setting of the variable that toggles the visibility of the extra options causes a redraw/update and that causes the form controls to rerender, with the value that is currently set. Because the value of the checkbox group was not updated on change (.value=${this.visibilityOptions} is no two-way binding) it was being "reset" once the change in the radiogroup caused a redraw.

Solution: https://github.com/fransdeboer/sl-change-poc/pull/1

What we can do is look into whether we need (and if not, can prevent) an emit of the change event on redraw/ initial render.

Diaan avatar Aug 12 '24 11:08 Diaan

I'm going to close this issue with the following explanation:

What you are seeing is not actually SLDS behavior but instead is Lit behavior:

  • After checking a checkbox, the value of the checkbox group changes
  • When you select an option in the radio group, change detection is triggert (you're changing a state variable)
  • Lit then compares values of the checkbox group and sees they don't match
  • So Lit "updates" the value property of the checkbox group

The solution here is to either

  • keep the form control values in sync
  • use a FormController do keep the form control values in sync for you

jpzwarte avatar Aug 23 '24 11:08 jpzwarte