Skip to content

feat(v3): Radio, RadioGroup and FieldRadioGroup #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 4, 2025

Conversation

ntatoud
Copy link
Member

@ntatoud ntatoud commented May 8, 2025

I followed the aria guidelines: https://www.w3.org/WAI/ARIA/apg/patterns/radio/

  • Radio
    • Component
  • RadioGroup
    • Component
    • Docs
  • FieldRadioGroup
    • Component
    • Docs
    • Tests
    • Add FormFieldController

I also squeezed in a small accessibility fix on the descriptionId and labelId for the FormField.

Summary by CodeRabbit

  • New Features

    • Introduced a new radio group form field component with full integration into forms, supporting custom rendering, disabled states, and validation.
    • Added Storybook stories for both the radio group form field and UI radio group components, demonstrating various layouts and customization options.
  • Bug Fixes

    • Improved accessibility by assigning dynamic IDs to form field labels and helper elements.
  • Tests

    • Added comprehensive tests for the radio group form field, covering accessibility, interactions, default values, keyboard navigation, and disabled states.
  • Chores

    • Updated a dependency to a newer version and added a test environment mock for scroll behavior.

Copy link

vercel bot commented May 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
start-ui ❌ Failed (Inspect) Jul 3, 2025 7:31pm
start-ui-web-restart ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 3, 2025 7:31pm

Copy link
Contributor

coderabbitai bot commented May 8, 2025

Walkthrough

A radio group form field component and its supporting UI primitives are introduced, including Storybook stories and comprehensive tests. The form field system is extended to support radio groups, with improved accessibility via dynamic label and description IDs. The @base-ui-components/react dependency is updated, and test setup is adjusted for browser API compatibility.

Changes

File(s) Change Summary
app/components/form/field-radio-group/index.tsx Added FieldRadioGroup React component with generic props and radio option types.
app/components/form/field-radio-group/docs.stories.tsx Added Storybook stories for FieldRadioGroup with various usage scenarios and custom rendering.
app/components/form/field-radio-group/field-radio-group.spec.tsx Added test suite for FieldRadioGroup covering accessibility, interaction, and form integration.
app/components/ui/radio-group.tsx Added RadioGroup and Radio UI components with styling, accessibility, and optional label logic.
app/components/ui/radio-group.stories.tsx Added Storybook stories for RadioGroup and Radio with various configurations and custom UI.
app/components/form/form-field-controller.tsx Extended to support "radio-group" type, rendering FieldRadioGroup as needed.
app/components/form/form-field.tsx Added labelId to form field context and its type for improved accessibility.
app/components/form/form-field-label.tsx, app/components/form/form-field-helper.tsx Set id attributes for label and helper elements using context-provided IDs.
app/tests/setup.ts Mocked Element.prototype.scrollTo for test environment compatibility.
package.json Updated @base-ui-components/react dependency version.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Form
    participant FieldRadioGroup
    participant RadioGroup
    participant Radio
    participant RHF as ReactHookForm

    User->>Form: Submit or interact with radio group
    Form->>FieldRadioGroup: Render with control, name, options
    FieldRadioGroup->>RHF: Use Controller to manage value
    FieldRadioGroup->>RadioGroup: Render radio group UI
    RadioGroup->>Radio: Render each radio option
    User->>Radio: Select a radio option
    Radio->>FieldRadioGroup: onChange event
    FieldRadioGroup->>RHF: Update form state
    RHF->>Form: Trigger validation and submission
Loading
sequenceDiagram
    participant Storybook
    participant FieldRadioGroup
    participant CustomRadio
    participant User

    Storybook->>FieldRadioGroup: Render with custom renderOption
    FieldRadioGroup->>CustomRadio: Use renderOption for each option
    User->>CustomRadio: Select option (click)
    CustomRadio->>FieldRadioGroup: onChange event
    FieldRadioGroup->>Storybook: Form state updates, submit handled
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c979fa and 1a8d325.

📒 Files selected for processing (1)
  • app/components/ui/radio-group.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/components/ui/radio-group.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Playwright E2E Tests
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yoannfleurydev
Copy link
Member

yoannfleurydev commented May 8, 2025

There is an issue on first selection (using label click and not radio click)

image

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
app/components/form/field-radio-group/index.tsx (1)

21-25: Consider extracting the RadioOptionProps type.

As suggested in past review comments, this type could be extracted to a shared location if it's used elsewhere in the codebase.

-type RadioOptionProps = {
+export type RadioOptionProps = {
   value: string;
   label: string;
   disabled?: boolean;
-};
+} as const;

This makes it reusable and applies readonly constraint as suggested in previous feedback.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8b459c and f34f876.

