diff --git a/src/hooks/useItems.tsx b/src/hooks/useItems.tsx index 4b937a9..114e749 100644 --- a/src/hooks/useItems.tsx +++ b/src/hooks/useItems.tsx @@ -1,7 +1,8 @@ import toArray from '@rc-component/util/lib/Children/toArray'; import React from 'react'; -import type { CollapsePanelProps, CollapseProps, ItemType } from '../interface'; +import type { CollapsePanelProps, CollapseProps, ItemType, SemanticName } from '../interface'; import CollapsePanel from '../Panel'; +import clsx from 'clsx'; type Props = Pick< CollapsePanelProps, @@ -22,7 +23,7 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => { openMotion, expandIcon, classNames: collapseClassNames, - styles, + styles: collapseStyles, } = props; return items.map((item, index) => { @@ -33,6 +34,8 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => { collapsible: rawCollapsible, onItemClick: rawOnItemClick, destroyOnHidden: rawDestroyOnHidden, + classNames, + styles, ...restProps } = item; @@ -57,11 +60,29 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => { isActive = activeKey.indexOf(key) > -1; } + const mergeClassNames: Partial> = { + ...collapseClassNames, + header: clsx(collapseClassNames?.header, classNames?.header), + body: clsx(collapseClassNames?.body, classNames?.body), + }; + + const mergeStyles: Partial> = { + ...collapseStyles, + header: { + ...collapseStyles?.header, + ...styles?.header, + }, + body: { + ...collapseStyles?.body, + ...styles?.body, + }, + }; + return ( { expect(titleElement.style.color).toBe('green'); expect(iconElement.style.color).toBe('yellow'); }); + + it('should support styles and classNames in panel', () => { + const customStyles = { + header: { color: 'red' }, + body: { color: 'blue' }, + title: { color: 'green' }, + icon: { color: 'yellow' }, + }; + const customClassnames = { + header: 'custom-header', + body: 'custom-body', + }; + + const { container } = render( + , + ); + const headerElement = container.querySelector('.rc-collapse-header') as HTMLElement; + const bodyElement = container.querySelector('.rc-collapse-body') as HTMLElement; + + // check classNames + expect(headerElement.classList).toContain('custom-header'); + expect(headerElement.classList).toContain('custom-header-panel'); + expect(bodyElement.classList).toContain('custom-body'); + expect(bodyElement.classList).toContain('custom-body-panel'); + + // check styles + expect(headerElement.style.color).toBe('blue'); + expect(headerElement.style.fontSize).toBe('20px'); + expect(bodyElement.style.color).toBe('blue'); + expect(bodyElement.style.fontSize).toBe('20px'); + }); }); });