From b5a366a863ecb0aaf74c282a5317974d672c849d Mon Sep 17 00:00:00 2001 From: caihuanyu Date: Thu, 7 Nov 2019 11:16:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20inline=20style=20?= =?UTF-8?q?=E5=8D=95=E4=BD=8D=20px=20=E6=B2=A1=E6=9C=89=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=88=90=20rpx=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #320 --- .../alipay/__snapshots__/index.test.tsx.snap | 4 ++-- packages/remax/src/utils/plainStyle.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/remax/src/__tests__/alipay/__snapshots__/index.test.tsx.snap b/packages/remax/src/__tests__/alipay/__snapshots__/index.test.tsx.snap index 87394934c..243ae074a 100644 --- a/packages/remax/src/__tests__/alipay/__snapshots__/index.test.tsx.snap +++ b/packages/remax/src/__tests__/alipay/__snapshots__/index.test.tsx.snap @@ -218,7 +218,7 @@ Object { ], "id": 2, "props": Object { - "style": "width:100px;height:100px;", + "style": "width:100rpx;height:100rpx;", }, "text": undefined, "type": "view", @@ -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", diff --git a/packages/remax/src/utils/plainStyle.ts b/packages/remax/src/utils/plainStyle.ts index ae518218d..6ae831772 100644 --- a/packages/remax/src/utils/plainStyle.ts +++ b/packages/remax/src/utils/plainStyle.ts @@ -17,6 +17,18 @@ 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 ''; @@ -24,7 +36,7 @@ const plainStyle = (style: CSSProperties | null | undefined) => { 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(''); };