Skip to content

fix: allow passing props shared by all checkboxes / radios to use...Group hook#4864

Open
unekinn wants to merge 5 commits into
mainfrom
fix/useCheckboxAndRadioGroup-variant
Open

fix: allow passing props shared by all checkboxes / radios to use...Group hook#4864
unekinn wants to merge 5 commits into
mainfrom
fix/useCheckboxAndRadioGroup-variant

Conversation

@unekinn
Copy link
Copy Markdown
Contributor

@unekinn unekinn commented May 7, 2026

Summary

useCheckboxGroup and useRadioGroup: these hooks now accept all CheckboxProps / RadioProps that don't conflict with the hooks' own props. This can be used to easily set common props like variant for all the checkboxes or radios in the group.

Checks

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 7, 2026

🦋 Changeset detected

Latest commit: 8450f94

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@digdir/designsystemet-react Patch
@digdir/designsystemet Patch
@digdir/designsystemet-css Patch
@digdir/designsystemet-types Patch
@digdir/designsystemet-web Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@unekinn unekinn marked this pull request as draft May 7, 2026 14:22
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 144c0654-a5a4-4ea5-b9b4-9c9c46120f95

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/useCheckboxAndRadioGroup-variant

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

Preview deployments for this pull request:

storybook - 8. May 2026 - 12:59

www - 8. May 2026 - 13:02

@unekinn unekinn changed the title fix(useCheckboxGroup): allow passing props shared by all checkboxes to useCheckboxGroup fix: allow passing props shared by all checkboxes / radios to use...Group hook May 7, 2026
@unekinn unekinn force-pushed the fix/useCheckboxAndRadioGroup-variant branch 2 times, most recently from b2c0757 to 3a378d2 Compare May 8, 2026 10:11
@unekinn unekinn force-pushed the fix/useCheckboxAndRadioGroup-variant branch from 3a378d2 to dea8d73 Compare May 8, 2026 10:11
@unekinn unekinn force-pushed the fix/useCheckboxAndRadioGroup-variant branch from 8475238 to 8450f94 Compare May 8, 2026 10:57
@unekinn unekinn marked this pull request as ready for review May 8, 2026 10:57
@mimarz
Copy link
Copy Markdown
Collaborator

mimarz commented May 11, 2026

Waiting for reviewing this after we finish web-first docs.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Expands the public surface of useCheckboxGroup and useRadioGroup so callers can pass any CheckboxProps/RadioProps (e.g. variant) at the hook level. Those props are then forwarded to every checkbox/radio via getCheckboxProps/getRadioProps. The www stories are updated to use the hooks instead of hand-rolled controlled state, and a Storybook Disabled story is repointed to Group.args.

Changes:

  • Use MergeRight<CheckboxProps|RadioProps, …> for UseCheckboxGroupProps/UseRadioGroupProps; rebuild a groupProps rest (minus onChange/error) inside the prop getters and spread it before per-element props.
  • Migrate www checkbox/radio Group/Outline stories from local useState to useCheckboxGroup/useRadioGroup (also fixing a value: 'epost'/'email' mismatch in GroupEn); use the hook's new variant support for the Outline stories.
  • Internal react package Disabled checkbox story now extends Group.args (matching its render: Group) and a changeset is added.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/react/src/utilities/hooks/use-radio-group/use-radio-group.ts Switches props type to MergeRight<RadioProps, …> and forwards shared props through getRadioProps.
packages/react/src/utilities/hooks/use-checkbox-group/use-checkbox-group.ts Same change for the checkbox-group hook.
packages/react/src/components/checkbox/checkbox.stories.tsx Repoints Disabled story args to Group.args to match its renderer.
apps/www/app/content/components/radio/radio.stories.tsx Rewrites Group/Outline stories to use useRadioGroup (incl. variant: 'outline').
apps/www/app/content/components/checkbox/checkbox.stories.tsx Rewrites Group/Outline stories to use useCheckboxGroup; fixes GroupEn initial value.
.changeset/fuzzy-zoos-rhyme.md Patch-level changeset describing the new hook capability.
Comments suppressed due to low confidence (2)

packages/react/src/utilities/hooks/use-radio-group/use-radio-group.ts:120

  • Inside getRadioProps the new local const { onChange, error, ...rest } = props; shadows the outer-scope onChange/error already destructured at the top of useRadioGroup (lines 83 & 88). It works because they refer to the same underlying values, but the duplication is confusing and the inner onChange/error are immediately discarded. Consider reusing the variables from the outer scope and computing groupProps from props (omitting onChange and error) once at the top of the hook so the intent is clearer. Same comment applies in useCheckboxGroup at lines 161–163.
      if (props) {
        const { onChange, error, ...rest } = props;
        groupProps = rest;
      }

packages/react/src/utilities/hooks/use-checkbox-group/use-checkbox-group.ts:163

  • groupProps still contains the group-level value (a string[] from the hook's args), and it is spread into the object returned to each checkbox before the explicit value: <string> override later in the same object literal. While property-key ordering currently rescues this (the explicit value wins), passing a string[] as a CheckboxProps['value'] is type-incompatible and the override is easy to break in future edits. Destructure and exclude value (and arguably checked) from groupProps along with onChange and error, so a per-checkbox value is never at risk of being overwritten by an upstream array. The same concern applies in useRadioGroup where the group-level value: string is spread in via ...groupProps before the explicit value override.
      if (props) {
        const { onChange, error, ...rest } = props;
        groupProps = rest;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 113 to +120
getRadioProps: (propsOrValue: string | GetRadioProps) => {
const props =
let groupProps:
| Omit<RadioProps, 'aria-label' | 'aria-labelledby'>
| undefined;
if (props) {
const { onChange, error, ...rest } = props;
groupProps = rest;
}
Comment on lines +12 to +52
export type UseCheckboxGroupProps = MergeRight<
CheckboxProps,
{
/**
* Disables all checkboxes in the group.
*/
disabled?: boolean;
/**
* Error message for the group.
* If set, all checkboxes will have `aria-invalid` set to `true`.
*/
error?: ReactNode;
/**
* Name of the group.
* If not set, a random id will be generated.
*/
name?: string;
/**
* Makes all checkboxes in the group read-only.
* If set, all checkboxes will have `aria-readonly` set to `true`.
*/
readOnly?: boolean;
/**
* Initial value of the group
* @default []
*/
value?: string[];
/**
* Makes all checkboxes in the group required.
* If set, all checkboxes will have `required` set to `true`.
*/
required?: boolean;
/**
* Callback that is called when the value of the group changes.
* @param nextValue string[]
* @param currentValue string[]
* @returns void
*/
onChange?: (nextValue: string[], currentValue: string[]) => void;
}
>;
Comment on lines +81 to +90
export function useRadioGroup(props?: UseRadioGroupProps): useRadioGroupReturn {
const {
error,
readOnly,
required,
disabled,
name,
onChange,
value: initialValue = '',
} = props || {};
Copy link
Copy Markdown
Collaborator

@mimarz mimarz left a comment

Choose a reason for hiding this comment

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

Looks fine to me, just need to fix the conflicts before merge.

@unekinn
Copy link
Copy Markdown
Contributor Author

unekinn commented May 19, 2026

Looks fine to me, just need to fix the conflicts before merge.

Should we do anything about the comments from Copilot? Some of it does make sense 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants