Skip to content

Commit 8dbfce7

Browse files
authored
feat(Badge): Badge 组件新增 position 属性 (#65)
* feat(Badge): Badge 组件新增 position 属性 * test(Badge): 更新依赖 Badge 组件的 Snapshots
1 parent 1415841 commit 8dbfce7

File tree

28 files changed

+370
-188
lines changed

28 files changed

+370
-188
lines changed

packages/rc-cli/src/commands/jest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function test(command: Config.Argv) {
2424
debug: command.debug,
2525
coverage: command.coverage,
2626
colors: command.colors,
27-
updateSnapshot: command.u,
27+
updateSnapshot: command.updateSnapshot,
2828
// make jest tests faster
2929
// see: https://ivantanev.com/make-jest-faster/
3030
maxWorkers: '50%',

packages/rc-ui-lib/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ module.exports = {
1010
'!**/**/PropsType.ts',
1111
'!**/test/**',
1212
],
13+
// testMatch: ['**/badge/__test__/**/*.[jt]s?(x)'],
1314
};

packages/rc-ui-lib/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build:site": "rc-cli build-site",
2121
"release:site": "pnpm build:site && gh-pages -d site",
2222
"prepack": "pnpm build",
23-
"test": "rc-cli test --u --colors --coverage",
23+
"test": "rc-cli test --colors --coverage",
2424
"test:coverage": "open tests/coverage/index.html"
2525
},
2626
"author": "rancui",
@@ -82,4 +82,4 @@
8282
"lib/**/style/*",
8383
"*.less"
8484
]
85-
}
85+
}

