Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复 inline style 单位 px 没有转换成 rpx 的问题
Browse files Browse the repository at this point in the history
fix #320
  • Loading branch information
Darmody authored and yesmeck committed Nov 8, 2019
1 parent 8e59eee commit b5a366a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Object {
],
"id": 2,
"props": Object {
"style": "width:100px;height:100px;",
"style": "width:100rpx;height:100rpx;",
},
"text": undefined,
"type": "view",
Expand All @@ -243,7 +243,7 @@ Object {
],
"id": 2,
"props": Object {
"style": "-webkit-line-clamp:2;height:100px;",
"style": "-webkit-line-clamp:2;height:100rpx;",
},
"text": undefined,
"type": "view",
Expand Down
14 changes: 13 additions & 1 deletion packages/remax/src/utils/plainStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ const transformReactStyleKey = (key: string) => {
return styleValue;
};

const transformPx = (value: string) => {
if (typeof value !== 'string') {
return value;
}

return value.replace(/\b(\d+(\.\d+)?)px\b/g, function(match, x) {
const targetUnit = 'rpx';
const size = x;
return size % 1 === 0 ? size + targetUnit : size.toFixed(2) + targetUnit;
});
};

const plainStyle = (style: CSSProperties | null | undefined) => {
if (!style) {
return '';
}
return Object.keys(style)
.reduce((acc: string[], key) => {
const value = (style as any)[key];
return [...acc, `${transformReactStyleKey(key)}:${value};`];
return [...acc, `${transformReactStyleKey(key)}:${transformPx(value)};`];
}, [])
.join('');
};
Expand Down

1 comment on commit b5a366a

@vercel
Copy link

@vercel vercel bot commented on b5a366a Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.