Skip to content

Commit 8d41a63

Browse files
authored
feat(badge): v15适配 (#2826)
* fix: update marking * fix: v14适配 * fix: update docs * feat: 0.5px边框 * fix: update types * fix: update test and docs * fix: tabbar test 有问题 * fix: update doc * fix: modify style * fix: 升级文档pr修改 * fix: removing the color property is done using a css variable * fix: update test * fix: update test * fix: update test
1 parent 6894b55 commit 8d41a63

32 files changed

+432
-484
lines changed

migrate-from-v2.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,8 @@ plugins: [
667667

668668
#### Badge
669669

670-
- 移除 `zIndex`,目前没有用到,也不生效,直接去掉。
671-
- 移除 `icon`,自定义 `icon` 可放在 `value` 中实现,扩充了 `value` 的类型。
672-
- 修改 `max` 的最大值为99(之前为10000),比较贴合实际场景。
673-
- 主题定制的 `css` 变量中,去掉和 `dot` 有关的其他值,只保留 `width`。其他值由 `width` 计算而来.
674-
- 主题定制,增加包含 icon 情况下的样式变量。
670+
- 新增 `size` 属性,dot 尺寸,当 dot 等于 `true` 时生效
671+
- 移除 `徽标背景颜色`,通过css变量`--nutui-badge-background-color`实现
675672

676673
#### CircleProgress
677674

src/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@
739739
"sort": 4,
740740
"show": true,
741741
"taro": true,
742+
"v14": true,
742743
"author": "lzz"
743744
},
744745
{
@@ -1274,4 +1275,4 @@
12741275
]
12751276
}
12761277
]
1277-
}
1278+
}

src/packages/badge/__test__/__snapshots__/badge.spec.tsx.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`should match custom icon 1`] = `
77
>
88
<div
99
class="nut-badge-content nut-badge-icon"
10+
style="top: 0px; right: 0px;"
1011
>
1112
<svg
1213
aria-labelledby="Checklist"
@@ -32,8 +33,8 @@ exports[`should match snapshot 1`] = `
3233
class="nut-badge"
3334
>
3435
<div
35-
class="nut-badge-content nut-badge-sup nut-badge-one"
36-
style="top: -4px; right: 8px;"
36+
class="nut-badge-sup nut-badge-number nut-badge-one"
37+
style="top: 0px; right: 0px;"
3738
>
3839
8
3940
</div>

