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

bug: ion-checkbox isn't updating using [checked] attribute and/or formControlName attribute #22272

Closed
thall1961 opened this issue Oct 7, 2020 · 5 comments
Labels

Comments

@thall1961
Copy link

thall1961 commented Oct 7, 2020

Bug Report

Ionic version:

[ ] 4.x
[x] 5.x

Current behavior:
If I have multiple ion-checkbox components and I try to control whether they are checked or not by using the [checked] and/or formControlName attributes, the 'check' does not appear as expected.

Expected behavior:
I would expect the 'check' in the checkbox to appear and disappear in accordance to the checked and not checked values (be checked when true, not be checked when false)

Steps to reproduce:
One parent component controlling the boolean value [checked] should be, 3 child components (2 copies of the same, and 1 other component). The child components emit their respective index numbers which the parent component uses to decide what each child component's ion-checkbox [checked] value should be (sorry, that's a bit confusing... view code for reference).

Related code:
https://github.com/thall1961/ionic-checkbox-bug

<ion-item lines="none">
    <ion-checkbox (click)="toggleDefaultContact()" formControlName="newContact" id="defaultContact{{num}}" color="medium" mode="ios"></ion-checkbox>
    <ion-label color="primary" class="ion-text-wrap">Make this passenger my default contact</ion-label>
</ion-item>

Other information:

Ionic info:

insert the output from ionic info here
@liamdebeasi
Copy link
Contributor

Thanks for the issue and PR. It sounds like you want to prevent the default onClick handler in ion-checkbox from firing so that you can manage the checked/unchecked state yourself. If that's the case, why not call event.stopImmediatePropagation() in toggleDefaultContact instead of adding a new prop to ion-checkbox?

stopImmediatePropagation prevents other listeners of the same event from being called: https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Oct 26, 2020
@ionitron-bot ionitron-bot bot removed the triage label Oct 26, 2020
@thall1961
Copy link
Author

Thanks for the response! That might help, but I'm not sure that's the issue. I've attached a movie that should show the behavior I'm seeing which is not expected. When I try to control the checkbox's checked attribute with [checked], it doesn't seem to work like expected. The same unexpected behavior happens when using the formControlName attribute.

checkbox.mov.zip

And here's the link to my repo again

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Oct 26, 2020
@liamdebeasi
Copy link
Contributor

Thanks for the video. I added event.stopImmediatePropagation() to both toggleDefaultContact in passenger.component.ts and toggleNewContact in contact.component.ts and that seemed to resolve the issue. Can you give that a try and let me know if it resolves the issue on your end?

Note that you will need to pass in $event in the click handler binding on ion-checkbox in your templates.

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Oct 26, 2020
@ionitron-bot ionitron-bot bot removed the triage label Oct 26, 2020
@thall1961
Copy link
Author

Ahh yep, that fixes it. Sheesh that was easy. Thanks for your help!

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Oct 26, 2020
@ionitron-bot
Copy link

ionitron-bot bot commented Nov 25, 2020

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants