Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve Semantic for collapse #358

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}
.@{prefixCls}-collapsible-header {
cursor: default;
.@{prefixCls}-header-text {
.@{prefixCls}-title {
cursor: pointer;
}
.@{prefixCls}-expand-icon {
Expand Down
10 changes: 6 additions & 4 deletions src/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
typeof expandIcon === 'function' ? expandIcon(props) : <i className="arrow" />;
const iconNode = iconNodeInner && (
<div
className={`${prefixCls}-expand-icon`}
className={classNames(`${prefixCls}-expand-icon`, customizeClassNames?.icon)}
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
style={styles?.icon}
{...(['header', 'icon'].includes(collapsible) ? collapsibleProps : {})}
>
{iconNodeInner}
Expand All @@ -74,13 +75,13 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
{
[`${prefixCls}-collapsible-${collapsible}`]: !!collapsible,
},
customizeClassNames.header,
customizeClassNames?.header,
);

// ======================== HeaderProps ========================
const headerProps: React.HTMLAttributes<HTMLDivElement> = {
className: headerClassName,
style: styles.header,
style: styles?.header,
...(['header', 'icon'].includes(collapsible) ? {} : collapsibleProps),
};

Expand All @@ -90,7 +91,8 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
<div {...headerProps}>
{showArrow && iconNode}
<span
className={`${prefixCls}-header-text`}
className={classNames(`${prefixCls}-title`, customizeClassNames?.title)}
style={styles?.title}
{...(collapsible === 'header' ? collapsibleProps : {})}
>
{header}
Expand Down
5 changes: 3 additions & 2 deletions src/PanelContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ const PanelContent = React.forwardRef<
ref={ref}
className={classnames(
`${prefixCls}-content`,
customizeClassNames?.content,
Copy link
Member

Choose a reason for hiding this comment

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

content 总是在 body 下面,这里可以不要加 content 的语义化。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

那我的想法是,修正他的 dom 结构,这边变成 body,下面 content-box 变成 content, content-box的语义太差了。而且其他组件基本上都是body content 对应的。 就在大版本中纠正它吧。

{
[`${prefixCls}-content-active`]: isActive,
[`${prefixCls}-content-inactive`]: !isActive,
},
className,
)}
style={style}
style={{ ...style, ...styles?.content }}
role={role}
>
<div
className={classnames(`${prefixCls}-content-box`, customizeClassNames?.body)}
className={classnames(`${prefixCls}-body`, customizeClassNames?.body)}
style={styles?.body}
>
{children}
Expand Down
5 changes: 3 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ export interface CollapseProps {
items?: ItemType[];
}

export type SemanticName = 'header' | 'title' | 'body' | 'content' | 'icon';
export interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement> {
id?: string;
header?: string | React.ReactNode;
prefixCls?: string;
headerClass?: string;
showArrow?: boolean;
className?: string;
classNames?: { header?: string; body?: string };
classNames?: Partial<Record<SemanticName, string>>;
style?: object;
styles?: { header?: React.CSSProperties; body?: React.CSSProperties };
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
isActive?: boolean;
openMotion?: CSSMotionProps;
destroyInactivePanel?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions tests/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports[`collapse props items should work with nested 1`] = `
/>
</div>
<span
class="rc-collapse-header-text"
class="rc-collapse-title"
>
collapse 1
</span>
Expand All @@ -46,7 +46,7 @@ exports[`collapse props items should work with nested 1`] = `
/>
</div>
<span
class="rc-collapse-header-text"
class="rc-collapse-title"
>
collapse 2
</span>
Expand Down Expand Up @@ -77,7 +77,7 @@ exports[`collapse props items should work with nested 1`] = `
/>
</div>
<span
class="rc-collapse-header-text"
class="rc-collapse-title"
>
collapse 3
</span>
Expand All @@ -101,7 +101,7 @@ exports[`collapse props items should work with nested 1`] = `
/>
</div>
<span
class="rc-collapse-header-text"
class="rc-collapse-title"
>
title 3
</span>
Expand Down
58 changes: 43 additions & 15 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('collapse', () => {
fireEvent.click(header);
jest.runAllTimers();
expect(collapse.container.querySelector('.rc-collapse-content-inactive')?.innerHTML).toBe(
'<div class="rc-collapse-content-box">second</div>',
'<div class="rc-collapse-body">second</div>',
);
expect(collapse.container.querySelectorAll('.rc-collapse-content-active').length).toBeFalsy();
});
Expand Down Expand Up @@ -528,7 +528,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(container.querySelector('.rc-collapse-header-text')).toBeTruthy();
expect(container.querySelector('.rc-collapse-title')).toBeTruthy();
fireEvent.click(container.querySelector('.rc-collapse-header')!);
expect(container.querySelectorAll('.rc-collapse-item-active')).toHaveLength(1);
});
Expand All @@ -540,10 +540,10 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(container.querySelector('.rc-collapse-header-text')).toBeTruthy();
expect(container.querySelector('.rc-collapse-title')).toBeTruthy();
fireEvent.click(container.querySelector('.rc-collapse-header')!);
expect(container.querySelectorAll('.rc-collapse-item-active')).toHaveLength(0);
fireEvent.click(container.querySelector('.rc-collapse-header-text')!);
fireEvent.click(container.querySelector('.rc-collapse-title')!);
expect(container.querySelectorAll('.rc-collapse-item-active')).toHaveLength(1);
});
it('should work when value is icon', () => {
Expand All @@ -569,7 +569,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(container.querySelector('.rc-collapse-header-text')).toBeTruthy();
expect(container.querySelector('.rc-collapse-title')).toBeTruthy();
expect(container.querySelectorAll('.rc-collapse-item-disabled')).toHaveLength(1);
fireEvent.click(container.querySelector('.rc-collapse-header')!);
expect(container.querySelectorAll('.rc-collapse-item-active')).toHaveLength(0);
Expand All @@ -583,7 +583,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(container.querySelector('.rc-collapse-header-text')).toBeTruthy();
expect(container.querySelector('.rc-collapse-title')).toBeTruthy();

expect(container.querySelectorAll('.rc-collapse-item-disabled')).toHaveLength(1);

Expand Down Expand Up @@ -613,7 +613,7 @@ describe('collapse', () => {
</Collapse>,
);

fireEvent.click(container.querySelector('.rc-collapse-header-text')!);
fireEvent.click(container.querySelector('.rc-collapse-title')!);
expect(container.querySelectorAll('.rc-collapse-item-active')).toHaveLength(0);
});
});
Expand Down Expand Up @@ -861,25 +861,53 @@ describe('collapse', () => {
});

it('should support styles and classNames', () => {
const customStyles = {
header: { color: 'red' },
body: { color: 'blue' },
title: { color: 'green' },
icon: { color: 'yellow' },
content: { color: 'purple' },
};
const customClassnames = {
header: 'custom-header',
body: 'custom-body',
content: 'custom-content',
title: 'custom-title',
icon: 'custom-icon',
};

const { container } = render(
<Collapse
activeKey={['1']}
items={[
{
key: '1',
label: 'title',
styles: { header: { color: 'red' }, body: { color: 'blue' } },
classNames: { header: 'header-class', body: 'body-class' },
styles: customStyles,
classNames: customClassnames,
},
]}
/>,
);

expect(container.querySelector('.rc-collapse-header')).toHaveClass('header-class');
expect(container.querySelector('.rc-collapse-content-box')).toHaveClass('body-class');

expect(container.querySelector('.rc-collapse-header')).toHaveStyle({ color: 'red' });
expect(container.querySelector('.rc-collapse-content-box')).toHaveStyle({ color: 'blue' });
const headerElement = container.querySelector('.rc-collapse-header') as HTMLElement;
const bodyElement = container.querySelector('.rc-collapse-body') as HTMLElement;
const contentElement = container.querySelector('.rc-collapse-content') 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(bodyElement.classList).toContain('custom-body');
expect(contentElement.classList).toContain('custom-content');
expect(titleElement.classList).toContain('custom-title');
expect(iconElement.classList).toContain('custom-icon');

// check styles
expect(headerElement.style.color).toBe('red');
expect(bodyElement.style.color).toBe('blue');
expect(contentElement.style.color).toBe('purple');
expect(titleElement.style.color).toBe('green');
expect(iconElement.style.color).toBe('yellow');
});
});
});
Loading