src/packages/badge/__test__/badge.spec.tsx

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ test('should match snapshot', () => {
1212

1313
test('should match value is string', () => {
1414
const { container } = render(<Badge value="new" />)
15-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
15+
const badgeContent = container.querySelectorAll('.nut-badge-sup')[0]
1616
expect(badgeContent).toHaveTextContent('new')
1717
})
1818

1919
test('should match max size', () => {
2020
const { container } = render(<Badge value={200} max={9} />)
21-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
21+
const badgeContent = container.querySelectorAll('.nut-badge-sup')[0]
2222
expect(badgeContent.textContent).toBe('9+')
2323
})
2424

@@ -29,42 +29,18 @@ test('should match dot', () => {
2929
})
3030

3131
test('should match top、right: bad number', () => {
32-
const { container } = render(<Badge value={10} top="--10" right="0" />)
33-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
34-
expect(badgeContent).toHaveStyle({ top: '0px' })
35-
})
36-
37-
test('should match top、right: have px', () => {
38-
const { container } = render(<Badge value={10} top="-10" right="0" />)
39-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
40-
expect(badgeContent).toHaveStyle({ top: '10px', right: '0px' })
32+
const { container } = render(<Badge value={10} top="10" right="0" />)
33+
const badgeContent = container.querySelectorAll('.nut-badge-sup')[0]
34+
expect(badgeContent).toHaveStyle({ top: '10px' })
4135
})
4236

4337
test('should match top、right: float', () => {
44-
const { container } = render(<Badge value={10} top="-10.8" right="0.5px" />)
45-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
38+
const { container } = render(<Badge value={10} top="10.8" right="0.5" />)
39+
const badgeContent = container.querySelectorAll('.nut-badge-sup')[0]
4640
expect(badgeContent).toHaveStyle({ top: '10.8px', right: '0.5px' })
4741
})
4842

49-
test('should match custom color', () => {
50-
const { container } = render(<Badge value={200} color="orange" />)
51-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
52-
expect(badgeContent).toHaveStyle({ 'background-color': 'orange' })
53-
})
54-
5543
test('should match custom icon', () => {
5644
const { asFragment } = render(<Badge value={<Checklist />} />)
5745
expect(asFragment()).toMatchSnapshot()
5846
})
59-
60-
test('should match custom color when fill = outline', () => {
61-
const { container } = render(
62-
<Badge value={200} color="orange" fill="outline" />
63-
)
64-
const badgeContent = container.querySelectorAll('.nut-badge-content')[0]
65-
expect(badgeContent).toHaveStyle({
66-
border: '1px solid orange',
67-
color: 'orange',
68-
background: '#fff',
69-
})
70-
})

src/packages/badge/badge.scss

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22
position: relative;
33
display: inline-flex;
44
width: auto;
5-
/* #ifdef harmony */
6-
min-width: 1px;
7-
/* #endif */
8-
/* #ifndef harmony */
9-
min-width: auto;
10-
/* #endif */
5+
116
&-icon {
12-
position: absolute;
137
display: flex;
148
justify-content: center;
159
align-items: center;
1610
background: $badge-background-color;
1711
padding: $badge-icon-padding;
1812
text-align: center;
19-
border: $badge-border;
20-
border-radius: $badge-border-radius;
2113
z-index: $badge-z-index;
2214

2315
.nut-icon {
@@ -27,53 +19,88 @@
2719
}
2820
}
2921

22+
&-sup,
23+
&-icon {
24+
border-radius: $badge-border-radius;
25+
&::after {
26+
content: '';
27+
position: absolute;
28+
top: -50%;
29+
bottom: -50%;
30+
left: -50%;
31+
right: -50%;
32+
transform: scale(0.5);
33+
border: $badge-border;
34+
border-radius: $badge-border-radius;
35+
}
36+
}
37+
3038
&-sup {
31-
height: $badge-height;
32-
position: absolute;
33-
display: flex;
39+
display: inline-flex;
3440
justify-content: center;
35-
align-items: center;
36-
/* #ifdef harmony */
37-
background: $color-primary;
38-
/* #endif */
39-
/* #ifndef harmony */
40-
background: $badge-background-color;
41-
/* #endif */
42-
color: $badge-color;
41+
height: $badge-height;
42+
min-width: $badge-min-width;
4343
padding: $badge-padding;
44+
box-sizing: border-box;
45+
color: $badge-color;
4446
font-size: $badge-font-size;
45-
font-weight: normal;
46-
text-align: center;
47-
border: $badge-border;
48-
border-radius: $badge-border-radius;
49-
min-width: $badge-min-width;
47+
line-height: 12px;
5048
white-space: nowrap;
49+
font-weight: normal;
50+
vertical-align: middle;
51+
background: $badge-background-color;
5152
z-index: 1;
5253
}
5354

55+
&-number {
56+
font-family: 'JD';
57+
/* #ifdef harmony */
58+
line-height: 12px;
59+
/* #endif */
60+
/* #ifndef harmony */
61+
line-height: 13px;
62+
/* #endif */
63+
}
64+
5465
&-one {
5566
height: $badge-height;
5667
width: $badge-height;
57-
padding: 0;
5868
}
5969

6070
&-content {
61-
/* #ifndef rn */
71+
position: absolute;
6272
transform: $badge-content-transform;
63-
/* #endif */
6473
}
6574

6675
&-dot {
67-
width: $badge-dot-width;
68-
height: $badge-dot-width;
69-
border: $badge-dot-border;
70-
border-radius: $badge-dot-width;
7176
padding: 0;
77+
border-radius: 50%;
78+
&::after {
79+
border: $badge-dot-border;
80+
border-radius: 50%;
81+
}
82+
&-normal {
83+
min-width: $badge-dot-width;
84+
width: $badge-dot-width;
85+
height: $badge-dot-width;
86+
}
87+
&-small {
88+
min-width: $badge-dot-small-width;
89+
width: $badge-dot-small-width;
90+
height: $badge-dot-small-width;
91+
}
92+
&-large {
93+
min-width: $badge-dot-large-width;
94+
width: $badge-dot-large-width;
95+
height: $badge-dot-large-width;
96+
}
7297
}
7398

7499
&-outline {
75100
background: $color-primary-text;
76-
border: 1px solid $color-primary;
77-
color: $color-primary;
101+
color: $badge-outline-color;
102+
&::after {
103+
border: $badge-outline-border;
104+
}
78105
}
79106
}

src/packages/badge/badge.taro.tsx

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
import React, {
22
CSSProperties,
33
FunctionComponent,
4-
ReactNode,
54
useEffect,
65
useRef,
76
useState,
87
} from 'react'
98
import classNames from 'classnames'
109
import { View } from '@tarojs/components'
11-
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
10+
import { ComponentDefaults } from '@/utils/typings'
1211
import { useRtl } from '@/packages/configprovider/index.taro'
1312
import pxTransform from '@/utils/px-transform'
14-
import { getRectByTaro } from '@/utils/get-rect-by-taro'
15-
import { harmony, rn } from '@/utils/platform-taro'
13+
import { harmony } from '@/utils/platform-taro'
14+
import { BadgeProps } from './types'
1615

17-
export type BadgeFill = 'solid' | 'outline'
18-
export interface BadgeProps extends BasicComponent {
19-
value: ReactNode
20-
dot: boolean
21-
max: number
22-
top: string | number
23-
right: string | number
24-
color: string
25-
fill: BadgeFill
26-
}
2716
const defaultProps = {
2817
...ComponentDefaults,
2918
value: '',
3019
dot: false,
3120
max: 99,
32-
top: '4',
33-
right: '8',
34-
color: '',
21+
top: 0,
22+
right: 0,
3523
fill: 'solid',
24+
size: 'large',
3625
} as BadgeProps
3726
export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
3827
const rtl = useRtl()
@@ -45,8 +34,8 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
4534
dot,
4635
top,
4736
right,
48-
color,
4937
fill,
38+
size,
5039
} = {
5140
...defaultProps,
5241
...props,
@@ -77,12 +66,15 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
7766
if (typeof value === 'string' && value) return value
7867
}
7968

