Skip to content

Commit

Permalink
Add inputRef to Checkbox and RadioButton (#4612)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Jan 23, 2025
1 parent 1203a3f commit efb37a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-jeans-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/core": minor
---

Add `inputRef` prop to `Checkbox` and `RadioButton`. The `inputRef` can be used to access the hidden `<input>` element.
10 changes: 9 additions & 1 deletion packages/core/src/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type FocusEventHandler,
type InputHTMLAttributes,
type ReactNode,
type Ref,
forwardRef,
useRef,
} from "react";
Expand All @@ -15,6 +16,7 @@ import type { DataAttributes } from "../types";
import {
makePrefixer,
useControlled,
useForkRef,
useIsomorphicLayoutEffect,
} from "../utils";
import { CheckboxIcon } from "./CheckboxIcon";
Expand Down Expand Up @@ -54,6 +56,10 @@ export interface CheckboxProps
* Properties applied to the input element.
*/
inputProps?: Partial<InputHTMLAttributes<HTMLInputElement>> & DataAttributes;
/**
* Used to access the hidden `<input>` element.
*/
inputRef?: Ref<HTMLInputElement>;
/**
* The label to be shown next to the checkbox.
*/
Expand Down Expand Up @@ -97,6 +103,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
error,
indeterminate,
inputProps = {},
inputRef: inputRefProp,
label,
name,
onBlur,
Expand Down Expand Up @@ -156,6 +163,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
: undefined;

const inputRef = useRef<HTMLInputElement>(null);
const handleInputRef = useForkRef(inputRefProp, inputRef);

const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
// Workaround for https://github.com/facebook/react/issues/9023
Expand Down Expand Up @@ -217,7 +225,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
onChange={handleChange}
onFocus={onFocus}
type="checkbox"
ref={inputRef}
ref={handleInputRef}
{...restInputProps}
/>
<CheckboxIcon
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/radio-button/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type FocusEventHandler,
type InputHTMLAttributes,
type ReactNode,
type Ref,
forwardRef,
} from "react";
import { useFormFieldProps } from "../form-field-context";
Expand Down Expand Up @@ -42,6 +43,10 @@ export interface RadioButtonProps
* Props to be passed to the radio input
*/
inputProps?: Partial<InputHTMLAttributes<HTMLInputElement>> & DataAttributes;
/**
* Used to access the hidden `<input>` element.
*/
inputRef?: Ref<HTMLInputElement>;
/**
* The label to be shown next to the radio icon
*/
Expand Down Expand Up @@ -88,6 +93,7 @@ export const RadioButton = forwardRef<HTMLLabelElement, RadioButtonProps>(
disabled: disabledProp,
error,
inputProps = {},
inputRef,
label,
name: nameProp,
onFocus,
Expand Down Expand Up @@ -196,6 +202,7 @@ export const RadioButton = forwardRef<HTMLLabelElement, RadioButtonProps>(
onChange={handleChange}
onFocus={onFocus}
type="radio"
ref={inputRef}
{...restInputProps}
/>
<RadioButtonIcon
Expand Down

0 comments on commit efb37a0

Please sign in to comment.