Skip to content

Commit

Permalink
Fix primefaces#5610: Radio/Checkbox always fire onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jan 9, 2024
1 parent 8f174ee commit 5c5ebb1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
53 changes: 26 additions & 27 deletions components/lib/checkbox/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMountEffect, useUpdateEffect } from '../hooks/Hooks';
import { CheckIcon } from '../icons/check';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, IconUtils, ObjectUtils, classNames, mergeProps } from '../utils/Utils';
import { CheckboxBase } from './CheckboxBase';
import { useHandleStyle } from '../componentbase/ComponentBase';

export const Checkbox = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -37,35 +37,34 @@ export const Checkbox = React.memo(
const checkboxClicked = event.target instanceof HTMLDivElement || event.target instanceof HTMLSpanElement || event.target instanceof Object;
const isInputToggled = event.target === inputRef.current;
const isCheckboxToggled = checkboxClicked && event.target.checked !== checked;

if (isInputToggled || isCheckboxToggled) {
const value = checked ? props.falseValue : props.trueValue;
const eventData = {
originalEvent: event,
const value = checked ? props.falseValue : props.trueValue;
const eventData = {
originalEvent: event,
value: props.value,
checked: value,
stopPropagation: () => {
event.stopPropagation();
},
preventDefault: () => {
event.preventDefault();
},
target: {
type: 'checkbox',
name: props.name,
id: props.id,
value: props.value,
checked: value,
stopPropagation: () => {
event.stopPropagation();
},
preventDefault: () => {
event.preventDefault();
},
target: {
type: 'checkbox',
name: props.name,
id: props.id,
value: props.value,
checked: value
}
};

props.onClick && props.onClick(eventData);

// do not continue if the user defined click wants to prevent
if (event.defaultPrevented) {
return;
checked: value
}
};

props.onClick && props.onClick(eventData);

// do not continue if the user defined click wants to prevent
if (event.defaultPrevented) {
return;
}

if (isInputToggled || isCheckboxToggled) {
props.onChange && props.onChange(eventData);
}

Expand Down
56 changes: 28 additions & 28 deletions components/lib/radiobutton/RadioButton.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react';
import { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMountEffect } from '../hooks/Hooks';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, ObjectUtils, classNames, mergeProps } from '../utils/Utils';
import { RadioButtonBase } from './RadioButtonBase';
import { useHandleStyle } from '../componentbase/ComponentBase';

export const RadioButton = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -38,36 +38,36 @@ export const RadioButton = React.memo(
const radioClicked = event.target instanceof HTMLDivElement;
const inputClicked = event.target === inputRef.current;
const isInputToggled = inputClicked && event.target.checked !== checked;
const isRadioToggled = radioClicked && (DomHandler.isAttributeEquals(elementRef.current, 'data-p-checked', true) === checked ? !checked : false);

if (isInputToggled || isRadioToggled) {
const value = !checked;
const eventData = {
originalEvent: event,
const isRadioToggled = radioClicked && (DomHandler.hasClass(elementRef.current, 'p-radiobutton-checked') === checked ? !checked : false);
const value = !checked;

const eventData = {
originalEvent: event,
value: props.value,
checked: value,
stopPropagation: () => {
event.stopPropagation();
},
preventDefault: () => {
event.preventDefault();
},
target: {
type: 'radio',
name: props.name,
id: props.id,
value: props.value,
checked: value,
stopPropagation: () => {
event.stopPropagation();
},
preventDefault: () => {
event.preventDefault();
},
target: {
type: 'radio',
name: props.name,
id: props.id,
value: props.value,
checked: value
}
};

props.onClick && props.onClick(eventData);

// do not continue if the user defined click wants to prevent
if (event.defaultPrevented) {
return;
checked: value
}
};

props.onClick && props.onClick(eventData);

// do not continue if the user defined click wants to prevent
if (event.defaultPrevented) {
return;
}

if (isInputToggled || isRadioToggled) {
props.onChange && props.onChange(eventData);

if (isRadioToggled) {
Expand Down

0 comments on commit 5c5ebb1

Please sign in to comment.