Skip to content

Conversation

@GoldGroove06
Copy link
Contributor

@GoldGroove06 GoldGroove06 commented Apr 25, 2025

added useControllableState to accordion. closes #1010

Summary by CodeRabbit

  • New Features
    • Accordion component now supports both controlled and uncontrolled state management for open items, allowing external control via new props.
  • Documentation
    • Added Storybook examples demonstrating usage with default and controlled values, including interactive controls for testing different open states.
  • Tests
    • Expanded test coverage to include scenarios for controlled and uncontrolled Accordion behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 25, 2025

"""

Walkthrough

The Accordion component was updated to support both controlled and uncontrolled modes by introducing new props: value, defaultValue, and onValueChange. The internal state management was refactored to use a custom hook that handles both controlled and uncontrolled scenarios. Storybook stories were added to demonstrate usage with these new props, including examples for both controlled and default value initialization. The test suite was also expanded to verify correct behavior in both modes, with specific tests for controlled state updates and default value initialization.

Changes

Files/Paths Change Summary
src/components/ui/Accordion/fragments/AccordionRoot.tsx Refactored to support controlled/uncontrolled state via new props (value, defaultValue, onValueChange). Updated prop types and internal state logic.
src/components/ui/Accordion/stories/Accordion.stories.tsx Added two new stories: one using defaultValue, one demonstrating controlled usage with buttons.
src/components/ui/Accordion/tests/Accordion.test.tsx Updated test helper to accept AccordionRootProps. Added tests for controlled and default value scenarios.
src/components/ui/Card/stories/Card.stories.tsx, src/core/primitives/Avatar/fragments/AvatarPrimitiveRoot.tsx, src/core/utils/RovingFocusGroup/context/RovingFocusRootContext.tsx, src/core/utils/RovingFocusGroup/fragments/RovingFocusRoot.tsx Minor formatting fixes: removed trailing commas and adjusted indentation/whitespace without affecting logic or behavior.

Sequence Diagram(s)

sequenceDiagram
    participant ParentComponent
    participant AccordionRoot
    participant useControllableState

    ParentComponent->>AccordionRoot: Pass value/defaultValue/onValueChange props
    AccordionRoot->>useControllableState: Initialize state (controlled or uncontrolled)
    useControllableState-->>AccordionRoot: Return current state and setter
    AccordionRoot-->>ParentComponent: Call onValueChange when state changes (if controlled)
    ParentComponent-->>AccordionRoot: Update value prop (if controlled)
Loading

Assessment against linked issues

Objective Addressed Explanation
Support value, defaultValue, and onValueChange props for controlled/uncontrolled Accordion (#1010)

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • kotAPI

Poem

Accordion now bends to your will,
Controlled or free, it fits the bill!
With stories and tests, it’s ready to show,
Open or closed, just let it know.
A hop, a skip, a bunny cheer—
Accordion magic is finally here! 🐇🎶
"""


📜 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 dbac3b3 and 49120c0.

📒 Files selected for processing (2)
  • src/components/ui/Accordion/fragments/AccordionRoot.tsx (2 hunks)
  • src/components/ui/Accordion/stories/Accordion.stories.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/components/ui/Accordion/fragments/AccordionRoot.tsx
  • src/components/ui/Accordion/stories/Accordion.stories.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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.

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

🧹 Nitpick comments (2)
src/components/ui/Accordion/stories/Accordion.stories.tsx (1)

101-103: Fix typo in story name.

There's a spelling error in the story name: "WithDeafultValue" should be "WithDefaultValue".

-export const WithDeafultValue: Story = {
+export const WithDefaultValue: Story = {
    render: () => <AccordionExample defaultValue={[2]} />    
};
src/components/ui/Accordion/tests/Accordion.test.tsx (1)

145-146: Remove extra blank line.

There's an extra blank line that should be removed according to the ESLint warning.

      });

-
      test('works with defaultValue to show initial item', () => {
🧰 Tools
🪛 ESLint

[error] 145-146: More than 1 blank line not allowed.

(no-multiple-empty-lines)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4d24b93 and eff9992.

📒 Files selected for processing (3)
  • src/components/ui/Accordion/fragments/AccordionRoot.tsx (2 hunks)
  • src/components/ui/Accordion/stories/Accordion.stories.tsx (2 hunks)
  • src/components/ui/Accordion/tests/Accordion.test.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: kotAPI
PR: rad-ui/ui#1031
File: src/components/ui/Accordion/fragments/AccordionRoot.tsx:41-44
Timestamp: 2025-04-07T04:38:34.864Z
Learning: The Accordion component in rad-ui/ui supports both controlled and uncontrolled modes through props like `value`, `defaultValue`, and `onValueChange`. When implementing controlled components, remember to: 1) Initialize state from defaultValue, 2) Update internal state when value changes (controlled mode), 3) Call onValueChange callback, and 4) Prevent internal state updates when in controlled mode.
src/components/ui/Accordion/fragments/AccordionRoot.tsx (1)
Learnt from: kotAPI
PR: rad-ui/ui#1031
File: src/components/ui/Accordion/fragments/AccordionRoot.tsx:41-44
Timestamp: 2025-04-07T04:38:34.864Z
Learning: The Accordion component in rad-ui/ui supports both controlled and uncontrolled modes through props like `value`, `defaultValue`, and `onValueChange`. When implementing controlled components, remember to: 1) Initialize state from defaultValue, 2) Update internal state when value changes (controlled mode), 3) Call onValueChange callback, and 4) Prevent internal state updates when in controlled mode.
🪛 ESLint
src/components/ui/Accordion/tests/Accordion.test.tsx

[error] 145-146: More than 1 blank line not allowed.

(no-multiple-empty-lines)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (8)
src/components/ui/Accordion/fragments/AccordionRoot.tsx (4)

6-6: Good addition of the useControllableState hook.

This properly supports both controlled and uncontrolled modes as mentioned in the retrieved learning.


22-24: Well-implemented props for controlled/uncontrolled state management.

These new props align perfectly with React patterns:

  • value for controlled state
  • defaultValue for initial uncontrolled state
  • onValueChange callback to notify parent components of state changes

27-27: Good default value initialization.

Setting defaultValue = [] ensures the component works correctly in uncontrolled mode without any items initially open.


31-35: Excellent implementation of useControllableState.

The useControllableState hook is correctly implemented to handle both controlled and uncontrolled modes, managing the internal state while respecting external control when provided.

src/components/ui/Accordion/stories/Accordion.stories.tsx (1)

105-119: Well-implemented controlled mode example.

This story effectively demonstrates how to use the Accordion in controlled mode with external state management. The inclusion of buttons to manipulate the state provides a clear example of how to control the component from outside.

src/components/ui/Accordion/tests/Accordion.test.tsx (3)

6-6: Good updates to TestAccordion to support new props.

The TestAccordion component has been correctly updated to accept and spread Accordion props, enabling proper testing of the new functionality.

Also applies to: 17-19


105-143: Comprehensive test for controlled mode.

This test thoroughly verifies the controlled behavior of the Accordion component, ensuring it responds correctly to external state changes.


146-153: Good test for defaultValue initialization.

This test correctly verifies that the Accordion properly initializes with the specified defaultValue.

@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Apr 27, 2025
@kodiakhq kodiakhq bot merged commit 3abb78a into rad-ui:main Apr 27, 2025
5 checks passed
@kotAPI kotAPI mentioned this pull request Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[New Feature] Controlled + Uncontrolled modes for Accordion component

2 participants