Skip to content

Commit 6a5510b

Browse files
RSS1102github-actions[bot]
authored andcommitted
refactor(Statistic): change color attribute type to string and update docs description (#3671)
* refactor(Statistic): change color attribute type to string and update docs description * test(Statistic): add tests for trend increase and decrease icons * chore: update snapshot * feat(Statistic): add color prop tests and update documentation for color styles * fix(tests): update import path for COLOR_MAP in Statistic tests * test(Statistic): refactor colorKeys test to check style attribute instead of contentStyle * chore: update snapshot * chore: update settings.json * docs: 更新颜色风格描述,明确支持深浅色模式切换 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 49badb7 commit 6a5510b

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

src/statistic/__tests__/index.test.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mount } from '@vue/test-utils';
22
import Statistic from '@/src/statistic/index.ts';
3+
import { COLOR_MAP } from '../../_common/js/statistic/utils';
34

45
describe('Statistic', () => {
56
describe(':props', () => {
@@ -110,5 +111,48 @@ describe('Statistic', () => {
110111
expect(trendIconElement.exists()).toBe(true);
111112
expect(trendIconElement.is('svg')).toBe(true);
112113
});
114+
115+
it('color:#ff0000', () => {
116+
const wrapper = mount(Statistic, {
117+
propsData: {
118+
title: 'Total Sales',
119+
value: 1000,
120+
color: '#ff0000',
121+
},
122+
});
123+
124+
const contentElement = wrapper.find('.t-statistic-content');
125+
expect(contentElement.exists()).toBe(true);
126+
expect(contentElement.attributes('style')).toContain('color: rgb(255, 0, 0)');
127+
});
128+
129+
it('color:yellow', () => {
130+
const wrapper = mount(Statistic, {
131+
propsData: {
132+
title: 'Total Sales',
133+
value: 1000,
134+
color: 'yellow',
135+
},
136+
});
137+
138+
const contentElement = wrapper.find('.t-statistic-content');
139+
expect(contentElement.exists()).toBe(true);
140+
expect(contentElement.attributes('style')).toContain('color: yellow');
141+
});
142+
143+
it('colors: colorKeys', async () => {
144+
Object.keys(COLOR_MAP).forEach((color) => {
145+
const wrapper = mount(Statistic, {
146+
propsData: {
147+
value: 1000,
148+
color,
149+
},
150+
});
151+
152+
const contentElement = wrapper.find('.t-statistic-content');
153+
expect(contentElement.exists()).toBe(true);
154+
expect(contentElement.attributes('style')).toContain(`color: ${COLOR_MAP[color]}`);
155+
});
156+
});
113157
});
114158
});

