fix(Button): loading icon appearing when rendering (#45030)

This commit is contained in:
Zhaolin Liang 2023-09-26 11:57:03 +08:00 committed by GitHub
parent e85054879e
commit ef7c500a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -56,6 +56,7 @@ const LoadingIcon: React.FC<LoadingIconProps> = (props) => {
visible={visible}
// We do not really use this motionName
motionName={`${prefixCls}-loading-icon-motion`}
motionLeave={visible}
removeOnLeave
onAppearStart={getCollapsedWidth}
onAppearActive={getRealWidth}

View File

@ -1,6 +1,6 @@
import { SearchOutlined } from '@ant-design/icons';
import { resetWarned } from 'rc-util/lib/warning';
import React, { useState } from 'react';
import React, { Suspense, useRef, useState } from 'react';
import { act } from 'react-dom/test-utils';
import Button from '..';
import mountTest from '../../../tests/shared/mountTest';
@ -388,4 +388,44 @@ describe('Button', () => {
expect(refHtml).toBeTruthy();
expect(btnAttr).toBeTruthy();
});
it('should not display loading when not set', () => {
function Suspender({ freeze }: { freeze: boolean }) {
const promiseCache = useRef<{
promise?: Promise<void>;
resolve?: (value: void | PromiseLike<void>) => void;
}>({}).current;
if (freeze && !promiseCache.promise) {
promiseCache.promise = new Promise((resolve) => {
promiseCache.resolve = resolve;
});
throw promiseCache.promise;
} else if (freeze) {
throw promiseCache.promise;
} else if (promiseCache.promise) {
promiseCache.resolve?.();
promiseCache.promise = undefined;
}
return <Button>button</Button>;
}
const MyCom: React.FC = () => {
const [freeze, setFreeze] = useState(false);
return (
<div className="foo">
<Button className="change-btn" onClick={() => setFreeze(!freeze)}>
Change
</Button>
<Suspense fallback={<>frozen</>}>
<Suspender freeze={freeze} />
</Suspense>
</div>
);
};
const { container } = render(<MyCom />);
fireEvent.click(container.querySelector('.change-btn')!);
expect(container.querySelector('.foo')).toHaveTextContent('frozen');
fireEvent.click(container.querySelector('.change-btn')!);
expect(container.querySelectorAll('.ant-btn-loading-icon').length).toBe(0);
});
});