packages/rc-ui-lib/src/action-bar/__test__/__snapshots__/index.spec.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ exports[`ActionBar should render correctly when using badge prop in ActionBarIco
180180
class="rc-badge__wrapper rc-action-bar-icon__icon van-icon van-icon-chat-o"
181181
>
182182
<div
183-
class="rc-badge rc-badge--dot rc-badge--fixed"
183+
class="rc-badge rc-badge--top-right rc-badge--dot rc-badge--fixed"
184184
/>
185185
</div>
186186
客服
@@ -194,7 +194,7 @@ exports[`ActionBar should render correctly when using badge prop in ActionBarIco
194194
class="rc-badge__wrapper rc-action-bar-icon__icon van-icon van-icon-cart-o"
195195
>
196196
<div
197-
class="rc-badge rc-badge--fixed"
197+
class="rc-badge rc-badge--top-right rc-badge--fixed"
198198
>
199199
5
200200
</div>

packages/rc-ui-lib/src/action-sheet/__test__/__snapshots__/index.spec.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Array [
1111
<Badge
1212
className="rc-action-sheet__close van-icon van-icon-success"
1313
onClick={[Function]}
14+
position="top-right"
1415
showZero={true}
1516
style={
1617
Object {
@@ -35,6 +36,7 @@ Array [
3536
<Badge
3637
className="rc-action-sheet__close van-icon van-icon-success"
3738
onClick={[Function]}
39+
position="top-right"
3840
showZero={true}
3941
style={
4042
Object {

packages/rc-ui-lib/src/badge/Badge.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ const Badge: React.FC<BadgeProps> = (props) => {
1212
const [bem] = createNamespace('badge', prefixCls);
1313

1414
const hasContent = () => {
15-
// if (props.content) {
16-
// return true;
17-
// }
1815
return isDef(content) && content !== '' && (showZero || +content !== 0);
1916
};
2017

2118
const renderContent = () => {
2219
if (!dot && hasContent()) {
23-
if (isDef(max) && isNumeric(content?.toString()) && +content > max) {
20+
if (isDef(max) && isNumeric(content?.toString()) && +content > +max) {
2421
return `${max}+`;
2522
}
2623

@@ -29,6 +26,9 @@ const Badge: React.FC<BadgeProps> = (props) => {
2926
return null;
3027
};
3128

29+
const getOffsetWithMinusString = (val: string) =>
30+
val.startsWith('-') ? val.replace('-', '') : `-${val}`;
31+
3232
const renderBadge = () => {
3333
if (hasContent() || props.dot) {
3434
let style: CSSProperties = {
@@ -37,13 +37,20 @@ const Badge: React.FC<BadgeProps> = (props) => {
3737

3838
if (props.offset) {
3939
const [x, y] = props.offset;
40+
const { position } = props;
41+
const [offsetY, offsetX] = position.split('-') as ['top' | 'bottom', 'left' | 'right'];
4042

4143
if (props.children) {
42-
style.top = addUnit(y);
44+
if (typeof y === 'number') {
45+
style[offsetY] = addUnit(offsetY === 'top' ? y : -y);
46+
} else {
47+
style[offsetY] = offsetY === 'top' ? addUnit(y) : getOffsetWithMinusString(y);
48+
}
49+
4350
if (typeof x === 'number') {
44-
style.right = addUnit(-x);
51+
style[offsetX] = addUnit(offsetX === 'left' ? x : -x);
4552
} else {
46-
style.right = x.startsWith('-') ? x.replace('-', '') : `-${x}`;
53+
style[offsetX] = offsetX === 'left' ? addUnit(x) : getOffsetWithMinusString(x);
4754
}
4855
} else {
4956
style.marginTop = addUnit(y);
@@ -60,7 +67,7 @@ const Badge: React.FC<BadgeProps> = (props) => {
6067
{
6168
[props.className]: props.className && !props.children,
6269
},
63-
bem({ dot: props.dot, fixed: !!props.children }),
70+
bem([props.position, { dot: props.dot, fixed: !!props.children }]),
6471
)}
6572
style={style}
6673
>
@@ -91,6 +98,7 @@ const Badge: React.FC<BadgeProps> = (props) => {
9198
Badge.defaultProps = {
9299
tag: 'div',
93100
showZero: true,
101+
position: 'top-right',
94102
};
95103

96104
export default Badge;

packages/rc-ui-lib/src/badge/PropsType.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { BaseTypeProps } from '../utils';
44
/** HTML 标签 */
55
export type HtmlTagType = keyof HTMLElementTagNameMap;
66

7+
export type BadgePosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
8+
79
export interface BadgeProps extends BaseTypeProps {
810
/** 徽标内容 */
911
content?: number | string | React.ReactNode;
@@ -24,6 +26,7 @@ export interface BadgeProps extends BaseTypeProps {
2426
* @default div
2527
*/
2628
tag?: HtmlTagType;
29+
position?: BadgePosition;
2730
/** 自元素 */
2831
children?: React.ReactNode;
2932
onClick?: (e: React.MouseEvent) => void;
@@ -34,3 +37,16 @@ export type BadgeSettingProps = Omit<
3437
BadgeProps,
3538
'children' | 'tag' | 'onClick' | 'style' | 'className'
3639
>;
40+
41+
export type BadgeThemeVars = {
42+
badgeSize?: string;
43+
badgeColor?: string;
44+
badgePadding?: string;
45+
badgeFontSize?: string;
46+
badgeFontWeight?: string;
47+
badgeBorderWidth?: string;
48+
badgeBackground?: string;
49+
badgeDotColor?: string;
50+
badgeDotSize?: string;
51+
badgeFont?: string;
52+
};

packages/rc-ui-lib/src/badge/README.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import { Badge } from 'rc-ui-lib';
1818

1919
```jsx
2020
<Badge content="5">
21-
<div class="child" />
21+
<div className="child" />
2222
</Badge>
2323
<Badge content="10">
24-
<div class="child" />
24+
<div className="child" />
2525
</Badge>
2626
<Badge content="Hot">
27-
<div class="child" />
27+
<div className="child" />
2828
</Badge>
2929
<Badge dot>
30-
<div class="child" />
30+
<div className="child" />
3131
</Badge>
3232

3333
<style>
@@ -46,13 +46,13 @@ import { Badge } from 'rc-ui-lib';
4646

4747
```jsx
4848
<Badge content="20" max="9">
49-
<div class="child" />
49+
<div className="child" />
5050
</Badge>
5151
<Badge content="50" max="20">
52-
<div class="child" />
52+
<div className="child" />
5353
</Badge>
5454
<Badge content="200" max="99">
55-
<div class="child" />
55+
<div className="child" />
5656
</Badge>
5757
```
5858

@@ -62,21 +62,21 @@ import { Badge } from 'rc-ui-lib';
6262

6363
```jsx
6464
<Badge content="5" color="#1989fa">
65-
<div class="child" />
65+
<div className="child" />
6666
</Badge>
6767
<Badge content="10" color="#1989fa">
68-
<div class="child" />
68+
<div className="child" />
6969
</Badge>
7070
<Badge dot color="#1989fa">
71-
<div class="child" />
71+
<div className="child" />
7272
</Badge>
7373
```
7474

7575
### 自定义徽标内容
7676

7777
```jsx
7878
<Badge content={<Icon name="success" className="badge-icon" />}>
79-
<div class="child" />
79+
<div className="child" />
8080
</Badge>
8181
```
8282

@@ -87,6 +87,22 @@ import { Badge } from 'rc-ui-lib';
8787
line-height: 16px;
8888
}
8989
```
90+
### 自定义徽标位置
91+
92+
通过 `position` 属性来设置徽标的位置。
93+
94+
```jsx
95+
<Badge content="10" position="top-left">
96+
<div className="child" />
97+
</Badge>
98+
<Badge content="10" position="bottom-left">
99+
<div className="child" />
100+
</Badge>
101+
<Badge content="10" position="bottom-right">
102+
<div className="child" />
103+
</Badge>
104+
```
105+
90106

91107
### 独立展示
92108

@@ -110,13 +126,21 @@ import { Badge } from 'rc-ui-lib';
110126
| max | 最大值,超过最大值会显示 `{max}+`,仅当 content 为数字时有效 | _number \| string_ | - |
111127
| offset | 设置徽标的偏移量,数组的两项分别对应水平和垂直方向的偏移量,默认单位为 `px` | _[number \| string, number \| string]_ | - |
112128
| showZero | 当 content 为数字 0 时,是否展示徽标 | _boolean_ | `true` |
129+
| position | 徽标位置,可选值为 `top-left` `bottom-left` `bottom-right` | _string_ | `top-right` |
113130

114131
### 事件
115132

116133
| 事件名 | 说明 | 回调参数 |
117134
| ------- | ---------- | -------------- |
118135
| onClick | 点击时触发 | _event: Event_ |
119136

137+
### 类型定义
138+
139+
组件导出以下类型定义:
140+
141+
```ts
142+
import type { BadgeProps, BadgePosition, BadgeThemeVars } from 'rc-ui-lib';
143+
120144
## 主题定制
121145

122146
### 样式变量

0 commit comments

Comments
 (0)