src/statistic/props.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ export default {
1414
},
1515
/** 是否开始动画 */
1616
animationStart: Boolean,
17-
/** 颜色风格,依次为 TDesign 风格的黑色、蓝色、红色、橙色、绿色。也可以为任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持的 RGB 等值 */
17+
/** 颜色风格,预设五个 TDesign 颜色风格:黑色(black)、蓝色(blue)、红色(red)、橙色(orange)、绿色(green)支持深浅色模式切换。也可以自定义任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持颜色值,深浅色模式切换需自行适配 */
1818
color: {
19-
type: String as PropType<TdStatisticProps['color']>,
20-
validator(val: TdStatisticProps['color']): boolean {
21-
if (!val) return true;
22-
return ['black', 'blue', 'red', 'orange', 'green'].includes(val);
23-
},
19+
type: String,
20+
default: '',
2421
},
2522
/** 小数保留位数 */
2623
decimalPlaces: {
@@ -40,7 +37,7 @@ export default {
4037
prefix: {
4138
type: [String, Function] as PropType<TdStatisticProps['prefix']>,
4239
},
43-
/** 默认展示进位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时隐藏分隔符 */
40+
/** 默认展示千位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时展示默认分隔符 */
4441
separator: {
4542
type: String,
4643
default: ',',

src/statistic/statistic.en-US.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
:: BASE_DOC ::
22

33
## API
4+
45
### Statistic Props
56

67
name | type | default | description | required
78
-- | -- | -- | -- | --
89
animation | Object | - | Animation effect control, `duration` refers to the transition time of the animation `unit: millisecond`, `valueFrom` refers to the initial value of the animation. `{ duration, valueFrom }`。Typescript:`animation` `interface animation { duration: number; valueFrom: number; }`[see more ts definition](https://github.com/Tencent/tdesign-vue/tree/develop/src/statistic/type.ts) | N
910
animationStart | Boolean | false | Whether to start animation | N
10-
color | String | - | Color style, followed by TDesign style black, blue, red, orange, green.Can also be any RGB equivalent supported by [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value)。options:black/blue/red/orange/green | N
11+
color | String | - | The color style can support TDesign's light and dark modes with the following options: black, blue, red, orange, and green. Alternatively, it can be any [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) value, but in this case, TDesign's light and dark modes will not be supported. | N
1112
decimalPlaces | Number | - | Decimal places | N
1213
extra | String / Slot / Function | - | Additional display content。Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1314
format | Function | - | Format numeric display value。Typescript:`(value: number) => number` | N
1415
loading | Boolean | false | Loading | N
1516
prefix | String / Slot / Function | - | Prefix content, display priority is higher than trend。Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
16-
separator | String | , | The carry separator is displayed by default, and can be customized to other content. When `separator = ''` is set to an empty string/null/undefined, the separator is hidden | N
17+
separator | String | , | Thousands separator is displayed by default, and can be customized to other content, and the default separator is displayed when `separator = ''` is set to an empty string/null/undefined | N
1718
suffix | String / Slot / Function | - | Suffix content, display priority is higher than trend。Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1819
title | String / Slot / Function | - | The title of Statistic。Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
19-
trend | String | - | trend。optionsincrease/decrease | N
20-
trendPlacement | String | left | Position of trending placements。optionsleft/right | N
20+
trend | String | - | trend。options: increase/decrease | N
21+
trendPlacement | String | left | Position of trending placements。options: left/right | N
2122
unit | String / Slot / Function | - | Unit content。Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
2223
value | Number | - | The value of Statistic | N
24+
25+
### StatisticInstanceFunctions 组件实例方法
26+
27+
name | params | return | description
28+
-- | -- | -- | --
29+
start | \- | \- | required。start animation

src/statistic/statistic.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
:: BASE_DOC ::
22

33
## API
4+
45
### Statistic Props
56

6-
名称 | 类型 | 默认值 | 说明 | 必传
7+
名称 | 类型 | 默认值 | 描述 | 必传
78
-- | -- | -- | -- | --
89
animation | Object | - | 动画效果控制,`duration` 指动画的过渡时间`单位:毫秒``valueFrom` 指动画的起始数值。`{ duration, valueFrom }`。TS 类型:`animation` `interface animation { duration: number; valueFrom: number; }`[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/statistic/type.ts) | N
910
animationStart | Boolean | false | 是否开始动画 | N
10-
color | String | - | 颜色风格,依次为 TDesign 风格的黑色、蓝色、红色、橙色、绿色。也可以为任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持的 RGB 等值。可选项:black/blue/red/orange/green | N
11+
color | String | - | 颜色风格,预设五个 TDesign 颜色风格:黑色(black)、蓝色(blue)、红色(red)、橙色(orange)、绿色(green)支持深浅色模式切换。也可以自定义任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持颜色值,深浅色模式切换需自行适配 | N
1112
decimalPlaces | Number | - | 小数保留位数 | N
1213
extra | String / Slot / Function | - | 额外的显示内容。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1314
format | Function | - | 格式化数值显示值。TS 类型:`(value: number) => number` | N
1415
loading | Boolean | false | 是否加载中 | N
1516
prefix | String / Slot / Function | - | 前缀内容,展示优先级高于 trend。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
16-
separator | String | , | 默认展示进位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时隐藏分隔符 | N
17+
separator | String | , | 默认展示千位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时展示默认分隔符 | N
1718
suffix | String / Slot / Function | - | 后缀内容,展示优先级高于 trend。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1819
title | String / Slot / Function | - | 数值显示的标题。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1920
trend | String | - | 趋势。可选项:increase/decrease | N
2021
trendPlacement | String | left | 趋势展示位置。可选项:left/right | N
2122
unit | String / Slot / Function | - | 单位内容。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
2223
value | Number | - | 数值显示的值 | N
24+
25+
### StatisticInstanceFunctions 组件实例方法
26+
27+
名称 | 参数 | 返回值 | 描述
28+
-- | -- | -- | --
29+
start | \- | \- | 必需。开始动画

src/statistic/statistic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default defineComponent({
3030
const tween = ref(null);
3131

3232
const numberValue = computed(() => (isNumber(props.value) ? props.value : 0));
33-
const valueStyle = computed(() => ({ color: COLOR_MAP[color.value] || color.value }));
33+
const valueStyle = computed(() => ({ color: COLOR_MAP[color.value as keyof typeof COLOR_MAP] || color.value }));
3434
const innerDecimalPlaces = computed(
3535
() => decimalPlaces.value ?? numberValue.value.toString().split('.')[1]?.length ?? 0,
3636
);

src/statistic/type.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export interface TdStatisticProps {
1717
*/
1818
animationStart?: boolean;
1919
/**
20-
* 颜色风格,依次为 TDesign 风格的黑色、蓝色、红色、橙色、绿色。也可以为任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持的 RGB 等值
20+
* 颜色风格,预设五个 TDesign 颜色风格:黑色(black)、蓝色(blue)、红色(red)、橙色(orange)、绿色(green)支持深浅色模式切换。也可以自定义任何 [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) 支持颜色值,深浅色模式切换需自行适配
21+
* @default ''
2122
*/
22-
color?: 'black' | 'blue' | 'red' | 'orange' | 'green';
23+
color?: string;
2324
/**
2425
* 小数保留位数
2526
*/
@@ -42,7 +43,7 @@ export interface TdStatisticProps {
4243
*/
4344
prefix?: string | TNode;
4445
/**
45-
* 默认展示进位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时隐藏分隔符
46+
* 默认展示千位分隔符,可以自定义为其他内容,`separator = ''` 设置为空字符串/null/undefined 时展示默认分隔符
4647
* @default ,
4748
*/
4849
separator?: string;
@@ -73,6 +74,14 @@ export interface TdStatisticProps {
7374
value?: number;
7475
}
7576

77+
/** 组件实例方法 */
78+
export interface StatisticInstanceFunctions {
79+
/**
80+
* 开始动画
81+
*/
82+
start: () => void;
83+
}
84+
7685
export interface animation {
7786
duration: number;
7887
valueFrom: number;

0 commit comments

Comments
 (0)