Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions src/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import React from 'react';
import type { CollapseProps, CollapsibleType } from './interface';
import type { CollapsePanelProps, CollapseProps, CollapsibleType } from './interface';
import CollapsePanel from './Panel';

function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
Expand Down Expand Up @@ -56,7 +56,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
});

// ======================== Children ========================
const getNewChild = (child: React.ReactElement, index: number) => {
const getNewChild = (child: React.ReactElement<CollapsePanelProps>, index: number) => {
if (!child) return null;

const key = child.key || String(index);
Expand All @@ -66,6 +66,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
headerClass,
destroyInactivePanel: childDestroyInactivePanel,
collapsible: childCollapsible,
onItemClick: childOnItemClick,
} = child.props;

let isActive = false;
Expand All @@ -77,6 +78,12 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>

const mergeCollapsible: CollapsibleType = childCollapsible ?? collapsible;

const handleItemClick = (value: React.Key) => {
if (mergeCollapsible === 'disabled') return;
onClickItem(value);
childOnItemClick?.(value);
};

const childProps = {
key,
panelKey: key,
Expand All @@ -88,7 +95,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
openMotion,
accordion,
children: child.props.children,
onItemClick: mergeCollapsible === 'disabled' ? null : onClickItem,
onItemClick: handleItemClick,
expandIcon,
collapsible: mergeCollapsible,
};
Expand Down
27 changes: 27 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,31 @@ describe('collapse', () => {
expect(ref.current).toBe(container.firstChild);
expect(panelRef.current).toBe(container.querySelector('.rc-collapse-item'));
});

// https://github.com/react-component/collapse/issues/235
it('onItemClick should work', () => {
const onItemClick = jest.fn();
const { container } = render(
<Collapse>
<Panel header="collapse 1" key="1" onItemClick={onItemClick}>
first
</Panel>
</Collapse>,
);
fireEvent.click(container.querySelector('.rc-collapse-header')!);
expect(onItemClick).toHaveBeenCalled();
});

it('onItemClick should not work when collapsible is disabled', () => {
const onItemClick = jest.fn();
const { container } = render(
<Collapse collapsible="disabled">
<Panel header="collapse 1" key="1" onItemClick={onItemClick}>
first
</Panel>
</Collapse>,
);
fireEvent.click(container.querySelector('.rc-collapse-header')!);
expect(onItemClick).not.toHaveBeenCalled();
});
});