Skip to content

Commit bcda418

Browse files
committed
fix race condition in Grid when rendered before device is initialized
1 parent 104d882 commit bcda418

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

packages/main/src/components/Grid/__snapshots__/Grid.test.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`Grid Custom Class Names and Styling 1`] = `
66
style="width: 60%; background-color: purple;"
77
>
88
<div
9-
class="Grid-gridSpan-0"
9+
class="Grid-gridSpan-0 Grid-gridSpan3-0"
1010
>
1111
<div
1212
style="height: 6rem; width: 100%; background-color: rgb(169, 234, 255);"
@@ -29,7 +29,7 @@ exports[`Grid Grid Position Center 1`] = `
2929
class="Grid-grid-0 Grid-gridHSpace1-0 Grid-gridVSpace1-0 Grid-gridPositionCenter-0"
3030
>
3131
<div
32-
class="Grid-gridSpan-0"
32+
class="Grid-gridSpan-0 Grid-gridSpan3-0"
3333
>
3434
<div
3535
style="height: 6rem; width: 100%; background-color: rgb(169, 234, 255);"
@@ -52,7 +52,7 @@ exports[`Grid Grid Position Right 1`] = `
5252
class="Grid-grid-0 Grid-gridHSpace1-0 Grid-gridVSpace1-0 Grid-gridPositionRight-0"
5353
>
5454
<div
55-
class="Grid-gridSpan-0"
55+
class="Grid-gridSpan-0 Grid-gridSpan3-0"
5656
>
5757
<div
5858
style="height: 6rem; width: 100%; background-color: rgb(169, 234, 255);"
@@ -75,7 +75,7 @@ exports[`Grid Renders Children 1`] = `
7575
class="Grid-grid-0 Grid-gridHSpace1-0 Grid-gridVSpace1-0"
7676
>
7777
<div
78-
class="Grid-gridSpan-0"
78+
class="Grid-gridSpan-0 Grid-gridSpan3-0"
7979
>
8080
<div
8181
style="height: 6rem; width: 100%; background-color: rgb(169, 234, 255);"
@@ -98,15 +98,15 @@ exports[`Grid Renders Children with custom layout data 1`] = `
9898
class="Grid-grid-0 Grid-gridHSpace1-0 Grid-gridVSpace1-0"
9999
>
100100
<div
101-
class="Grid-gridSpan-0"
101+
class="Grid-gridSpan-0 Grid-gridSpan12-0"
102102
>
103103
<div
104104
data-layout="[object Object]"
105105
style="height: 6rem; width: 100%; background-color: rgb(169, 234, 255);"
106106
/>
107107
</div>
108108
<div
109-
class="Grid-gridSpan-0"
109+
class="Grid-gridSpan-0 Grid-gridSpan3-0 Grid-gridIndent1-0"
110110
>
111111
<div
112112
data-layout="[object Object]"

packages/main/src/components/Grid/index.tsx

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { createComponentStyles } from '@ui5/webcomponents-react-base/lib/createComponentStyles';
12
import { StyleClassHelper } from '@ui5/webcomponents-react-base/lib/StyleClassHelper';
23
import { usePassThroughHtmlProps } from '@ui5/webcomponents-react-base/lib/usePassThroughHtmlProps';
3-
import { useViewportRange } from "@ui5/webcomponents-react-base/lib/useViewportRange";
4+
import { useViewportRange } from '@ui5/webcomponents-react-base/lib/useViewportRange';
45
import React, {
56
Children,
67
CSSProperties,
@@ -12,7 +13,6 @@ import React, {
1213
Ref,
1314
useMemo
1415
} from 'react';
15-
import { createComponentStyles } from '@ui5/webcomponents-react-base/lib/createComponentStyles';
1616
import { CommonProps } from '../../interfaces/CommonProps';
1717
import { GridClasses } from './Grid.jss';
1818

@@ -61,32 +61,29 @@ export interface GridPropTypes extends CommonProps {
6161
children: ReactNode | ReactNodeArray;
6262
}
6363

64-
const INDENT_PATTERN = /^([X][L](?:[0-9]|1[0-1]))? ?([L](?:[0-9]|1[0-1]))? ?([M](?:[0-9]|1[0-1]))? ?([S](?:[0-9]|1[0-1]))?$/i;
65-
const SPAN_PATTERN = /^([X][L](?:[1-9]|1[0-2]))? ?([L](?:[1-9]|1[0-2]))? ?([M](?:[1-9]|1[0-2]))? ?([S](?:[1-9]|1[0-2]))?$/i;
64+
const INDENT_PATTERN = /^([X][L](?<LargeDesktop>[0-9]|1[0-1]))? ?([L](?<Desktop>[0-9]|1[0-1]))? ?([M](?<Tablet>[0-9]|1[0-1]))? ?([S](?<Phone>[0-9]|1[0-1]))?$/i;
65+
const SPAN_PATTERN = /^([X][L](?<LargeDesktop>[1-9]|1[0-2]))? ?([L](?<Desktop>[1-9]|1[0-2]))? ?([M](?<Tablet>[1-9]|1[0-2]))? ?([S](?<Phone>[1-9]|1[0-2]))?$/i;
6666

67-
const getCurrentSpan = () => {
68-
const classList = document.querySelector('html').classList;
69-
const isXL = classList.contains('sapUiMedia-StdExt-LargeDesktop');
70-
const isL = !isXL && classList.contains('sapUiMedia-Std-Desktop');
71-
const isM = !isL && classList.contains('sapUiMedia-Std-Tablet');
72-
const isS = !isM && classList.contains('sapUiMedia-Std-Phone');
73-
return [false, isXL, isL, isM, isS].indexOf(true);
74-
};
67+
const DefaultSpanMap = new Map();
68+
DefaultSpanMap.set('Phone', 12);
69+
DefaultSpanMap.set('Tablet', 6);
70+
DefaultSpanMap.set('Desktop', 3);
71+
DefaultSpanMap.set('LargeDesktop', 3);
72+
73+
const DefaultIndentMap = new Map();
74+
DefaultIndentMap.set('Phone', 0);
75+
DefaultIndentMap.set('Tablet', 0);
76+
DefaultIndentMap.set('Desktop', 0);
77+
DefaultIndentMap.set('LargeDesktop', 0);
7578

76-
const getSpanFromString = (span) => {
77-
const currentSpan = getCurrentSpan();
79+
const getSpanFromString = (span, currentRange) => {
7880
const spanConfig = SPAN_PATTERN.exec(span);
79-
return spanConfig[currentSpan]
80-
? parseInt(spanConfig[currentSpan].replace(/[XLMS]{0,2}/g, ''), 10)
81-
: [undefined, 3, 3, 6, 12][currentSpan];
81+
return spanConfig.groups[currentRange] ?? DefaultSpanMap.get(currentRange);
8282
};
8383

84-
const getIndentFromString = (indent) => {
85-
const currentSpan = getCurrentSpan();
84+
const getIndentFromString = (indent, currentRange) => {
8685
const indentConfig = INDENT_PATTERN.exec(indent);
87-
return indentConfig[currentSpan]
88-
? parseInt(indentConfig[currentSpan].replace(/[XLMS]{0,2}/g, ''), 10)
89-
: [undefined, 0, 0, 0, 0][currentSpan];
86+
return indentConfig.groups[currentRange] ?? DefaultIndentMap.get(currentRange);
9087
};
9188

9289
const useStyles = createComponentStyles(GridClasses, { name: 'Grid' });
@@ -114,6 +111,8 @@ const Grid: FC<GridPropTypes> = forwardRef((props: GridPropTypes, ref: Ref<HTMLD
114111
gridClasses.put(classes[`gridHSpace${hSpacing === 0.5 ? '05' : hSpacing}`]);
115112
gridClasses.put(classes[`gridVSpace${vSpacing === 0.5 ? '05' : vSpacing}`]);
116113

114+
const currentRange = useViewportRange('StdExt');
115+
117116
if (GridPosition.Center === position) {
118117
gridClasses.put(classes.gridPositionCenter);
119118
}
@@ -142,22 +141,19 @@ const Grid: FC<GridPropTypes> = forwardRef((props: GridPropTypes, ref: Ref<HTMLD
142141
gridClasses.put(className);
143142
}
144143

145-
useViewportRange('StdExt');
146-
147144
const renderGridElements = (child: ReactElement<any>) => {
148-
const span = getSpanFromString(defaultSpan);
149-
const indentSpan = getIndentFromString(defaultIndent);
150-
151145
const gridSpanClasses = StyleClassHelper.of(classes.gridSpan);
152146
if (child.props['data-layout'] && child.props['data-layout'].span) {
153-
const childSpan = getSpanFromString(child.props['data-layout'].span);
147+
const childSpan = getSpanFromString(child.props['data-layout'].span, currentRange);
154148
gridSpanClasses.put(classes[`gridSpan${childSpan}`]);
155149
} else {
150+
const span = getSpanFromString(defaultSpan, currentRange);
156151
gridSpanClasses.put(classes[`gridSpan${span}`]);
157152
}
158153

154+
const indentSpan = getIndentFromString(defaultIndent, currentRange);
159155
if (child.props['data-layout'] && child.props['data-layout'].indent) {
160-
const childIndent = getIndentFromString(child.props['data-layout'].indent);
156+
const childIndent = getIndentFromString(child.props['data-layout'].indent, currentRange);
161157
if (childIndent && childIndent > 0) {
162158
gridSpanClasses.put(classes[`gridIndent${childIndent}`]);
163159
}

0 commit comments

Comments
 (0)