Skip to content

fix(faq): correct state handling on close and improve button accessibility#113

Open
ZinMyoWin wants to merge 1 commit into
ui-layouts:mainfrom
ZinMyoWin:fix/faq-accordion-toggle
Open

fix(faq): correct state handling on close and improve button accessibility#113
ZinMyoWin wants to merge 1 commit into
ui-layouts:mainfrom
ZinMyoWin:fix/faq-accordion-toggle

Conversation

@ZinMyoWin

@ZinMyoWin ZinMyoWin commented May 25, 2026

Copy link
Copy Markdown
  • Fixed a bug where activeItem state was updated incorrectly when closing a tab
  • Replaced tabs.find() array search with a direct index lookup for optimization
  • Removed unnecessary async keyword from handleClick function
  • Moved onClick handler from the wrapper div to the button element for better web accessibility (a11y)

Summary by CodeRabbit

  • Refactor
    • Improved accordion/FAQ component interaction handling by optimizing click detection and state management.
    • Enhanced animation performance by streamlining transition logic and removing unnecessary delays.

Review Change Stack

…ility

- Fixed a bug where activeItem state was updated incorrectly when closing a tab
- Replaced tabs.find() array search with a direct index lookup for optimization
- Removed unnecessary async keyword from handleClick function
- Moved onClick handler from the wrapper div to the button element for better web accessibility (a11y)
@vercel

vercel Bot commented May 25, 2026

Copy link
Copy Markdown

@ZinMyoWin is attempting to deploy a commit to the UI-Layouts OSS Program Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The FAQ accordion component undergoes refactoring to improve type safety and simplify state management. A TabItem type is introduced, click handling is made synchronous with clearer toggle logic, and rendering is unified to use a local isOpen boolean derived from activeIndex. Event handling moves to the button element, and animation transitions are adjusted.

Changes

FAQ Accordion Refactoring

Layer / File(s) Summary
Data structure and type definition
apps/ui-layout/registry/components/accordion/faq.tsx
Introduces TabItem type to type the activeItem state and cleans up string formatting in the final tabs array entry.
Click handler refactoring
apps/ui-layout/registry/components/accordion/faq.tsx
Removes async keyword and simplifies tab toggle logic; the handler now synchronously toggles activeIndex while setting or clearing activeItem based on the clicked index.
Accordion rendering and state management
apps/ui-layout/registry/components/accordion/faq.tsx
Relocates click handler from outer motion.div to inner button, introduces local isOpen boolean for standardized open/closed behavior, updates icon rotation and animated panel transitions, and removes fragment wrapper.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A FAQ refactored with type-safe care,
Click handlers lean, state flowing fair,
Async begone, the logic now clear—
Render and rotate, year after year!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately summarizes the main changes: fixing state handling on close (activeItem bug fix) and improving button accessibility (onClick moved to button element).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/ui-layout/registry/components/accordion/faq.tsx (1)

38-38: ⚡ Quick win

Consider removing unused activeItem state.

The activeItem state is updated in handleClick but never read anywhere in the component. The rendering logic relies entirely on activeIndex and the derived isOpen boolean. Unless this state is needed for future features or debugging, it can be safely removed to simplify the component.

♻️ Proposed cleanup
-  const [activeItem, setActiveItem] = useState<TabItem | undefined>(tabs[0]);

Then remove the setActiveItem calls from handleClick:

   const handleClick = (index: number) => {
     const isCurrentActive = activeIndex === index;

     if (isCurrentActive) {
-      // If clicking the active tab, close it and clear the active item
       setActiveIndex(null);
-      setActiveItem(undefined);
     } else {
-      // If clicking a new tab, open it and set the active item cleanly using index matching
       setActiveIndex(index);
-      setActiveItem(tabs[index]);
     }
   };
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/ui-layout/registry/components/accordion/faq.tsx` at line 38, Remove the
unused state activeItem and its setter setActiveItem from the component: delete
the useState line that declares activeItem and remove any setActiveItem(...)
calls inside handleClick; ensure rendering still uses activeIndex and the
derived isOpen boolean (and existing tabs) so behavior is unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/ui-layout/registry/components/accordion/faq.tsx`:
- Around line 69-79: The accordion toggle button lacks an explicit type and will
default to type="submit" inside forms; update the button element used in the FAQ
accordion (the element that calls handleClick(index) and renders <Plus ... />
and {tab.title}) to include type="button" so it never triggers form submission
unintentionally.

---

Nitpick comments:
In `@apps/ui-layout/registry/components/accordion/faq.tsx`:
- Line 38: Remove the unused state activeItem and its setter setActiveItem from
the component: delete the useState line that declares activeItem and remove any
setActiveItem(...) calls inside handleClick; ensure rendering still uses
activeIndex and the derived isOpen boolean (and existing tabs) so behavior is
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 70ab2a5f-391c-4090-89e2-853d1185c4f4

📥 Commits

Reviewing files that changed from the base of the PR and between ee23f5e and ea30900.

📒 Files selected for processing (1)
  • apps/ui-layout/registry/components/accordion/faq.tsx

Comment on lines 69 to 79
<button
className={`p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2
`}
onClick={() => handleClick(index)}
className='p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2'
>
<Plus
className={`${
activeIndex === index ? 'rotate-45' : 'rotate-0 '
} transition-transform ease-in-out w-5 h-5 dark:text-neutral-200 text-neutral-600`}
isOpen ? 'rotate-45' : 'rotate-0'
} transition-transform duration-300 ease-in-out w-5 h-5 dark:text-neutral-200 text-neutral-600`}
/>
{tab.title}
</button>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add type="button" to prevent unintended form submissions.

The button element is missing an explicit type attribute. Without it, the button defaults to type="submit", which can cause unintended form submissions if this accordion is placed inside a <form> element. As per coding guidelines, static analysis correctly flagged this accessibility and correctness issue.

🔘 Proposed fix
               <button
+                type='button'
                 onClick={() => handleClick(index)}
                 className='p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2'
               >
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<button
className={`p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2
`}
onClick={() => handleClick(index)}
className='p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2'
>
<Plus
className={`${
activeIndex === index ? 'rotate-45' : 'rotate-0 '
} transition-transform ease-in-out w-5 h-5 dark:text-neutral-200 text-neutral-600`}
isOpen ? 'rotate-45' : 'rotate-0'
} transition-transform duration-300 ease-in-out w-5 h-5 dark:text-neutral-200 text-neutral-600`}
/>
{tab.title}
</button>
<button
type='button'
onClick={() => handleClick(index)}
className='p-3 px-2 w-full cursor-pointer sm:text-base text-xs items-center transition-all font-semibold dark:text-white text-black flex gap-2'
>
<Plus
className={`${
isOpen ? 'rotate-45' : 'rotate-0'
} transition-transform duration-300 ease-in-out w-5 h-5 dark:text-neutral-200 text-neutral-600`}
/>
{tab.title}
</button>
🧰 Tools
🪛 Biome (2.4.15)

[error] 69-72: Provide an explicit type prop for the button element.

(lint/a11y/useButtonType)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/ui-layout/registry/components/accordion/faq.tsx` around lines 69 - 79,
The accordion toggle button lacks an explicit type and will default to
type="submit" inside forms; update the button element used in the FAQ accordion
(the element that calls handleClick(index) and renders <Plus ... /> and
{tab.title}) to include type="button" so it never triggers form submission
unintentionally.

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.

1 participant