1
+ import { createComponentStyles } from '@ui5/webcomponents-react-base/lib/createComponentStyles' ;
1
2
import { StyleClassHelper } from '@ui5/webcomponents-react-base/lib/StyleClassHelper' ;
2
3
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' ;
4
5
import React , {
5
6
Children ,
6
7
CSSProperties ,
@@ -12,7 +13,6 @@ import React, {
12
13
Ref ,
13
14
useMemo
14
15
} from 'react' ;
15
- import { createComponentStyles } from '@ui5/webcomponents-react-base/lib/createComponentStyles' ;
16
16
import { CommonProps } from '../../interfaces/CommonProps' ;
17
17
import { GridClasses } from './Grid.jss' ;
18
18
@@ -61,32 +61,29 @@ export interface GridPropTypes extends CommonProps {
61
61
children : ReactNode | ReactNodeArray ;
62
62
}
63
63
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;
66
66
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 ) ;
75
78
76
- const getSpanFromString = ( span ) => {
77
- const currentSpan = getCurrentSpan ( ) ;
79
+ const getSpanFromString = ( span , currentRange ) => {
78
80
const spanConfig = SPAN_PATTERN . exec ( span ) ;
79
- return spanConfig [ currentSpan ]
80
- ? parseInt ( spanConfig [ currentSpan ] . replace ( / [ X L M S ] { 0 , 2 } / g, '' ) , 10 )
81
- : [ undefined , 3 , 3 , 6 , 12 ] [ currentSpan ] ;
81
+ return spanConfig . groups [ currentRange ] ?? DefaultSpanMap . get ( currentRange ) ;
82
82
} ;
83
83
84
- const getIndentFromString = ( indent ) => {
85
- const currentSpan = getCurrentSpan ( ) ;
84
+ const getIndentFromString = ( indent , currentRange ) => {
86
85
const indentConfig = INDENT_PATTERN . exec ( indent ) ;
87
- return indentConfig [ currentSpan ]
88
- ? parseInt ( indentConfig [ currentSpan ] . replace ( / [ X L M S ] { 0 , 2 } / g, '' ) , 10 )
89
- : [ undefined , 0 , 0 , 0 , 0 ] [ currentSpan ] ;
86
+ return indentConfig . groups [ currentRange ] ?? DefaultIndentMap . get ( currentRange ) ;
90
87
} ;
91
88
92
89
const useStyles = createComponentStyles ( GridClasses , { name : 'Grid' } ) ;
@@ -114,6 +111,8 @@ const Grid: FC<GridPropTypes> = forwardRef((props: GridPropTypes, ref: Ref<HTMLD
114
111
gridClasses . put ( classes [ `gridHSpace${ hSpacing === 0.5 ? '05' : hSpacing } ` ] ) ;
115
112
gridClasses . put ( classes [ `gridVSpace${ vSpacing === 0.5 ? '05' : vSpacing } ` ] ) ;
116
113
114
+ const currentRange = useViewportRange ( 'StdExt' ) ;
115
+
117
116
if ( GridPosition . Center === position ) {
118
117
gridClasses . put ( classes . gridPositionCenter ) ;
119
118
}
@@ -142,22 +141,19 @@ const Grid: FC<GridPropTypes> = forwardRef((props: GridPropTypes, ref: Ref<HTMLD
142
141
gridClasses . put ( className ) ;
143
142
}
144
143
145
- useViewportRange ( 'StdExt' ) ;
146
-
147
144
const renderGridElements = ( child : ReactElement < any > ) => {
148
- const span = getSpanFromString ( defaultSpan ) ;
149
- const indentSpan = getIndentFromString ( defaultIndent ) ;
150
-
151
145
const gridSpanClasses = StyleClassHelper . of ( classes . gridSpan ) ;
152
146
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 ) ;
154
148
gridSpanClasses . put ( classes [ `gridSpan${ childSpan } ` ] ) ;
155
149
} else {
150
+ const span = getSpanFromString ( defaultSpan , currentRange ) ;
156
151
gridSpanClasses . put ( classes [ `gridSpan${ span } ` ] ) ;
157
152
}
158
153
154
+ const indentSpan = getIndentFromString ( defaultIndent , currentRange ) ;
159
155
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 ) ;
161
157
if ( childIndent && childIndent > 0 ) {
162
158
gridSpanClasses . put ( classes [ `gridIndent${ childIndent } ` ] ) ;
163
159
}
0 commit comments