Skip to content

Commit

Permalink
🐛 fix Badge color not working when contains children (#21333)
Browse files Browse the repository at this point in the history
close #21331
  • Loading branch information
afc163 committed Feb 11, 2020
1 parent f692e7e commit dd16a24
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
36 changes: 36 additions & 0 deletions components/badge/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,42 @@ exports[`Badge badge should support float number 2`] = `
</Badge>
`;

exports[`Badge render Badge status/color when contains children 1`] = `
Array [
<span
class="ant-badge ant-badge-status"
>
<a />
<sup
class="ant-scroll-number ant-badge-dot ant-badge-status-success"
data-show="true"
title="5"
/>
</span>,
<span
class="ant-badge ant-badge-status"
>
<a />
<sup
class="ant-scroll-number ant-badge-dot ant-badge-status-blue"
data-show="true"
title="5"
/>
</span>,
<span
class="ant-badge ant-badge-status"
>
<a />
<sup
class="ant-scroll-number ant-badge-dot"
data-show="true"
style="background:#08c"
title="5"
/>
</span>,
]
`;

exports[`Badge render correct with negative number 1`] = `
<div>
<span
Expand Down
18 changes: 18 additions & 0 deletions components/badge/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,22 @@ describe('Badge', () => {
);
expect(wrapper).toMatchSnapshot();
});

// https://github.com/ant-design/ant-design/issues/21331
it('render Badge status/color when contains children', () => {
const wrapper = render(
<>
<Badge count={5} status="success">
<a />
</Badge>
<Badge count={5} color="blue">
<a />
</Badge>
<Badge count={5} color="#08c">
<a />
</Badge>
</>,
);
expect(wrapper).toMatchSnapshot();
})
});
13 changes: 10 additions & 3 deletions components/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class Badge extends React.Component<BadgeProps, any> {
}

renderBadgeNumber(prefixCls: string, scrollNumberPrefixCls: string) {
const { status, count } = this.props;
const { status, count, color } = this.props;

const displayCount = this.getDispayCount();
const isDot = this.isDot();
Expand All @@ -147,9 +147,16 @@ export default class Badge extends React.Component<BadgeProps, any> {
[`${prefixCls}-count`]: !isDot,
[`${prefixCls}-multiple-words`]:
!isDot && count && count.toString && count.toString().length > 1,
[`${prefixCls}-status-${status}`]: this.hasStatus(),
[`${prefixCls}-status-${status}`]: !!status,
[`${prefixCls}-status-${color}`]: isPresetColor(color),
});

let statusStyle: React.CSSProperties | undefined = this.getStyleWithOffset();
if (color && !isPresetColor(color)) {
statusStyle = statusStyle || {};
statusStyle.background = color;
}

return hidden ? null : (
<ScrollNumber
prefixCls={scrollNumberPrefixCls}
Expand All @@ -158,7 +165,7 @@ export default class Badge extends React.Component<BadgeProps, any> {
count={displayCount}
displayComponent={this.renderDispayComponent()} // <Badge status="success" count={<Icon type="xxx" />}></Badge>
title={this.getScrollNumberTitle()}
style={this.getStyleWithOffset()}
style={statusStyle}
key="scrollNumber"
/>
);
Expand Down

0 comments on commit dd16a24

Please sign in to comment.