From ef36e17bb60d0d11bcb92320a570e8cf388d6716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=8F=E5=8F=B3?= Date: Tue, 11 Feb 2020 12:56:18 +0800 Subject: [PATCH] :bug: fix Badge color not working when contains children (#21333) close #21331 --- .../__snapshots__/index.test.js.snap | 36 +++++++++++++++++++ components/badge/__tests__/index.test.js | 18 ++++++++++ components/badge/index.tsx | 13 +++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/components/badge/__tests__/__snapshots__/index.test.js.snap b/components/badge/__tests__/__snapshots__/index.test.js.snap index a58afd99ee..0857e9ce9c 100644 --- a/components/badge/__tests__/__snapshots__/index.test.js.snap +++ b/components/badge/__tests__/__snapshots__/index.test.js.snap @@ -69,6 +69,42 @@ exports[`Badge badge should support float number 2`] = ` `; +exports[`Badge render Badge status/color when contains children 1`] = ` +Array [ + + + + , + + + + , + + + + , +] +`; + exports[`Badge render correct with negative number 1`] = `
{ ); expect(wrapper).toMatchSnapshot(); }); + + // https://github.com/ant-design/ant-design/issues/21331 + it('render Badge status/color when contains children', () => { + const wrapper = render( + <> + + + + + + + + + + , + ); + expect(wrapper).toMatchSnapshot(); + }) }); diff --git a/components/badge/index.tsx b/components/badge/index.tsx index 728380b5d5..fe7e68b9de 100644 --- a/components/badge/index.tsx +++ b/components/badge/index.tsx @@ -129,7 +129,7 @@ export default class Badge extends React.Component { } renderBadgeNumber(prefixCls: string, scrollNumberPrefixCls: string) { - const { status, count } = this.props; + const { status, count, color } = this.props; const displayCount = this.getDisplayCount(); const isDot = this.isDot(); @@ -140,9 +140,16 @@ export default class Badge extends React.Component { [`${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 : ( { count={displayCount} displayComponent={this.renderDisplayComponent()} // }> title={this.getScrollNumberTitle()} - style={this.getStyleWithOffset()} + style={statusStyle} key="scrollNumber" /> );