fix: Button with disabled and type="link" should not clickable (#30209)

close #30207
This commit is contained in:
afc163 2021-04-19 17:19:22 +08:00 committed by GitHub
parent ca31a75d1b
commit edb59bd2e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -300,4 +300,15 @@ describe('Button', () => {
},
});
});
it('should not redirect when button is disabled', () => {
const onClick = jest.fn();
const wrapper = mount(
<Button href="https://ant.design" onClick={onClick} disabled>
click me
</Button>,
);
wrapper.simulate('click');
expect(onClick).not.toHaveBeenCalled();
});
});

View File

@ -193,8 +193,10 @@ const InternalButton: React.ForwardRefRenderFunction<unknown, ButtonProps> = (pr
React.useEffect(fixTwoCNChar, [buttonRef]);
const handleClick = (e: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement, MouseEvent>) => {
const { onClick } = props;
if (innerLoading) {
const { onClick, disabled } = props;
// https://github.com/ant-design/ant-design/issues/30207
if (innerLoading || disabled) {
e.preventDefault();
return;
}
(onClick as React.MouseEventHandler<HTMLButtonElement | HTMLAnchorElement>)?.(e);