mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 11:10:01 +08:00
fix(Button): loading icon appearing when rendering (#45030)
This commit is contained in:
parent
e85054879e
commit
ef7c500a8e
@ -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}
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user