Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(CheckboxInput): preserve caller-provided IDs (#7242)

zakelstormc05e8d29e90b
diff --git a/.changeset/checkbox-file-input-ids.md b/.changeset/checkbox-file-input-ids.md
new file mode 100644
index 0000000000..4af00f2559
--- /dev/null
+++ b/.changeset/checkbox-file-input-ids.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Preserve caller-provided IDs on CheckboxInput and keep labels associated with the native input.
+@korkt-kim
diff --git a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx b/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
index 5d4b39281f..92a2520114 100644
--- a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
+++ b/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
@@ -209,6 +209,40 @@ describe('CheckboxInput', () => {
);
});
+ it('preserves a caller-provided id on the input and its label', () => {
+ render(
+ <CheckboxInput
+ id="accept-terms"
+ label="Accept terms"
+ value={false}
+ onChange={() => {}}
+ />,
+ );
+
+ const checkbox = screen.getByRole('checkbox');
+ expect(checkbox).toHaveAttribute('id', 'accept-terms');
+ expect(screen.getByText('Accept terms').closest('label')).toHaveAttribute(
+ 'for',
+ 'accept-terms',
+ );
+ expect(screen.getByLabelText('Accept terms')).toBe(checkbox);
+ });
+
+ it('generates an id the label resolves when the caller passes an empty id', () => {
+ render(
+ <CheckboxInput
+ id=""
+ label="Accept terms"
+ value={false}
+ onChange={() => {}}
+ />,
+ );
+
+ const checkbox = screen.getByRole('checkbox');
+ expect(checkbox.id).not.toBe('');
+ expect(screen.getByLabelText('Accept terms')).toBe(checkbox);
+ });
+
it('forwards arbitrary data-* attributes to the input', () => {
render(
<CheckboxInput
diff --git a/packages/core/src/CheckboxInput/CheckboxInput.tsx b/packages/core/src/CheckboxInput/CheckboxInput.tsx
index 67447d171c..09dc33e86a 100644
--- a/packages/core/src/CheckboxInput/CheckboxInput.tsx
+++ b/packages/core/src/CheckboxInput/CheckboxInput.tsx
@@ -311,6 +311,7 @@ const dynamicWidthStyles = stylex.create({
* ```
*/
export function CheckboxInput({
+ id: idProp,
label,
isLabelHidden = false,
description,
@@ -340,7 +341,8 @@ export function CheckboxInput({
// The row is the pressable: the overlay's pressed arm reads the row's
// scope marker, the way the indicator's hover tint does.
const pressable = usePressFeedback();
- const id = useId();
+ const generatedId = useId();
+ const id = idProp || generatedId;
const descriptionID = useId();
const statusMessageID = useId();
// Announce the effective required state (form default included) while the