Skip to content

Commit f9c344e

Browse files
committed
feat: 增加extra
1 parent 5de5100 commit f9c344e

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/Dialog/Content/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
3737

3838
const panelRef = useRef<PanelRef>(null);
3939

40-
const hasExtra =
41-
extra !== null &&
42-
extra !== undefined &&
43-
typeof extra !== 'boolean' &&
44-
!(typeof extra === 'string' && extra.trim() === '');
40+
const hasExtra = !!extra && !(typeof extra === 'string' && extra.trim() === '');
41+
42+
const curExtra = hasExtra ? extra : null;
4543

4644
// ============================== Refs ==============================
4745
React.useImperativeHandle(ref, () => ({
@@ -84,7 +82,7 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
8482
<Panel
8583
{...props}
8684
ref={panelRef}
87-
extra={hasExtra ? extra : null}
85+
extra={curExtra}
8886
title={title}
8987
ariaId={ariaId}
9088
prefixCls={prefixCls}

tests/index.spec.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
2-
import { fireEvent, render, act } from '@testing-library/react';
2+
import { fireEvent, render, act, screen } from '@testing-library/react';
33
import { Provider } from '@rc-component/motion';
44
import KeyCode from '@rc-component/util/lib/KeyCode';
55
import React, { cloneElement, useEffect } from 'react';
@@ -733,4 +733,40 @@ describe('dialog', () => {
733733
expect(document.querySelector('.rc-dialog')).toBeTruthy();
734734
expect(document.querySelector('.rc-dialog-close')).toBeFalsy();
735735
});
736+
737+
it('should render extra when extra is a React node', () => {
738+
render(<Dialog visible extra={<span data-testid="extra-node">Node</span>} />);
739+
740+
expect(screen.getByTestId('extra-node')).toBeInTheDocument();
741+
});
742+
743+
it('does not render extra when extra is empty string', () => {
744+
render(<Dialog visible extra="" />);
745+
expect(screen.queryByTestId('.rc-dialog-extra')).toBeNull();
746+
});
747+
748+
it('does not render extra when extra is string with only spaces', () => {
749+
render(<Dialog visible extra=" " />);
750+
expect(screen.queryByText(' ')).toBeNull();
751+
});
752+
753+
it('renders extra when extra is non-empty string', () => {
754+
render(<Dialog visible extra="hello" />);
755+
expect(screen.getByText('hello')).toBeInTheDocument();
756+
const extraDiv = document.querySelector('.rc-dialog-extra');
757+
expect(extraDiv).toHaveTextContent('hello');
758+
});
759+
760+
it('does not render extra when extra is null or undefined', () => {
761+
const { container } = render(<Dialog visible extra={null} />);
762+
expect(container.querySelector('.rc-dialog-extra')).toBeNull();
763+
764+
const { container: container2 } = render(<Dialog visible />);
765+
expect(container2.querySelector('.rc-dialog-extra')).toBeNull();
766+
});
767+
768+
it('renders extra when extra is a non-empty string', () => {
769+
render(<Dialog visible extra="Extra Text" />);
770+
expect(screen.getByText('Extra Text')).toBeInTheDocument();
771+
});
736772
});

0 commit comments

Comments
 (0)