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

RadioButton/Checkbox: onChange event is called for each click #3148

Closed
mikejav opened this issue Aug 9, 2022 · 10 comments · Fixed by #3149
Closed

RadioButton/Checkbox: onChange event is called for each click #3148

mikejav opened this issue Aug 9, 2022 · 10 comments · Fixed by #3149
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@mikejav
Copy link

mikejav commented Aug 9, 2022

Describe the bug

The nature of the radio button's onChange event is that they should call the onChange event only when its state is actually changed.

Steps to reproduce:

  1. render RadioButton component with checked prop set to true and add onChange event handler that does something.
  2. click on that RadioButton multiple times.

Result:

The onChange event is called even if the state was not changed.

Expected behavior:

When the RadioButton is clicked multiple times and if its state is "checked" - it should not call onChange.

Reproducer

No response

PrimeReact version

7.2.1

React version

17.x

Language

TypeScript

Build / Runtime

Webpack

Browser(s)

Chrome 103.0.5060.134

@mikejav mikejav added Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible Type: Bug Issue contains a defect related to a specific component. labels Aug 9, 2022
@melloware melloware changed the title Component: RadioButton onChange event is called for each click RadioButton/Checkbox: onChange event is called for each click Aug 9, 2022
@melloware melloware added 👍 confirmed and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Aug 9, 2022
@melloware
Copy link
Member

Confirmed with Reproducer: https://codesandbox.io/s/primereact-test-forked-hcj1zl?file=/src/index.js

Same issue for RadioButton and Checkbox.

melloware added a commit to melloware/primereact that referenced this issue Aug 9, 2022
@melloware melloware self-assigned this Aug 9, 2022
@melloware melloware added this to the 9.0.0 milestone Aug 9, 2022
melloware added a commit to melloware/primereact that referenced this issue Aug 9, 2022
@mertsincan mertsincan modified the milestones: 9.0.0, 8.4.0 Aug 22, 2022
@harrybradshaw
Copy link
Contributor

I am surprised that this behaviour was considered a bug for the Checkbox component.
Because your component allows the state to be externally managed (checked, onChange props), it seems a poor assumption to make that the click successfully causes the state to update. If the state update fails or is overridden, you are then left with a checkbox that is unusable without refreshing the page, as you cannot attempt the action again.

@melloware
Copy link
Member

I have a better fix if you don't mind testing it for your scenario?

@harrybradshaw
Copy link
Contributor

Happy to test, is there a PR/branch that I can check this out on?

@melloware
Copy link
Member

Yep PR here: #3223

@harrybradshaw
Copy link
Contributor

harrybradshaw commented Sep 1, 2022

Sad to say does not work, would suggest that the issue lies in having two sources of truth, both the inputRef.current.checked and the props.checked.

Checkbox.js:34
inputRef.current.checked = !checked;

Can make the component out of sync with the external state that is being passed into props.checked.
The useEffect already will keep these in sync and the docs don't seem to suggest that it should be able to run without a checked prop, so perhaps can just remove this?

@melloware
Copy link
Member

can you put together a code sandbox showing the issue using this? https://codesandbox.io/s/q2u1lr that way i can see what the issue is?

@harrybradshaw
Copy link
Contributor

Have done: https://codesandbox.io/s/lucid-visvesvaraya-kcc8qt?file=/src/demo/CheckboxDemo.js
With a PR that fixes this, in addition to some other ref related issues I found with clicking between labels and on the radiobuttons themselves: #3226

@mikejav
Copy link
Author

mikejav commented Nov 14, 2022

I checked on the latest PrimeReact version and looks like the bug is still reproducible for clicking on radio-button. Clicking on its label works as expected.

https://codesandbox.io/s/radio-bux-7m27iq-7m27iq

radio-on-change

@melloware
Copy link
Member

@mikejav please open a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants