Skip to content

Commit a5d71b7

Browse files
committed
fix: merge classNames and styles in the panel, and add test
1 parent ecd38af commit a5d71b7

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

src/hooks/useItems.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import toArray from '@rc-component/util/lib/Children/toArray';
22
import React from 'react';
3-
import type { CollapsePanelProps, CollapseProps, ItemType } from '../interface';
3+
import type { CollapsePanelProps, CollapseProps, ItemType, SemanticName } from '../interface';
44
import CollapsePanel from '../Panel';
5+
import clsx from 'clsx';
56

67
type Props = Pick<
78
CollapsePanelProps,
@@ -59,17 +60,29 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => {
5960
isActive = activeKey.indexOf(key) > -1;
6061
}
6162

63+
const mergeClassNames: Partial<Record<SemanticName, string>> = {
64+
...collapseClassNames,
65+
header: clsx(collapseClassNames?.header, classNames?.header),
66+
body: clsx(collapseClassNames?.body, classNames?.body),
67+
};
68+
69+
const mergeStyles: Partial<Record<SemanticName, React.CSSProperties>> = {
70+
...collapseStyles,
71+
header: {
72+
...collapseStyles?.header,
73+
...styles?.header,
74+
},
75+
body: {
76+
...collapseStyles?.body,
77+
...styles?.body,
78+
},
79+
};
80+
6281
return (
6382
<CollapsePanel
6483
{...restProps}
65-
classNames={{
66-
...collapseClassNames,
67-
...classNames,
68-
}}
69-
styles={{
70-
...collapseStyles,
71-
...styles,
72-
}}
84+
classNames={mergeClassNames}
85+
styles={mergeStyles}
7386
prefixCls={prefixCls}
7487
key={key}
7588
panelKey={key}

tests/index.spec.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,5 +898,59 @@ describe('collapse', () => {
898898
expect(titleElement.style.color).toBe('green');
899899
expect(iconElement.style.color).toBe('yellow');
900900
});
901+
902+
it('should support styles and classNames in panel', () => {
903+
const customStyles = {
904+
header: { color: 'red' },
905+
body: { color: 'blue' },
906+
title: { color: 'green' },
907+
icon: { color: 'yellow' },
908+
};
909+
const customClassnames = {
910+
header: 'custom-header',
911+
body: 'custom-body',
912+
};
913+
914+
const { container } = render(
915+
<Collapse
916+
activeKey={['1']}
917+
styles={customStyles}
918+
classNames={customClassnames}
919+
items={[
920+
{
921+
key: '1',
922+
styles: {
923+
header: {
924+
color: 'blue',
925+
fontSize: 20,
926+
},
927+
body: {
928+
fontSize: 20,
929+
},
930+
},
931+
classNames: {
932+
header: 'custom-header-panel',
933+
body: 'custom-body-panel',
934+
},
935+
label: 'title',
936+
},
937+
]}
938+
/>,
939+
);
940+
const headerElement = container.querySelector('.rc-collapse-header') as HTMLElement;
941+
const bodyElement = container.querySelector('.rc-collapse-body') as HTMLElement;
942+
943+
// check classNames
944+
expect(headerElement.classList).toContain('custom-header');
945+
expect(headerElement.classList).toContain('custom-header-panel');
946+
expect(bodyElement.classList).toContain('custom-body');
947+
expect(bodyElement.classList).toContain('custom-body-panel');
948+
949+
// check styles
950+
expect(headerElement.style.color).toBe('blue');
951+
expect(headerElement.style.fontSize).toBe('20px');
952+
expect(bodyElement.style.color).toBe('blue');
953+
expect(bodyElement.style.fontSize).toBe('20px');
954+
});
901955
});
902956
});

0 commit comments

Comments
 (0)