From dc7a8b586299998da6820eee6456258c6eac99a1 Mon Sep 17 00:00:00 2001 From: soroush5 Date: Tue, 17 Nov 2026 09:48:51 +0330 Subject: [PATCH] fix(ExpandableGroup): initialize expandables rendered after the group mounts --- .../expandable/expandable-group.test.tsx | 46 +++++++++++++++++++ .../expandable/expandable-group.tsx | 39 ++++++++++++++-- src/types/cfpb-expandables.d.ts | 8 +++- 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/components/expandable/expandable-group.test.tsx diff --git a/src/components/expandable/expandable-group.test.tsx b/src/components/expandable/expandable-group.test.tsx new file mode 100644 index 0000000000..e3d4b44848 --- /dev/null +++ b/src/components/expandable/expandable-group.test.tsx @@ -0,0 +1,46 @@ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; +import { Expandable } from './expandable'; +import { ExpandableGroup } from './expandable-group'; + +const renderGroup = (headers: string[], isAccordion = false) => ( + + {headers.map((header) => ( + +

{`${header} content`}

+
+ ))} +
+); + +const expectAllInitialized = (): void => { + for (const button of screen.getAllByRole('button')) { + expect(button).toHaveAttribute('aria-expanded', 'false'); + } + for (const content of screen.getAllByTestId('expandable-content')) { + expect(content).toHaveClass('u-hidden'); + } +}; + +describe('', () => { + it('initializes the expandables it renders with', () => { + render(renderGroup(['First', 'Second'])); + expectAllInitialized(); + }); + + it.each([false, true])( + 'initializes expandables added after mount (accordion: %s)', + (isAccordion) => { + const { rerender } = render(renderGroup(['First'], isAccordion)); + rerender(renderGroup(['First', 'Second', 'Third'], isAccordion)); + expectAllInitialized(); + }, + ); + + it('initializes expandables that are re-added after being removed', () => { + const { rerender } = render(renderGroup(['First', 'Second'], true)); + rerender(renderGroup(['First'], true)); + rerender(renderGroup(['First', 'Second'], true)); + expectAllInitialized(); + }); +}); diff --git a/src/components/expandable/expandable-group.tsx b/src/components/expandable/expandable-group.tsx index 5318170f31..c797c48566 100644 --- a/src/components/expandable/expandable-group.tsx +++ b/src/components/expandable/expandable-group.tsx @@ -1,6 +1,9 @@ -import { ExpandableGroup as CFPB_ExpandableGroup } from '@cfpb/cfpb-design-system/src/components/cfpb-expandables'; +import { + Expandable as CFPB_Expandable, + type ExpandableInstance, +} from '@cfpb/cfpb-design-system/src/components/cfpb-expandables'; import classnames from 'classnames'; -import React, { useEffect } from 'react'; +import React, { useEffect, useRef } from 'react'; import type { FC, HTMLAttributes, ReactElement } from 'react'; import type { ExpandableProperties } from './expandable'; @@ -20,10 +23,33 @@ export const ExpandableGroup: FC = ({ const cname = ['o-expandable-group', className]; if (accordion) cname.push('o-expandable-group--accordion'); + const groupReference = useRef(null); + const isAccordionReference = useRef(accordion); + const lastExpandedReference = useRef(null); + useEffect(() => { - CFPB_ExpandableGroup.init(); + isAccordionReference.current = accordion; }, [accordion]); + // Initialize every expandable in this group that is not initialized yet. + // This runs again when the children change, so items rendered after the + // group mounted (async data, filtered lists) work too. + useEffect(() => { + const newExpandables = CFPB_Expandable.init(groupReference.current); + + for (const expandable of newExpandables) { + expandable.addEventListener('expandbegin', () => { + // Accordion: opening one item closes the one opened before it + const lastExpanded = lastExpandedReference.current; + lastExpandedReference.current = expandable; + if (lastExpanded === expandable || !isAccordionReference.current) { + return; + } + lastExpanded?.collapse(); + }); + } + }, [children]); + const childrenWithProperties = React.Children.map(children, (child) => { if (React.isValidElement(child)) { return React.cloneElement(child, { @@ -36,7 +62,12 @@ export const ExpandableGroup: FC = ({ return child; }); return ( -
+
{childrenWithProperties}
); diff --git a/src/types/cfpb-expandables.d.ts b/src/types/cfpb-expandables.d.ts index 02536e00d5..9cfd9eaa87 100644 --- a/src/types/cfpb-expandables.d.ts +++ b/src/types/cfpb-expandables.d.ts @@ -1,8 +1,14 @@ declare module '@cfpb/cfpb-design-system/src/components/cfpb-expandables' { type InitScope = Element | Document | NodeList | null | undefined; + export interface ExpandableInstance { + addEventListener(event: 'expandbegin', callback: () => void): void; + collapse(): void; + } + export const Expandable: { - init(scope?: InitScope): void; + /** Returns the instances it initialized (already initialized ones are skipped). */ + init(scope?: InitScope): ExpandableInstance[]; }; export const ExpandableGroup: { init(scope?: InitScope): void;