Skip to content

Commit 11d700c

Browse files
committed
feat: update badge
1 parent 469702d commit 11d700c

File tree

3 files changed

+29
-67
lines changed

3 files changed

+29
-67
lines changed

components/badge/Badge.jsx

+17-24
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ import PropTypes from '../_util/vue-types';
22
import ScrollNumber from './ScrollNumber';
33
import { PresetColorTypes } from '../_util/colors';
44
import classNames from 'classnames';
5-
import {
6-
initDefaultProps,
7-
filterEmpty,
8-
getComponentFromProp,
9-
getListeners,
10-
} from '../_util/props-util';
5+
import { initDefaultProps, getComponent, getSlot } from '../_util/props-util';
116
import { cloneElement } from '../_util/vnode';
127
import getTransitionProps from '../_util/getTransitionProps';
138
import isNumeric from '../_util/isNumeric';
149
import { ConfigConsumerProps } from '../config-provider';
15-
import { inject } from 'vue';
10+
import { inject, Transition } from 'vue';
1611

1712
const BadgeProps = {
1813
/** Number to show in badge */
@@ -82,8 +77,7 @@ export default {
8277
}
8378
: { ...numberStyle };
8479
},
85-
getBadgeClassName(prefixCls) {
86-
const children = filterEmpty(this.$slots.default && this.$slots.default());
80+
getBadgeClassName(prefixCls, children) {
8781
const hasStatus = this.hasStatus();
8882
return classNames(prefixCls, {
8983
[`${prefixCls}-status`]: hasStatus,
@@ -127,9 +121,13 @@ export default {
127121
if (!customNode || typeof customNode !== 'object') {
128122
return undefined;
129123
}
130-
return cloneElement(customNode, {
131-
style: this.getStyleWithOffset(),
132-
});
124+
return cloneElement(
125+
customNode,
126+
{
127+
style: this.getStyleWithOffset(),
128+
},
129+
false,
130+
);
133131
},
134132

135133
renderBadgeNumber(prefixCls, scrollNumberPrefixCls) {
@@ -158,8 +156,8 @@ export default {
158156
<ScrollNumber
159157
prefixCls={scrollNumberPrefixCls}
160158
data-show={!hidden}
161-
v-show={!hidden}
162-
className={scrollNumberCls}
159+
vShow={!hidden}
160+
class={scrollNumberCls}
163161
count={displayCount}
164162
displayComponent={this.renderDispayComponent()}
165163
title={this.getScrollNumberTitle()}
@@ -177,15 +175,14 @@ export default {
177175
status,
178176
text,
179177
color,
180-
$slots,
181178
} = this;
182179

183180
const getPrefixCls = this.configProvider.getPrefixCls;
184181
const prefixCls = getPrefixCls('badge', customizePrefixCls);
185182
const scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls);
186183

187-
const children = filterEmpty($slots.default && $slots.default());
188-
let count = getComponentFromProp(this, 'count');
184+
const children = getSlot(this);
185+
let count = getComponent(this, 'count');
189186
if (Array.isArray(count)) {
190187
count = count[0];
191188
}
@@ -206,11 +203,7 @@ export default {
206203
const styleWithOffset = this.getStyleWithOffset();
207204
const statusTextColor = styleWithOffset && styleWithOffset.color;
208205
return (
209-
<span
210-
{...{ on: getListeners(this) }}
211-
class={this.getBadgeClassName(prefixCls)}
212-
style={styleWithOffset}
213-
>
206+
<span class={this.getBadgeClassName(prefixCls, children)} style={styleWithOffset}>
214207
<span class={statusCls} style={statusStyle} />
215208
<span style={{ color: statusTextColor }} class={`${prefixCls}-status-text`}>
216209
{text}
@@ -222,9 +215,9 @@ export default {
222215
const transitionProps = getTransitionProps(children.length ? `${prefixCls}-zoom` : '');
223216

224217
return (
225-
<span {...{ on: getListeners(this) }} class={this.getBadgeClassName(prefixCls)}>
218+
<span class={this.getBadgeClassName(prefixCls, children)}>
226219
{children}
227-
<transition {...transitionProps}>{scrollNumber}</transition>
220+
<Transition {...transitionProps}>{scrollNumber}</Transition>
228221
{statusText}
229222
</span>
230223
);

components/badge/ScrollNumber.jsx

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import classNames from 'classnames';
22
import PropTypes from '../_util/vue-types';
33
import BaseMixin from '../_util/BaseMixin';
4-
import { getStyle } from '../_util/props-util';
54
import omit from 'omit.js';
65
import { cloneElement } from '../_util/vnode';
76
import { ConfigConsumerProps } from '../config-provider';
87
import { inject } from 'vue';
8+
import syncWatch from '../_util/syncWatch';
99

1010
function getNumberArray(num) {
1111
return num
@@ -26,30 +26,32 @@ const ScrollNumberProps = {
2626
component: PropTypes.string,
2727
title: PropTypes.oneOfType([PropTypes.number, PropTypes.string, null]),
2828
displayComponent: PropTypes.any,
29-
className: PropTypes.object,
3029
};
3130

3231
export default {
32+
name: 'ScrollNumber',
3333
mixins: [BaseMixin],
34+
inheritAttrs: false,
3435
props: ScrollNumberProps,
3536
setup() {
3637
return {
3738
configProvider: inject('configProvider', ConfigConsumerProps),
3839
};
3940
},
4041
data() {
42+
this.lastCount = undefined;
4143
return {
4244
animateStarted: true,
4345
sCount: this.count,
4446
};
4547
},
4648
watch: {
47-
count() {
49+
count: syncWatch(function() {
4850
this.lastCount = this.sCount;
4951
this.setState({
5052
animateStarted: true,
5153
});
52-
},
54+
}),
5355
},
5456
updated() {
5557
const { animateStarted, count } = this;
@@ -157,31 +159,22 @@ export default {
157159
},
158160

159161
render() {
160-
const {
161-
prefixCls: customizePrefixCls,
162-
title,
163-
component: Tag = 'sup',
164-
displayComponent,
165-
className,
166-
} = this;
162+
const { prefixCls: customizePrefixCls, title, component: Tag = 'sup', displayComponent } = this;
167163
const getPrefixCls = this.configProvider.getPrefixCls;
168164
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
165+
const { class: className, style = {} } = this.$attrs;
169166
if (displayComponent) {
170167
return cloneElement(displayComponent, {
171168
class: `${prefixCls}-custom-component`,
172169
});
173170
}
174-
const style = getStyle(this, true);
175171
// fix https://fb.me/react-unknown-prop
176172
const restProps = omit(this.$props, ['count', 'component', 'prefixCls', 'displayComponent']);
173+
const tempStyle = { ...style };
177174
const newProps = {
178-
props: {
179-
...restProps,
180-
},
181-
attrs: {
182-
title,
183-
},
184-
style,
175+
...restProps,
176+
title,
177+
style: tempStyle,
185178
class: classNames(prefixCls, className),
186179
};
187180
// allow specify the border

components/badge/index.en_US.md

-24
This file was deleted.

0 commit comments

Comments
 (0)