Skip to content

Commit 305b30a

Browse files
authored
refactor: use renderable guards for panel content (#455)
* refactor: use renderable guards * fix: preserve extra value semantics
1 parent dcd0f78 commit 305b30a

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"dependencies": {
4949
"@babel/runtime": "^8.0.0",
5050
"@rc-component/motion": "^1.1.4",
51-
"@rc-component/util": "^1.11.1",
51+
"@rc-component/util": "^1.13.0",
5252
"clsx": "^2.1.1"
5353
},
5454
"devDependencies": {

‎src/Panel.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { clsx } from 'clsx';
22
import CSSMotion from '@rc-component/motion';
3-
import { KeyCode } from '@rc-component/util';
3+
import { isNonNullable, isReactRenderable, KeyCode } from '@rc-component/util';
44
import React from 'react';
55
import type { CollapsePanelProps } from './interface';
66
import PanelContent from './PanelContent';
@@ -30,7 +30,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
3030

3131
const disabled = collapsible === 'disabled';
3232

33-
const ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean';
33+
const ifExtraExist = isNonNullable(extra) && typeof extra !== 'boolean';
3434

3535
const collapsibleProps = {
3636
onClick: () => {
@@ -50,7 +50,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
5050
// ======================== Icon ========================
5151
const iconNodeInner =
5252
typeof expandIcon === 'function' ? expandIcon(props) : <i className="arrow" />;
53-
const iconNode = iconNodeInner && (
53+
const iconNode = isReactRenderable(iconNodeInner) && (
5454
<div
5555
className={clsx(`${prefixCls}-expand-icon`, customizeClassNames?.icon)}
5656
style={styles?.icon}

‎tests/index.spec.tsx‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,25 @@ describe('collapse', () => {
205205
});
206206
});
207207

208-
it('should support extra whit number 0', () => {
208+
it.each([
209+
{ name: 'number zero', extra: 0, expected: true, content: '0' },
210+
{ name: 'empty string', extra: '', expected: true, content: '' },
211+
{ name: 'true', extra: true, expected: false },
212+
{ name: 'false', extra: false, expected: false },
213+
])('should preserve extra semantics for $name', ({ extra, expected, content }) => {
209214
const { container } = render(
210215
<Collapse onChange={onChange} activeKey={0}>
211-
<Panel header="collapse 0" key={0} extra={0}>
216+
<Panel header="collapse 0" key={0} extra={extra}>
212217
zero
213218
</Panel>
214219
</Collapse>,
215220
);
216221

217222
const extraNodes = container.querySelectorAll('.rc-collapse-extra');
218-
expect(extraNodes).toHaveLength(1);
219-
expect(extraNodes[0].innerHTML).toBe('0');
223+
expect(extraNodes).toHaveLength(expected ? 1 : 0);
224+
if (expected) {
225+
expect(extraNodes[0].innerHTML).toBe(content);
226+
}
220227
});
221228

222229
it('should support activeKey number 0', () => {

0 commit comments

Comments
 (0)