forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBadge.tsx
235 lines (209 loc) · 8.16 KB
/
Badge.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import PropTypes from '../_util/vue-types';
import ScrollNumber from './ScrollNumber';
import classNames from '../_util/classNames';
import { getPropsSlot, flattenChildren } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import { getTransitionProps, Transition } from '../_util/transition';
import type { ExtractPropTypes, CSSProperties, PropType } from 'vue';
import { defineComponent, computed, ref, watch } from 'vue';
import Ribbon from './Ribbon';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import isNumeric from '../_util/isNumeric';
import useStyle from './style';
import type { PresetColorKey } from '../theme/interface';
import type { LiteralUnion } from '../_util/type';
import type { PresetStatusColorType } from '../_util/colors';
import { isPresetColor } from '../_util/colors';
export const badgeProps = () => ({
/** Number to show in badge */
count: PropTypes.any.def(null),
showZero: { type: Boolean, default: undefined },
/** Max count to show */
overflowCount: { type: Number, default: 99 },
/** whether to show red dot without number */
dot: { type: Boolean, default: undefined },
prefixCls: String,
scrollNumberPrefixCls: String,
status: { type: String as PropType<PresetStatusColorType> },
size: { type: String as PropType<'default' | 'small'>, default: 'default' },
color: String as PropType<LiteralUnion<PresetColorKey>>,
text: PropTypes.any,
offset: Array as unknown as PropType<[number | string, number | string]>,
numberStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
title: String,
});
export type BadgeProps = Partial<ExtractPropTypes<ReturnType<typeof badgeProps>>>;
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'ABadge',
Ribbon,
inheritAttrs: false,
props: badgeProps(),
slots: ['text', 'count'],
setup(props, { slots, attrs }) {
const { prefixCls, direction } = useConfigInject('badge', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
// ================================ Misc ================================
const numberedDisplayCount = computed(() => {
return (
(props.count as number) > (props.overflowCount as number)
? `${props.overflowCount}+`
: props.count
) as string | number | null;
});
const isZero = computed(
() => numberedDisplayCount.value === '0' || numberedDisplayCount.value === 0,
);
const ignoreCount = computed(() => props.count === null || (isZero.value && !props.showZero));
const hasStatus = computed(
() =>
((props.status !== null && props.status !== undefined) ||
(props.color !== null && props.color !== undefined)) &&
ignoreCount.value,
);
const showAsDot = computed(() => props.dot && !isZero.value);
const mergedCount = computed(() => (showAsDot.value ? '' : numberedDisplayCount.value));
const isHidden = computed(() => {
const isEmpty =
mergedCount.value === null || mergedCount.value === undefined || mergedCount.value === '';
return (isEmpty || (isZero.value && !props.showZero)) && !showAsDot.value;
});
// Count should be cache in case hidden change it
const livingCount = ref(props.count);
// We need cache count since remove motion should not change count display
const displayCount = ref(mergedCount.value);
// We will cache the dot status to avoid shaking on leaved motion
const isDotRef = ref(showAsDot.value);
watch(
[() => props.count, mergedCount, showAsDot],
() => {
if (!isHidden.value) {
livingCount.value = props.count;
displayCount.value = mergedCount.value;
isDotRef.value = showAsDot.value;
}
},
{ immediate: true },
);
// InternalColor
const isInternalColor = computed(() => isPresetColor(props.color, false));
// Shared styles
const statusCls = computed(() => ({
[`${prefixCls.value}-status-dot`]: hasStatus.value,
[`${prefixCls.value}-status-${props.status}`]: !!props.status,
[`${prefixCls.value}-status-${props.color}`]: isInternalColor.value,
}));
const statusStyle = computed(() => {
if (props.color && !isInternalColor.value) {
return { background: props.color, color: props.color };
} else {
return {};
}
});
const scrollNumberCls = computed(() => ({
[`${prefixCls.value}-dot`]: isDotRef.value,
[`${prefixCls.value}-count`]: !isDotRef.value,
[`${prefixCls.value}-count-sm`]: props.size === 'small',
[`${prefixCls.value}-multiple-words`]:
!isDotRef.value && displayCount.value && displayCount.value.toString().length > 1,
[`${prefixCls.value}-status-${props.status}`]: !!props.status,
[`${prefixCls.value}-status-${props.color}`]: isInternalColor.value,
}));
return () => {
const { offset, title, color } = props;
const style = attrs.style as CSSProperties;
const text = getPropsSlot(slots, props, 'text');
const pre = prefixCls.value;
const count = livingCount.value;
let children = flattenChildren(slots.default?.());
children = children.length ? children : null;
const visible = !!(!isHidden.value || slots.count);
// =============================== Styles ===============================
const mergedStyle = (() => {
if (!offset) {
return { ...style };
}
const offsetStyle: CSSProperties = {
marginTop: isNumeric(offset[1]) ? `${offset[1]}px` : offset[1],
};
if (direction.value === 'rtl') {
offsetStyle.left = `${parseInt(offset[0] as string, 10)}px`;
} else {
offsetStyle.right = `${-parseInt(offset[0] as string, 10)}px`;
}
return {
...offsetStyle,
...style,
};
})();
// =============================== Render ===============================
// >>> Title
const titleNode =
title ?? (typeof count === 'string' || typeof count === 'number' ? count : undefined);
// >>> Status Text
const statusTextNode =
visible || !text ? null : <span class={`${pre}-status-text`}>{text}</span>;
// >>> Display Component
const displayNode =
typeof count === 'object' || (count === undefined && slots.count)
? cloneElement(
count ?? slots.count?.(),
{
style: mergedStyle,
},
false,
)
: null;
const badgeClassName = classNames(
pre,
{
[`${pre}-status`]: hasStatus.value,
[`${pre}-not-a-wrapper`]: !children,
[`${pre}-rtl`]: direction.value === 'rtl',
},
attrs.class,
hashId.value,
);
// <Badge status="success" />
if (!children && hasStatus.value) {
const statusTextColor = mergedStyle.color;
return wrapSSR(
<span {...attrs} class={badgeClassName} style={mergedStyle}>
<span class={statusCls.value} style={statusStyle.value} />
<span style={{ color: statusTextColor }} class={`${pre}-status-text`}>
{text}
</span>
</span>,
);
}
const transitionProps = getTransitionProps(children ? `${pre}-zoom` : '', {
appear: false,
});
let scrollNumberStyle: CSSProperties = { ...mergedStyle, ...(props.numberStyle as object) };
if (color && !isInternalColor.value) {
scrollNumberStyle = scrollNumberStyle || {};
scrollNumberStyle.background = color;
}
return wrapSSR(
<span {...attrs} class={badgeClassName}>
{children}
<Transition {...transitionProps}>
<ScrollNumber
v-show={visible}
prefixCls={props.scrollNumberPrefixCls}
show={visible}
class={scrollNumberCls.value}
count={displayCount.value}
title={titleNode}
style={scrollNumberStyle}
key="scrollNumber"
>
{displayNode}
</ScrollNumber>
</Transition>
{statusTextNode}
</span>,
);
};
},
});