From e3043f77cb11b2b5c3164fea3aa97b2ddca7742e Mon Sep 17 00:00:00 2001 From: Harry Bradshaw Date: Thu, 1 Sep 2022 19:13:22 +0100 Subject: [PATCH 1/2] Remove second source of truth for Checkbox and RadioButton, such that they respect external checked prop. --- components/lib/checkbox/Checkbox.js | 7 ++++--- components/lib/radiobutton/RadioButton.js | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/lib/checkbox/Checkbox.js b/components/lib/checkbox/Checkbox.js index 15a1ff7367..507265f9ed 100644 --- a/components/lib/checkbox/Checkbox.js +++ b/components/lib/checkbox/Checkbox.js @@ -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, @@ -29,7 +31,6 @@ export const Checkbox = React.memo( checked: value } }); - inputRef.current.checked = !checked; } DomHandler.focus(inputRef.current); diff --git a/components/lib/radiobutton/RadioButton.js b/components/lib/radiobutton/RadioButton.js index 886b7bf4b7..7b7da65970 100644 --- a/components/lib/radiobutton/RadioButton.js +++ b/components/lib/radiobutton/RadioButton.js @@ -1,4 +1,5 @@ import * as React from 'react'; +import { useUpdateEffect } from '../hooks/Hooks'; import { Tooltip } from '../tooltip/Tooltip'; import { classNames, DomHandler, ObjectUtils } from '../utils/Utils'; @@ -9,14 +10,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, @@ -32,7 +36,6 @@ export const RadioButton = React.memo( checked: value } }); - inputRef.current.checked = value; } DomHandler.focus(inputRef.current); From 6a71af955707ae013e0a8fee818a246b16f2b746 Mon Sep 17 00:00:00 2001 From: Melloware Date: Thu, 1 Sep 2022 16:43:46 -0400 Subject: [PATCH 2/2] Removed dead import --- components/lib/radiobutton/RadioButton.js | 1 - 1 file changed, 1 deletion(-) diff --git a/components/lib/radiobutton/RadioButton.js b/components/lib/radiobutton/RadioButton.js index 7b7da65970..40c887538f 100644 --- a/components/lib/radiobutton/RadioButton.js +++ b/components/lib/radiobutton/RadioButton.js @@ -1,5 +1,4 @@ import * as React from 'react'; -import { useUpdateEffect } from '../hooks/Hooks'; import { Tooltip } from '../tooltip/Tooltip'; import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';