feat(Badge): max support string type

This commit is contained in:
chenjiahan 2020-10-02 17:05:08 +08:00
parent 56c50666ce
commit 839b8654ee
7 changed files with 24 additions and 23 deletions

View File

@ -15,7 +15,7 @@ app.use(Badge);
### Basic Usage ### Basic Usage
```html ```html
<van-badge :content="5"> <van-badge content="5">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge dot> <van-badge dot>
@ -35,10 +35,10 @@ app.use(Badge);
### Max ### Max
```html ```html
<van-badge :content="20" :max="9"> <van-badge content="20" max="9">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge :content="200" :max="99"> <van-badge content="200" max="99">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
``` ```
@ -46,7 +46,7 @@ app.use(Badge);
### Custom Color ### Custom Color
```html ```html
<van-badge :content="5" color="#1989fa"> <van-badge content="5" color="#1989fa">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge dot color="#1989fa"> <van-badge dot color="#1989fa">
@ -57,7 +57,7 @@ app.use(Badge);
### Standaline ### Standaline
```html ```html
<van-badge :content="200" :max="99" /> <van-badge content="200" max="99" />
``` ```
## API ## API
@ -66,10 +66,10 @@ app.use(Badge);
| Attribute | Description | Type | Default | | Attribute | Description | Type | Default |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| content | Badge content | _string \| number_ | - | | content | Badge content | _number \| string_ | - |
| color | Background color | _string_ | `#ee0a24` | | color | Background color | _string_ | `#ee0a24` |
| dot | Whether to show dot | _boolean_ | `false` | | dot | Whether to show dot | _boolean_ | `false` |
| max | Max valueshow `{max}+` when exceedonly works when content is number type | _number_ | - | | max | Max valueshow `{max}+` when exceedonly works when content is number | _number \| string_ | - |
### Slots ### Slots

View File

@ -43,10 +43,10 @@ app.use(Badge);
设置 `max` 属性后,当 `content` 的数值超过最大值时,会自动显示为 `{max}+` 设置 `max` 属性后,当 `content` 的数值超过最大值时,会自动显示为 `{max}+`
```html ```html
<van-badge :content="20" :max="9"> <van-badge :content="20" max="9">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge :content="200" :max="99"> <van-badge :content="200" max="99">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
``` ```
@ -69,7 +69,7 @@ app.use(Badge);
当 Badge 没有子元素时,会作为一个独立的元素进行展示。 当 Badge 没有子元素时,会作为一个独立的元素进行展示。
```html ```html
<van-badge :content="200" :max="99" /> <van-badge :content="200" max="99" />
``` ```
## API ## API
@ -78,10 +78,10 @@ app.use(Badge);
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| content | 徽标内容 | _string \| number_ | - | | content | 徽标内容 | _number \| string_ | - |
| color | 徽标背景颜色 | _string_ | `#ee0a24` | | color | 徽标背景颜色 | _string_ | `#ee0a24` |
| dot | 是否展示为小红点 | _boolean_ | `false` | | dot | 是否展示为小红点 | _boolean_ | `false` |
| max | 最大值,超过最大值会显示 `{max}+`,仅当 content 为 number 类型时有效 | _number_ | - | | max | 最大值,超过最大值会显示 `{max}+`,仅当 content 为数字时有效 | _number \| string_ | - |
### Slots ### Slots

View File

@ -1,7 +1,7 @@
<template> <template>
<demo-section> <demo-section>
<demo-block :title="t('basicUsage')"> <demo-block :title="t('basicUsage')">
<van-badge :content="5"> <van-badge content="5">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge dot> <van-badge dot>
@ -10,16 +10,16 @@
</demo-block> </demo-block>
<demo-block :title="t('max')"> <demo-block :title="t('max')">
<van-badge :content="20" :max="9"> <van-badge content="20" max="9">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge :content="200" :max="99"> <van-badge content="200" max="99">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
</demo-block> </demo-block>
<demo-block :title="t('customColor')"> <demo-block :title="t('customColor')">
<van-badge :content="5" :color="BLUE"> <van-badge content="5" :color="BLUE">
<div class="child" /> <div class="child" />
</van-badge> </van-badge>
<van-badge dot :color="BLUE"> <van-badge dot :color="BLUE">
@ -28,7 +28,7 @@
</demo-block> </demo-block>
<demo-block :title="t('standalone')"> <demo-block :title="t('standalone')">
<van-badge :content="200" :max="99" style="margin-left: 16px;" /> <van-badge content="200" max="99" style="margin-left: 16px;" />
</demo-block> </demo-block>
</demo-section> </demo-section>
</template> </template>

View File

@ -1,12 +1,13 @@
import type { PropType } from 'vue'; import type { PropType } from 'vue';
import { isDef, createNamespace } from '../utils'; import { isDef, createNamespace } from '../utils';
import { isNumeric } from '../utils/validate/number';
const [createComponent, bem] = createNamespace('badge'); const [createComponent, bem] = createNamespace('badge');
export default createComponent({ export default createComponent({
props: { props: {
max: Number,
dot: Boolean, dot: Boolean,
max: [Number, String],
color: String, color: String,
content: [Number, String], content: [Number, String],
tag: { tag: {
@ -27,7 +28,7 @@ export default createComponent({
return slots.content(); return slots.content();
} }
if (isDef(max) && typeof content === 'number' && content > max) { if (isDef(max) && isNumeric(content!) && +content > max) {
return `${max}+`; return `${max}+`;
} }

View File

@ -6,8 +6,7 @@ export function addUnit(value?: string | number): string | undefined {
return undefined; return undefined;
} }
value = String(value); return isNumeric(value) ? `${value}px` : String(value);
return isNumeric(value) ? `${value}px` : value;
} }
export function getSizeStyle(originSize?: string | number) { export function getSizeStyle(originSize?: string | number) {

View File

@ -91,6 +91,7 @@ test('isMobile', () => {
}); });
test('isNumeric', () => { test('isNumeric', () => {
expect(isNumeric(1)).toBeTruthy();
expect(isNumeric('1')).toBeTruthy(); expect(isNumeric('1')).toBeTruthy();
expect(isNumeric('1.2')).toBeTruthy(); expect(isNumeric('1.2')).toBeTruthy();
expect(isNumeric('1..2')).toBeFalsy(); expect(isNumeric('1..2')).toBeFalsy();

View File

@ -1,5 +1,5 @@
export function isNumeric(val: string): boolean { export function isNumeric(val: string | number): val is string {
return /^\d+(\.\d+)?$/.test(val); return typeof val === 'number' || /^\d+(\.\d+)?$/.test(val);
} }
export function isNaN(val: number): val is typeof NaN { export function isNaN(val: number): val is typeof NaN {