📒 Files selected for processing (6)
  • app/components/form/field-radio-group/docs.stories.tsx (1 hunks)
  • app/components/form/field-radio-group/field-radio-group.spec.tsx (1 hunks)
  • app/components/form/field-radio-group/index.tsx (1 hunks)
  • app/components/form/form-field-controller.tsx (3 hunks)
  • app/components/ui/radio-group.stories.tsx (1 hunks)
  • app/components/ui/radio-group.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
app/components/form/form-field-controller.tsx (1)
app/components/form/field-radio-group/index.tsx (2)
  • FieldRadioGroupProps (27-46)
  • FieldRadioGroup (48-142)
app/components/ui/radio-group.stories.tsx (2)
app/components/ui/radio-group.tsx (3)
  • RadioGroup (74-74)
  • Radio (74-74)
  • RadioItem (74-74)
app/components/form/field-radio-group/docs.stories.tsx (4)
  • Default (41-63)
  • DefaultValue (65-92)
  • Disabled (94-122)
  • Row (124-147)
app/components/ui/radio-group.tsx (1)
app/lib/tailwind/utils.ts (1)
  • cn (4-6)
app/components/form/field-radio-group/index.tsx (4)
app/components/form/form-field-controller.tsx (1)
  • FieldProps (32-39)
app/components/ui/radio-group.tsx (2)
  • RadioGroup (74-74)
  • Radio (74-74)
app/components/form/form-field.tsx (1)
  • useFormField (48-54)
app/lib/tailwind/utils.ts (1)
  • cn (4-6)
🪛 GitHub Actions: 🔎 Code Quality
app/components/form/field-radio-group/field-radio-group.spec.tsx

[error] 1-1: Invalid environment variables: 'VITE_BASE_URL' is required but was not provided.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Playwright E2E Tests
🔇 Additional comments (10)
app/components/form/form-field-controller.tsx (1)

13-13: Clean integration of radio-group support.

The addition of FieldRadioGroup support follows the established patterns in the form controller. The import, type definition, and switch case are all correctly implemented and consistent with existing field types.

Also applies to: 52-52, 89-90

app/components/ui/radio-group.tsx (2)

9-17: Clean RadioGroup implementation.

The RadioGroup component provides a clean wrapper around Radix primitives with sensible defaults and customization options.


23-65: Verify label‐click behavior on first interaction

The Radio component correctly uses useId() to generate a unique id, applies it to RadioGroupPrimitive.Item, and sets htmlFor on the <label>. No other radio implementations were found for comparison.

• File to test:

  • app/components/ui/radio-group.tsx (lines 23–65)
    • Confirm in your dev environment that clicking the label selects the radio on the very first click, per the PR objectives.
app/components/ui/radio-group.stories.tsx (1)

17-101: Comprehensive story coverage with good examples.

The stories effectively demonstrate all major use cases of the RadioGroup component, including custom styling with RadioItem. The examples are clear and follow Storybook best practices.

app/components/form/field-radio-group/field-radio-group.spec.tsx (1)

30-245: Excellent test coverage for form integration.

The test suite provides comprehensive coverage of accessibility, interactions, keyboard navigation, and various states. The use of axe for accessibility testing and thorough keyboard navigation tests demonstrates attention to usability.

app/components/form/field-radio-group/docs.stories.tsx (1)

41-230: Comprehensive form integration documentation.

The stories effectively demonstrate all aspects of the FieldRadioGroup component, from basic usage to advanced custom rendering. The validation integration and various configuration options are well-documented, providing clear guidance for developers.

app/components/form/field-radio-group/index.tsx (4)

69-75: LGTM! Clean conditional rendering implementation.

The use of getUiState for handling conditional rendering between custom and default radio rendering is well-implemented and provides good separation of concerns.


98-109: LGTM! Proper accessibility implementation.

The accessibility attributes are correctly implemented:

  • aria-invalid properly reflects validation state
  • aria-describedby correctly references description and error IDs
  • Form field integration follows established patterns

78-88: LGTM! Proper react-hook-form Controller integration.

The Controller setup correctly handles all necessary props and properly destructures the render options for form state management.


98-135: Check label-targeting in the Radio component

The issue isn’t the onBlur handler itself but that the <label htmlFor> may not be pointing at an actual <input> element. Radix’s RadioGroupPrimitive.Item renders a non-input element (e.g. a div or button with role="radio"), so on the first click the label doesn’t activate selection.

• In app/components/ui/radio-group.tsx (the Radio function), verify which HTML element receives the id and whether it’s a real <input> that a <label> can target.
• In app/components/form/field-radio-group/index.tsx (lines 98–135), confirm that the label’s htmlFor matches a real radio input and selects on first click.
• If it isn’t an <input>, consider one of:
– using <RadioGroupPrimitive.Item asChild> wrapping a native <input type="radio">
– switching to the built-in RadioItem alias or another input-based pattern
– moving your blur logic up to the RadioGroup root so it doesn’t need per-item listeners

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
app/components/form/field-radio-group/index.tsx (1)

