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

fix(Collapse): solve deprecated warning display unexpected in collapse issue #42876

Merged
merged 3 commits into from
Jun 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 16 additions & 14 deletions components/collapse/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,23 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
leavedClassName: `${prefixCls}-content-hidden`,
};

const items = React.useMemo<React.ReactNode[]>(
const items = React.useMemo<React.ReactNode[] | null>(
() =>
toArray(children).map<React.ReactNode>((child, index) => {
if (child.props?.disabled) {
const key = child.key ?? String(index);
const { disabled, collapsible } = child.props;
const childProps: Omit<CollapseProps, 'items'> & { key: React.Key } = {
...omit(child.props, ['disabled']),
key,
collapsible: collapsible ?? (disabled ? 'disabled' : undefined),
};
return cloneElement(child, childProps);
}
return child;
}),
children
? toArray(children).map<React.ReactNode>((child, index) => {
if (child.props?.disabled) {
const key = child.key ?? String(index);
const { disabled, collapsible } = child.props;
const childProps: Omit<CollapseProps, 'items'> & { key: React.Key } = {
...omit(child.props, ['disabled']),
key,
collapsible: collapsible ?? (disabled ? 'disabled' : undefined),
};
return cloneElement(child, childProps);
}
return child;
})
: null,
[children],
);

Expand Down
30 changes: 29 additions & 1 deletion components/collapse/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { waitFakeTimer, render, fireEvent } from '../../../tests/utils';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning';

describe('Collapse', () => {
Expand Down Expand Up @@ -124,6 +124,34 @@ describe('Collapse', () => {
expect(container.querySelectorAll('.ant-collapse-item-active').length).toBe(0);
});

it('should not trigger warning when using items instead of children', () => {
render(
<Collapse
items={[
{
key: '1',
label: 'This is panel header 1',
children: <p>aaa</p>,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>bbb</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>ccc</p>,
},
]}
/>,
);

expect(errorSpy).not.toHaveBeenCalledWith(
'Warning: `children` will be removed in next major version. Please use `items` instead.',
);
});

it('should end motion when set activeKey while hiding', async () => {
jest.useFakeTimers();
const spiedRAF = jest
Expand Down