80-
const contentClasses = classNames(`${classPrefix}-content`, {
69+
const contentClasses = classNames({
8170
[`${classPrefix}-sup`]: isNumber() || isString() || dot,
71+
[`${classPrefix}-number`]: isNumber(),
8272
[`${classPrefix}-one`]:
8373
typeof content() === 'string' && `${content()}`?.length === 1,
8474
[`${classPrefix}-dot`]: dot,
75+
[`${classPrefix}-dot-${size}`]: dot,
8576
[`${classPrefix}-${fill}`]: fill === 'outline',
77+
[`${classPrefix}-content`]: children,
8678
})
8779

8880
useEffect(() => {
@@ -92,41 +84,14 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
9284
}, [])
9385
const getPositionStyle = async () => {
9486
const style: CSSProperties = {}
95-
style.top = pxTransform(-Number(top) || 0)
96-
if (rn()) {
97-
const reacts = await getRectByTaro(badgeRef.current)
98-
style.left =
99-
reacts?.width && reacts?.width > Number(right)
100-
? pxTransform(reacts.width - Number(right))
101-
: 0
102-
} else {
103-
const dir = rtl ? 'left' : 'right'
104-
style[dir] = isHarmony
105-
? pxTransform(Number(right))
106-
: `${Number(right) || parseFloat(String(right)) || 0}px`
107-
}
87+
style.top = pxTransform(Number(top) || 0)
88+
const dir = rtl ? 'left' : 'right'
89+
style[dir] = isHarmony
90+
? pxTransform(Number(right))
91+
: `${Number(right) || 0}px`
10892
setContentStyle(style)
10993
}
11094

111-
const getStyle = () => {
112-
const style: CSSProperties = {}
113-
if (color) {
114-
if (fill === 'outline') {
115-
style.color = color
116-
isHarmony
117-
? (style.backgroundColor = '#fff')
118-
: (style.background = '#fff')
119-
if (!color?.includes('gradient')) {
120-
style.borderColor = color
121-
}
122-
} else {
123-
style.color = '#fff'
124-
isHarmony ? (style.backgroundColor = color) : (style.background = color)
125-
}
126-
}
127-
return style
128-
}
129-
13095
return (
13196
<View className={classes} style={style} ref={badgeRef}>
13297
{isIcon() && (
@@ -142,10 +107,7 @@ export const Badge: FunctionComponent<Partial<BadgeProps>> = (props) => {
142107
)}
143108
{children}
144109
{!isIcon() && (
145-
<View
146-
className={contentClasses}
147-
style={{ ...contentStyle, ...getStyle() }}
148-
>
110+
<View className={contentClasses} style={contentStyle}>
149111
{content()}
150112
</View>
151113
)}

0 commit comments

Comments
 (0)