25-25: Improve type safety by making options readonly.

Based on past review feedback, consider making the options array readonly to prevent accidental mutations and improve type safety.

- options: Array<RadioOption>;
+ options: readonly RadioOption[];
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f34f876 and 7527d06.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (6)
  • app/components/form/field-radio-group/docs.stories.tsx (1 hunks)
  • app/components/form/field-radio-group/index.tsx (1 hunks)
  • app/components/ui/radio-group.stories.tsx (1 hunks)
  • app/components/ui/radio-group.tsx (1 hunks)
  • app/tests/setup.ts (1 hunks)
  • package.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • package.json
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/components/ui/radio-group.tsx
  • app/components/ui/radio-group.stories.tsx
🧰 Additional context used
🧠 Learnings (1)
app/components/form/field-radio-group/index.tsx (2)
Learnt from: ivan-dalmet
PR: BearStudio/start-ui-web#532
File: src/features/auth/PageOAuthCallback.tsx:43-45
Timestamp: 2024-09-30T11:07:14.833Z
Learning: When suggesting changes to `useEffect` dependencies in React components, ensure that removing dependencies doesn't cause React Hook errors about missing dependencies.
Learnt from: ivan-dalmet
PR: BearStudio/start-ui-web#532
File: src/server/config/oauth/providers/discord.ts:11-11
Timestamp: 2024-10-14T15:29:53.279Z
Learning: In `src/server/config/oauth/providers/discord.ts`, when defining the `zDiscordUser` schema, keep fields like `username` as nullable (e.g., `.nullish()`), as the Discord API might return null values despite the documentation stating otherwise.
🧬 Code Graph Analysis (1)
app/components/form/field-radio-group/index.tsx (4)
app/components/ui/radio-group.tsx (4)
  • RadioProps (18-23)
  • RadioGroupProps (8-8)
  • RadioGroup (9-16)
  • Radio (24-48)
app/components/form/form-field-controller.tsx (1)
  • FieldProps (32-39)
app/components/form/form-field.tsx (1)
  • useFormField (48-54)
app/lib/tailwind/utils.ts (1)
  • cn (4-6)
🔇 Additional comments (4)
app/tests/setup.ts (1)

16-19: LGTM! Necessary mock for jsdom compatibility.

The scrollTo mock is essential for testing environments since jsdom doesn't implement this method. This prevents test failures when components use scrolling functionality.

app/components/form/field-radio-group/docs.stories.tsx (1)

1-253: Excellent comprehensive Storybook documentation!

The stories effectively demonstrate all key features of the FieldRadioGroup component, including:

  • Basic usage with validation
  • Default values and disabled states
  • Horizontal layout configuration
  • Individual disabled options
  • Custom rendering with the CardRadio example

The custom CardRadio component demonstrates advanced customization possibilities with proper accessibility handling and visual feedback.

app/components/form/field-radio-group/index.tsx (2)

31-104: Component implementation looks solid overall.

The FieldRadioGroup component is well-implemented with proper:

  • Form integration via react-hook-form Controller
  • Accessibility attributes handling
  • Support for both default and custom rendering
  • Form field context usage

The component should handle the label clicking issue mentioned in the PR objectives correctly since it properly delegates to the RadioGroup's onValueChange handler.


68-73: No missing descriptionId; context always provides it.

The FormFieldContextValue type and its provider in app/components/form/form-field.tsx ensure descriptionId is always a string (\${id}-description`). You don’t need to guard against an undefined ctx.descriptionId. If the real concern is that you will reference an ID for a description element that the user never rendered, handle that at the rendering site instead (e.g., only emit aria-describedbywhendescription` exists) rather than here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
app/components/form/field-radio-group/index.tsx (1)

25-25: Consider making options array readonly for better type safety.

As suggested in past reviews, making the options array readonly prevents accidental mutations and improves type safety.

Apply this diff to enhance type safety:

- options: Array<RadioOption>;
+ options: readonly RadioOption[];
🧹 Nitpick comments (1)
app/components/form/field-radio-group/index.tsx (1)

16-18: Fix unnecessary type omission in RadioOption.

The RadioOption type omits 'render' from RadioProps, but based on the UI component definition, RadioProps doesn't have a render property. This omission is unnecessary and potentially confusing.

Apply this diff to clean up the type definition:

