Skip to content
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

fix(accordion): add missing ref to AccordionItem #3718

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from

Conversation

AnthonyPaulO
Copy link
Contributor

@AnthonyPaulO AnthonyPaulO commented Sep 5, 2024

Closes #3498

📝 Description

Fix issue where AccordionItem ref is not properly implemented, add typescript typing

⛳️ Current behavior (updates)

AccordionItem does not currently support ref.

🚀 New behavior

AccordionItem now implements ref.

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

Accordion Ref Bug Fixed Accordion Ref Bug Test Pass

Summary by CodeRabbit

  • New Features

    • Enhanced the AccordionItem component to support ref forwarding, improving focus management and interactivity.
    • Added a reference to AccordionItem within the accordion package for better organization and accessibility.
  • Tests

    • Added a new test case to verify the correct forwarding of refs in the AccordionItem, increasing test coverage and reliability.

Copy link

changeset-bot bot commented Sep 5, 2024

🦋 Changeset detected

Latest commit: 97ddc6d

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

This PR includes changesets to release 2 packages
Name Type
@nextui-org/accordion Patch
@nextui-org/react 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

Copy link

vercel bot commented Sep 5, 2024

Someone is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Sep 5, 2024

Walkthrough

The changes introduce a ref prop to the AccordionItem component, allowing it to forward references to its underlying DOM element. This functionality is validated through a new test case that ensures the ref is correctly attached when the component is rendered. Additionally, the type definition for AccordionItemBaseProps is updated to accept the new ref prop, enhancing the component's usability in React applications.

Changes

File Path Change Summary
packages/components/accordion/__tests__/accordion.test.tsx Added a test case to verify ref forwarding for AccordionItem.
packages/components/accordion/src/accordion.tsx Modified AccordionItem to accept and forward a ref prop from AccordionGroup.
packages/components/accordion/src/base/accordion-item-base.tsx Updated AccordionItemBaseProps to include an optional ref property of type Ref<HTMLButtonElement>.

Assessment against linked issues

Objective Addressed Explanation
AccordionItem should accept the ref prop (#3498)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 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.

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: 2

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 81da063 and 094dffe.

Files selected for processing (3)
  • packages/components/accordion/tests/accordion.test.tsx (1 hunks)
  • packages/components/accordion/src/accordion.tsx (1 hunks)
  • packages/components/accordion/src/base/accordion-item-base.tsx (2 hunks)
Additional context used
Biome
packages/components/accordion/src/base/accordion-item-base.tsx

[error] 97-97: Don't use '{}' as a type.

Prefer explicitly define the object shape. '{}' means "any non-nullable value".

(lint/complexity/noBannedTypes)

Additional comments not posted (2)
packages/components/accordion/src/accordion.tsx (1)

39-39: Refactor the ref prop handling for better robustness.

The current implementation of forwarding the ref prop to AccordionItem uses the expression (props.children[index] || props.children).ref. This approach assumes that children will always have a ref property and can handle being accessed by index, which might not always be the case.

Consider using React's React.Children API to more safely interact with children. This API provides utilities like React.Children.map that handle different types of children (single child, no children, or an array of children) more gracefully.

Here's a suggested refactor:

- ref={(props.children[index] || props.children).ref}
+ ref={React.Children.map(props.children, (child, idx) => idx === index ? child.ref : null)}

This change ensures that the ref is only applied to the correct child and handles various forms of children more safely.

packages/components/accordion/src/base/accordion-item-base.tsx (1)

10-10: Approved import statement for Ref.

The addition of Ref from React is necessary for implementing the new ref prop functionality in the AccordionItem component.

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

please include changeset

@AnthonyPaulO
Copy link
Contributor Author

please include changeset

oops, done!

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 094dffe and 97ddc6d.

Files selected for processing (1)
  • .changeset/neat-trains-sit.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • .changeset/neat-trains-sit.md

@@ -94,7 +94,8 @@ export interface Props<T extends object = {}>
HeadingComponent?: As;
}

export type AccordionItemBaseProps<T extends object = {}> = Props<T> & AccordionItemVariantProps;
export type AccordionItemBaseProps<T extends object = {}> = Props<T> &
AccordionItemVariantProps & {ref?: Ref<HTMLButtonElement>};
Copy link
Member

Choose a reason for hiding this comment

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

ref should be from Props

@@ -36,6 +36,7 @@ const AccordionGroup = forwardRef<"div", AccordionProps>((props, ref) => {
return (
<Fragment key={item.key}>
<AccordionItem
ref={(props.children[index] || props.children).ref}
Copy link
Member

Choose a reason for hiding this comment

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

please explain both case

"@nextui-org/accordion": patch
---

Added ref to AccordionItem
Copy link
Member

Choose a reason for hiding this comment

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

please also include issue number

Added ref to AccordionItem (#3498)

@wingkwong wingkwong changed the title fix: add ref to AccordionItem #3498 fix(accordion): add missing ref to AccordionItem Sep 6, 2024
Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

also check with #3498 (comment)

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

Successfully merging this pull request may close these issues.

AccordionItem does not forward ref
2 participants