Skip to content

Commit 3384f45

Browse files
authored
fix(react inline styles): Fix React inline styles issue (T1289861) (DevExpress#29821)
1 parent 0d8def4 commit 3384f45

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

packages/devextreme-react/src/core/__tests__/component.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,30 @@ describe('element attrs management', () => {
312312
expect(element.style.color).toEqual('red');
313313
});
314314

315+
it('element inline styles that should convert to px in strict mode (T1289861)', () => {
316+
const { container } = testingLib.render(
317+
<React.StrictMode>
318+
<TestComponent style={{ maxWidth: 300 }} />
319+
</React.StrictMode>,
320+
);
321+
322+
const element: HTMLElement = container?.firstChild as HTMLElement;
323+
324+
expect(element.style.maxWidth).toEqual('300px');
325+
});
326+
327+
it('element inline styles that should not convert to px in strict mode (T1289861)', () => {
328+
const { container } = testingLib.render(
329+
<React.StrictMode>
330+
<TestComponent style={{ zIndex: 1000 }} />
331+
</React.StrictMode>,
332+
);
333+
334+
const element: HTMLElement = container?.firstChild as HTMLElement;
335+
336+
expect(element.style.zIndex).toEqual('1000');
337+
});
338+
315339
it('updates id, className and style', () => {
316340
const { container, rerender } = testingLib.render(
317341
<TestComponent id="id1" className="class1" style={{ background: 'red' }} />,

packages/devextreme-react/src/core/component-base.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
RestoreTreeContext,
3434
TemplateRenderingContext,
3535
} from './contexts';
36+
import { UNITLESS_NUMBERS_SET } from './const';
3637

3738
const DX_REMOVE_EVENT = 'dxremove';
3839

@@ -159,7 +160,11 @@ const ComponentBase = forwardRef<ComponentBaseRef, any>(
159160

160161
Object.entries(styles).forEach(
161162
([name, value]) => {
162-
el.style[name] = value;
163+
if (typeof value === 'number' && !UNITLESS_NUMBERS_SET.has(name)) {
164+
el.style[name] = `${value}px`;
165+
} else {
166+
el.style[name] = value;
167+
}
163168
},
164169
);
165170
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
export const UNITLESS_NUMBERS_SET = new Set([
2+
'animationIterationCount',
3+
'aspectRatio',
4+
'borderImageOutset',
5+
'borderImageSlice',
6+
'borderImageWidth',
7+
'boxFlex',
8+
'boxFlexGroup',
9+
'boxOrdinalGroup',
10+
'columnCount',
11+
'columns',
12+
'flex',
13+
'flexGrow',
14+
'flexPositive',
15+
'flexShrink',
16+
'flexNegative',
17+
'flexOrder',
18+
'gridArea',
19+
'gridRow',
20+
'gridRowEnd',
21+
'gridRowSpan',
22+
'gridRowStart',
23+
'gridColumn',
24+
'gridColumnEnd',
25+
'gridColumnSpan',
26+
'gridColumnStart',
27+
'fontWeight',
28+
'lineClamp',
29+
'lineHeight',
30+
'opacity',
31+
'order',
32+
'orphans',
33+
'scale',
34+
'tabSize',
35+
'widows',
36+
'zIndex',
37+
'zoom',
38+
'fillOpacity',
39+
'floodOpacity',
40+
'stopOpacity',
41+
'strokeDasharray',
42+
'strokeDashoffset',
43+
'strokeMiterlimit',
44+
'strokeOpacity',
45+
'strokeWidth',
46+
'MozAnimationIterationCount',
47+
'MozBoxFlex',
48+
'MozBoxFlexGroup',
49+
'MozLineClamp',
50+
'msAnimationIterationCount',
51+
'msFlex',
52+
'msZoom',
53+
'msFlexGrow',
54+
'msFlexNegative',
55+
'msFlexOrder',
56+
'msFlexPositive',
57+
'msFlexShrink',
58+
'msGridColumn',
59+
'msGridColumnSpan',
60+
'msGridRow',
61+
'msGridRowSpan',
62+
'WebkitAnimationIterationCount',
63+
'WebkitBoxFlex',
64+
'WebKitBoxFlexGroup',
65+
'WebkitBoxOrdinalGroup',
66+
'WebkitColumnCount',
67+
'WebkitColumns',
68+
'WebkitFlex',
69+
'WebkitFlexGrow',
70+
'WebkitFlexPositive',
71+
'WebkitFlexShrink',
72+
'WebkitLineClamp',
73+
]);

0 commit comments

Comments
 (0)