Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/components/CheckBox/CheckBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,24 @@ export const CheckBox: FC<CheckboxProps> = React.forwardRef(
},
]);

const applyCheckedState = (checked: boolean) => {
setIsChecked(checked);
onChange?.({
target: { checked, type: 'checkbox' } as HTMLInputElement,
currentTarget: { checked, type: 'checkbox' } as HTMLInputElement,
type: 'change',
} as React.ChangeEvent<HTMLInputElement>);
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
applyCheckedState(!isChecked);
}
};

const toggleChecked = (e: React.ChangeEvent<HTMLInputElement>): void => {
setIsChecked(e.target.checked);
onChange?.(e);
applyCheckedState(e.target.checked);
};
Comment on lines +183 to 201
Copy link
Collaborator

@ypatadia-eightfold ypatadia-eightfold Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        const syntheticEvent = {
          ...e,
          currentTarget: {
            ...e.currentTarget,
            checked: !isChecked,
          } as HTMLInputElement,
          target: {
            ...e.target,
            checked: !isChecked,
          } as HTMLInputElement,
        } as React.ChangeEvent<HTMLInputElement>;
        setIsChecked(!isChecked);
        onChange?.(syntheticEvent);
      }
    };
    const toggleChecked = (e: React.ChangeEvent<HTMLInputElement>): void => {
      setIsChecked(e.target.checked);
      onChange?.(e);
    };

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please test these out. This implmentation for checkbox and radio ensures consistent implementation across both components.


return (
Expand All @@ -205,6 +220,7 @@ export const CheckBox: FC<CheckboxProps> = React.forwardRef(
disabled={!allowDisabledFocus && mergedDisabled}
id={checkBoxId.current}
onChange={!allowDisabledFocus ? toggleChecked : null}
onKeyDown={!allowDisabledFocus ? handleKeyDown : null}
name={name}
type={'checkbox'}
value={value}
Expand Down
32 changes: 28 additions & 4 deletions src/components/RadioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,37 @@ export const RadioButton: FC<RadioButtonProps> = React.forwardRef(
setIsActive(radioGroupContext?.value === value);
}, [radioGroupContext?.value]);

const toggleChecked = (e: React.ChangeEvent<HTMLInputElement>): void => {
const applyRadioChange = (
value: string,
e: React.SyntheticEvent<HTMLInputElement>
) => {
if (!radioGroupContext) {
setSelectedValue(e.currentTarget?.value as RadioButtonValue);
setSelectedValue(value as RadioButtonValue);
} else {
radioGroupContext?.onChange?.(e);
radioGroupContext.onChange?.(e as React.ChangeEvent<HTMLInputElement>);
}

onChange?.(e);
onChange?.(e as React.ChangeEvent<HTMLInputElement>);
};

const toggleChecked = (e: React.ChangeEvent<HTMLInputElement>): void => {
applyRadioChange(e.currentTarget.value, e);
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => {
if (e.key === 'Enter' || e.key === ' ') {
const target = e.target as HTMLInputElement;

setIsActive((prev) => !prev);

const syntheticEvent = {
...e,
currentTarget: target,
target: target,
} as unknown as React.ChangeEvent<HTMLInputElement>;

applyRadioChange(target.value, syntheticEvent);
}
};
Comment on lines +163 to 194
Copy link
Collaborator

@ypatadia-eightfold ypatadia-eightfold Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const toggleChecked = (e: React.ChangeEvent<HTMLInputElement>): void => {
      if (!radioGroupContext) {
        setSelectedValue(e.currentTarget?.value as RadioButtonValue);
      } else {
        radioGroupContext?.onChange?.(e);
      }

      onChange?.(e);
    };
    const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        const syntheticEvent = {
          ...e,
          currentTarget: {
            ...e.currentTarget,
            checked: true,
            value: value,
          } as HTMLInputElement,
          target: {
            ...e.target,
            checked: true,
            value: value,
          } as HTMLInputElement,
        } as React.ChangeEvent<HTMLInputElement>;
        
        toggleChecked(syntheticEvent);
      }
    };


return (
Expand Down Expand Up @@ -196,6 +219,7 @@ export const RadioButton: FC<RadioButtonProps> = React.forwardRef(
value={value}
onChange={!allowDisabledFocus ? toggleChecked : null}
readOnly
onKeyDown={!allowDisabledFocus ? handleKeyDown : null}
{...rest}
/>
<label htmlFor={radioButtonId.current} className={labelClassNames}>
Expand Down