Skip to content

Commit

Permalink
Fixes #3220 (#3226)
Browse files Browse the repository at this point in the history
* Remove second source of truth for Checkbox and RadioButton, such that they respect external checked prop.

* Removed dead import

Co-authored-by: Harry Bradshaw <harry.bradshaw@softwire.com>
Co-authored-by: Melloware <mellowaredev@gmail.com>
  • Loading branch information
3 people committed Sep 1, 2022
1 parent 8a95e86 commit b573c86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions components/lib/checkbox/Checkbox.js
Expand Up @@ -12,8 +12,10 @@ export const Checkbox = React.memo(
const onClick = (event) => {
if (!props.disabled && !props.readOnly && props.onChange) {
const checked = isChecked();

if (inputRef.current.checked === checked) {
const checkboxClicked = event.target instanceof HTMLDivElement || event.target instanceof HTMLSpanElement;
const isInputToggled = event.target === inputRef.current;
const isCheckboxToggled = checkboxClicked && event.target.checked !== checked;
if (isInputToggled || isCheckboxToggled) {
const value = checked ? props.falseValue : props.trueValue;
props.onChange({
originalEvent: event,
Expand All @@ -29,7 +31,6 @@ export const Checkbox = React.memo(
checked: value
}
});
inputRef.current.checked = !checked;
}

DomHandler.focus(inputRef.current);
Expand Down
8 changes: 5 additions & 3 deletions components/lib/radiobutton/RadioButton.js
Expand Up @@ -9,14 +9,17 @@ export const RadioButton = React.memo(
const inputRef = React.useRef(props.inputRef);

const select = (e) => {
inputRef.current.checked = true;
onClick(e);
};

const onClick = (e) => {
if (!props.disabled && props.onChange) {
const checked = props.checked;
if (inputRef.current.checked === checked) {
const radioClicked = e.target instanceof HTMLDivElement;
const inputClicked = e.target === inputRef.current;
const isInputToggled = inputClicked && e.target.checked !== checked;
const isRadioToggled = radioClicked && !e.target.checked;
if (isInputToggled || isRadioToggled) {
const value = !checked;
props.onChange({
originalEvent: e,
Expand All @@ -32,7 +35,6 @@ export const RadioButton = React.memo(
checked: value
}
});
inputRef.current.checked = value;
}

DomHandler.focus(inputRef.current);
Expand Down

0 comments on commit b573c86

Please sign in to comment.