diff --git a/src/hooks/useItems.tsx b/src/hooks/useItems.tsx index 4b937a9..d01768a 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,39 @@ 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), + title: clsx(collapseClassNames?.title, classNames?.title), + icon: clsx(collapseClassNames?.icon, classNames?.icon), + }; + + const mergeStyles: Partial> = { + ...collapseStyles, + header: { + ...collapseStyles?.header, + ...styles?.header, + }, + body: { + ...collapseStyles?.body, + ...styles?.body, + }, + title: { + ...collapseStyles?.title, + ...styles?.title, + }, + icon: { + ...collapseStyles?.icon, + ...styles?.icon, + }, + }; + 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; + const titleElement = container.querySelector('.rc-collapse-title') as HTMLElement; + const iconElement = container.querySelector('.rc-collapse-expand-icon') 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).toHaveStyle({ color: 'blue', fontSize: '20px' }); + expect(bodyElement).toHaveStyle({ color: 'blue', fontSize: '20px' }); + expect(titleElement).toHaveStyle({ color: 'red' }); + expect(iconElement).toHaveStyle({ color: 'blue' }); + }); }); });