chore: add warning of tip with spin (#42293)

This commit is contained in:
二货爱吃白萝卜 2023-05-12 09:51:44 +08:00 committed by GitHub
parent 4207b71aa8
commit 31852e1b65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -1,9 +1,9 @@
import React from 'react';
import { render } from '@testing-library/react';
import { waitFakeTimer } from '../../../tests/utils';
import React from 'react';
import Spin from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { waitFakeTimer } from '../../../tests/utils';
describe('Spin', () => {
mountTest(Spin);
@ -52,4 +52,15 @@ describe('Spin', () => {
const { container } = render(<Spin>{0}</Spin>);
expect(container.querySelector('.ant-spin-container')?.textContent).toBe('0');
});
it('warning tip without nest', () => {
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = render(<Spin tip="Not Show" />);
expect(container.querySelector('.ant-spin-text')).toBeFalsy();
expect(errSpy).toHaveBeenCalledWith('Warning: [antd: Spin] `tip` only work in nest pattern.');
errSpy.mockRestore();
});
});

View File

@ -1,10 +1,11 @@
import classNames from 'classnames';
import { debounce } from 'throttle-debounce';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import { debounce } from 'throttle-debounce';
import { cloneElement, isValidElement } from '../_util/reactNode';
import warning from '../_util/warning';
import type { ConfigConsumerProps } from '../config-provider';
import { ConfigContext } from '../config-provider';
import { cloneElement, isValidElement } from '../_util/reactNode';
import useStyle from './style/index';
const SpinSizes = ['small', 'default', 'large'] as const;
@ -108,6 +109,10 @@ const Spin: React.FC<SpinClassProps> = (props) => {
const isNestedPattern = React.useMemo<boolean>(() => typeof children !== 'undefined', [children]);
if (process.env.NODE_ENV !== 'production') {
warning(!tip || isNestedPattern, 'Spin', '`tip` only work in nest pattern.');
}
const { direction } = React.useContext<ConfigConsumerProps>(ConfigContext);
const spinClassName = classNames(
@ -140,7 +145,7 @@ const Spin: React.FC<SpinClassProps> = (props) => {
aria-busy={spinning}
>
{renderIndicator(prefixCls, props)}
{tip ? <div className={`${prefixCls}-text`}>{tip}</div> : null}
{tip && isNestedPattern ? <div className={`${prefixCls}-text`}>{tip}</div> : null}
</div>
);