Skip to content

Commit b604a33

Browse files
RSS1102liweijie0812Copilot
authored
fix: Number 类型支持分数形式的默认值配置 (#742)
* fix(getDefaultValue): 支持分数形式的默认值配置 * docs: 文档更新 * feat(getDefaultValue): 支持分数形式的默认值配置 * fix(getDefaultValue): 统一默认值变量名,增强代码可读性 * fix(getDefaultValue): 修复分数类型默认值处理逻辑 * chore: 更新react 文档 * Update packages/scripts/types/vue-props.js Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: liweijie <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 60401ff commit b604a33

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

packages/products/tdesign-react/packages/components/swiper/defaultProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TdSwiperProps } from './type';
77
export const swiperDefaultProps: TdSwiperProps = {
88
animation: 'slide',
99
autoplay: true,
10-
cardScale: '210/332',
10+
cardScale: 210 / 332,
1111
defaultCurrent: 0,
1212
direction: 'horizontal',
1313
duration: 300,

packages/products/tdesign-vue-next/packages/components/swiper/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
/** 卡片模式下非当前展示轮播图的缩放比例 */
2626
cardScale: {
2727
type: Number,
28+
default: 210/332,
2829
},
2930
/** 当前轮播在哪一项(下标) */
3031
current: {

packages/products/tdesign-vue/src/swiper/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
/** 卡片模式下非当前展示轮播图的缩放比例 */
2626
cardScale: {
2727
type: Number,
28+
default: 210/332,
2829
},
2930
/** 当前轮播在哪一项(下标) */
3031
current: {

packages/scripts/types/react-default-props.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ function getDefaultValue(api) {
3838
if (defaultValue === '') return;
3939
if (defaultValue === '\'\'') return defaultValue;
4040
// 输出 Number 类型的默认值
41-
if (api.field_type_text.includes('Number') && !isNaN(defaultValue)) return Number(defaultValue);
41+
if (api.field_type_text.includes('Number')) {
42+
if (defaultValue) {
43+
// 支持诸如 210/332 的分数形式默认值配置原样返回
44+
const frac = defaultValue.match(
45+
/^\s*([+-]?\d+(?:\.\d+)?)\s*\/\s*([+-]?\d+(?:\.\d+)?)\s*$/
46+
);
47+
if (frac) {
48+
return defaultValue;
49+
} else if (!isNaN(defaultValue)) {
50+
return Number(defaultValue);
51+
}
52+
}
53+
}
4254
// 处理字符串类型
4355
if (typeof defaultValue === 'string') {
4456
try {

packages/scripts/types/vue-props.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function getType(cmp, api, name) {
4949

5050
function getDefaultValue(cmp, api, name, isUncontrolApi, useDefault) {
5151
const type = api.field_type_text.join();
52-
const value = api.field_default_value;
53-
let dl = value;
52+
const defaultValue = api.field_default_value;
53+
let dl = defaultValue;
5454
// 如果 API 显示指明 undefined,则一定返回 default: undefined
5555
if (dl === 'undefined') return dl;
5656
if (defaultValueIsUndefined(api)) {
@@ -65,17 +65,30 @@ function getDefaultValue(cmp, api, name, isUncontrolApi, useDefault) {
6565
const type = `: ${getPropType(cmp, name)}`;
6666
dl = `()${type} => ${dl}`;
6767
} else {
68-
dl = value;
68+
dl = defaultValue;
6969
}
7070
} catch (e) {
71-
dl = value;
71+
dl = defaultValue;
7272
}
7373
}
7474
if (type === 'Number') {
75-
dl = value ? Number(value) : value;
75+
if (defaultValue) {
76+
// 支持诸如 210/332 的分数形式默认值配置原样返回
77+
const frac = defaultValue.match(
78+
/^\s*([+-]?\d+(?:\.\d+)?)\s*\/\s*([+-]?\d+(?:\.\d+)?)\s*$/
79+
);
80+
if (frac) {
81+
dl = defaultValue;
82+
} else {
83+
// 其它数字类型,按数值处理
84+
dl = Number(defaultValue);
85+
}
86+
} else {
87+
dl = defaultValue;
88+
}
7689
} else if (type === 'String') {
7790
// 为字符串添加单引号
78-
dl = `'${value}'`;
91+
dl = `'${defaultValue}'`;
7992
// 值不为 '' 时,避免连续两个单引号出现
8093
dl.length !== 2 && (dl = dl.replace(/''/g, '\''));
8194
}

0 commit comments

Comments
 (0)