-type RadioOption = Omit<RadioProps, 'children' | 'render'> & {
+type RadioOption = Omit<RadioProps, 'children'> & {
   label: string;
 };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ec0f77b and 3a82e32.

📒 Files selected for processing (11)
  • app/components/form/field-otp/field-otp.spec.tsx (1 hunks)
  • app/components/form/field-radio-group/docs.stories.tsx (1 hunks)
  • app/components/form/field-radio-group/field-radio-group.spec.tsx (1 hunks)
  • app/components/form/field-radio-group/index.tsx (1 hunks)
  • app/components/form/field-text/field-text.spec.tsx (1 hunks)
  • app/components/form/form-field-controller.tsx (3 hunks)
  • app/components/form/form-field-helper.tsx (1 hunks)
  • app/components/form/form-field-label.tsx (1 hunks)
  • app/components/form/form-field.tsx (2 hunks)
  • app/components/ui/radio-group.stories.tsx (1 hunks)
  • app/components/ui/radio-group.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (10)
  • app/components/form/field-otp/field-otp.spec.tsx
  • app/components/form/field-text/field-text.spec.tsx
  • app/components/form/form-field-label.tsx
  • app/components/form/form-field.tsx
  • app/components/form/form-field-controller.tsx
  • app/components/ui/radio-group.tsx
  • app/components/form/form-field-helper.tsx
  • app/components/form/field-radio-group/docs.stories.tsx
  • app/components/ui/radio-group.stories.tsx
  • app/components/form/field-radio-group/field-radio-group.spec.tsx
🧰 Additional context used
🧠 Learnings (1)
app/components/form/field-radio-group/index.tsx (3)
Learnt from: ivan-dalmet
PR: BearStudio/start-ui-web#532
File: src/features/auth/PageOAuthCallback.tsx:43-45
Timestamp: 2024-09-30T11:07:14.833Z
Learning: When suggesting changes to `useEffect` dependencies in React components, ensure that removing dependencies doesn't cause React Hook errors about missing dependencies.
Learnt from: ivan-dalmet
PR: BearStudio/start-ui-web#532
File: src/server/config/oauth/providers/discord.ts:11-11
Timestamp: 2024-10-14T15:29:53.279Z
Learning: In `src/server/config/oauth/providers/discord.ts`, when defining the `zDiscordUser` schema, keep fields like `username` as nullable (e.g., `.nullish()`), as the Discord API might return null values despite the documentation stating otherwise.
Learnt from: DecampsRenan
PR: BearStudio/start-ui-web#537
File: src/features/devtools/EnvHintDevPopover.tsx:0-0
Timestamp: 2024-10-11T14:57:53.600Z
Learning: When using `React.cloneElement`, remember that it automatically merges the child's existing `props` with the new props provided, so manually spreading `children.props` is unnecessary.
🧬 Code Graph Analysis (1)
app/components/form/field-radio-group/index.tsx (4)
app/components/ui/radio-group.tsx (4)
  • RadioProps (18-23)
  • RadioGroupProps (8-8)
  • RadioGroup (9-16)
  • Radio (24-48)
app/components/form/form-field-controller.tsx (1)
  • FieldProps (32-39)
app/components/form/form-field.tsx (1)
  • useFormField (50-56)
app/lib/tailwind/utils.ts (1)
  • cn (4-6)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Playwright E2E Tests
🔇 Additional comments (3)
app/components/form/field-radio-group/index.tsx (3)

67-81: Excellent accessibility implementation.

The ARIA attributes are properly implemented with conditional error handling. The comment explaining why aria-labelledby is used instead of the label's for attribute is particularly helpful for understanding the RadioGroup's behavior.


82-102: Well-structured option rendering with proper key handling.

The implementation correctly handles both custom renderOption and default Radio rendering. The key is properly applied to the React.Fragment wrapper, which fixes the issue mentioned in past reviews. The consistent passing of onBlur to both custom and default renderers ensures proper form integration.


50-109: Verify label clicking behavior for custom renderOption implementations.

Based on the PR objectives, there's a reported issue with label clicking not working on first selection. While the default Radio implementation should handle this correctly through its built-in label wrapping, custom renderOption implementations might not properly associate labels with radio inputs.

Consider adding documentation or examples showing how to properly implement the renderOption callback to ensure label clicking works correctly:

// Example of proper renderOption implementation
renderOption: ({ label, value, ...props }) => (
  <label className="custom-radio-label">
    <input 
      type="radio" 
      value={value} 
      {...props}
    />
    {label}
  </label>
)

Would you like me to help create comprehensive documentation or examples for the renderOption callback?

Copy link

sonarqubecloud bot commented Jul 3, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
32.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@ivan-dalmet ivan-dalmet merged commit 0040e92 into v3-main Jul 4, 2025
14 of 16 checks passed
@ivan-dalmet ivan-dalmet deleted the v3/feat/field-radio branch July 4, 2025 09:06
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