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

fix: 修复 CSS 变量的数字会被加上单位的问题 #1870

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/remax-runtime/src/__tests__/utils/plainStyle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ describe('plainStyle pxToRpx', () => {
style = plainStyle({ width: '100.55555px' });
expect(style.indexOf('100.56rpx') > -1).toBeTruthy();
});

it('does not transform css variable', () => {
const style = plainStyle({ '--column': 1 });
expect(style).toMatchInlineSnapshot(`"--column:1;"`);
});
});
5 changes: 2 additions & 3 deletions packages/remax-runtime/src/utils/plainStyle/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CSSProperties } from 'react';
import { isUnitlessNumber } from './CSSProperty';
import { find, RuntimeOptions } from '@remax/framework-shared';

Expand Down Expand Up @@ -39,12 +38,12 @@ const transformPx = (value: string) => {
});
};

const plainStyle = (style: CSSProperties) => {
const plainStyle = (style: Record<string, string | number>) => {
return Object.keys(style)
.reduce((acc: string[], key) => {
let value = (style as any)[key];

if (!Number.isNaN(Number(value)) && !isUnitlessNumber[key]) {
if (!Number.isNaN(Number(value)) && !isUnitlessNumber[key] && !key?.startsWith('--')) {
value = value + 'rpx';
}

Expand Down