Skip to content

Commit

Permalink
fix: Compatible with v4 logic (ant-design#47612)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored and tanzhenyun committed Mar 29, 2024
1 parent bd8c26f commit dd8df41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 8 additions & 1 deletion components/button/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { Suspense, useRef, useState } from 'react';
import { SearchOutlined } from '@ant-design/icons';
import { resetWarned } from 'rc-util/lib/warning';
import React, { Suspense, useRef, useState } from 'react';
import { act } from 'react-dom/test-utils';

import Button from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
Expand Down Expand Up @@ -434,4 +435,10 @@ describe('Button', () => {
const { container } = render(<Button loading={{ delay: 0 }}>Button</Button>);
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(1);
});

// https://github.com/ant-design/ant-design/issues/47605
it('Compatible with original `type` behavior', async () => {
const { container } = render(<Button type={'' as any} />);
expect(container.querySelector('.ant-btn-default')).toBeTruthy();
});
});
17 changes: 11 additions & 6 deletions components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const InternalButton: React.ForwardRefRenderFunction<
const {
loading = false,
prefixCls: customizePrefixCls,
type = 'default',
type,
danger,
shape = 'default',
size: customizeSize,
Expand All @@ -115,6 +115,10 @@ const InternalButton: React.ForwardRefRenderFunction<
...rest
} = props;

// https://github.com/ant-design/ant-design/issues/47605
// Compatible with original `type` behavior
const mergedType = type || 'default';

const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext);
const prefixCls = getPrefixCls('btn', customizePrefixCls);

Expand All @@ -135,7 +139,8 @@ const InternalButton: React.ForwardRefRenderFunction<

const buttonRef = composeRef(ref, internalRef);

const needInserted = Children.count(children) === 1 && !icon && !isUnBorderedButtonType(type);
const needInserted =
Children.count(children) === 1 && !icon && !isUnBorderedButtonType(mergedType);

useEffect(() => {
let delayTimer: ReturnType<typeof setTimeout> | null = null;
Expand Down Expand Up @@ -193,7 +198,7 @@ const InternalButton: React.ForwardRefRenderFunction<
);

warning(
!(ghost && isUnBorderedButtonType(type)),
!(ghost && isUnBorderedButtonType(mergedType)),
'usage',
"`link` or `text` button can't be a `ghost` button.",
);
Expand All @@ -218,10 +223,10 @@ const InternalButton: React.ForwardRefRenderFunction<
cssVarCls,
{
[`${prefixCls}-${shape}`]: shape !== 'default' && shape,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${mergedType}`]: mergedType,
[`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-icon-only`]: !children && children !== 0 && !!iconType,
[`${prefixCls}-background-ghost`]: ghost && !isUnBorderedButtonType(type),
[`${prefixCls}-background-ghost`]: ghost && !isUnBorderedButtonType(mergedType),
[`${prefixCls}-loading`]: innerLoading,
[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace && !innerLoading,
[`${prefixCls}-block`]: block,
Expand Down Expand Up @@ -291,7 +296,7 @@ const InternalButton: React.ForwardRefRenderFunction<
</button>
);

if (!isUnBorderedButtonType(type)) {
if (!isUnBorderedButtonType(mergedType)) {
buttonNode = (
<Wave component="Button" disabled={!!innerLoading}>
{buttonNode}
Expand Down

0 comments on commit dd8df41

Please sign in to comment.