@@ -20,13 +20,10 @@ import {
2020 InputContextConsumer
2121} from '../../utils/context' ;
2222
23- const ContainResizeMode = 'contain' ;
24-
2523export class Img extends React . Component {
2624
2725 static contextType = InputContext ;
2826
29-
3027 constructor ( props ) {
3128 super ( props ) ;
3229 this . payload = props . json ;
@@ -40,7 +37,9 @@ export class Img extends React.Component {
4037 }
4138
4239 componentDidMount ( ) {
43- this . addResourceInformation ( this . payload . url , "" ) ;
40+ if ( this . addResourceInformation ) {
41+ this . addResourceInformation ( this . payload . url , "" ) ;
42+ }
4443 }
4544
4645 /**
@@ -86,7 +85,7 @@ export class Img extends React.Component {
8685 getImageAlignment ( ) {
8786 let imageAlignmentStyle = [ ] ;
8887
89- switch ( this . payload . horizontalAlignment ) {
88+ switch ( this . payload ? .horizontalAlignment ?. toLowerCase ( ) ) {
9089 case Constants . CenterString :
9190 imageAlignmentStyle . push ( styles . centerAlignment ) ;
9291 break ;
@@ -131,10 +130,7 @@ export class Img extends React.Component {
131130 switch ( sizeValue ) {
132131 case 1 :
133132 {
134- sizeStyle . push ( [ styles . imageStretch ,
135- {
136- width : this . state . imageWidth , height : this . state . imageHeight
137- } ] ) ;
133+ sizeStyle . push ( styles . imageStretch ) ;
138134 break ;
139135 }
140136 case 2 :
@@ -175,6 +171,23 @@ export class Img extends React.Component {
175171 * the size of the image is taken as medium as default as per native iOS renderer.
176172 */
177173 sizeStyle . push ( styles . imageAuto ) ;
174+
175+ /**
176+ * If horizontal alignment exists for the image schema, we align the image accordingly
177+ * or we fallback to the default left alignment
178+ */
179+ switch ( this . payload ?. horizontalAlignment ?. toLowerCase ( ) ) {
180+ case Constants . CenterString :
181+ sizeStyle . push ( styles . imageCenterAlignment ) ;
182+ break ;
183+ case Constants . RightString :
184+ sizeStyle . push ( styles . imageRightAlignment ) ;
185+ break ;
186+ default :
187+ sizeStyle . push ( styles . imageLeftAlignment ) ;
188+ break ;
189+ }
190+
178191 if ( ( this . isSizeUndefined && this . payload . fromImageSet == true ) ||
179192 ( this . payload . fromImageSet == true ) ) {
180193 const spacingValue = Utils . parseHostConfigEnum (
@@ -247,6 +260,20 @@ export class Img extends React.Component {
247260 } ) ;
248261 }
249262
263+ getImageComponent ( imageUrl , wrapperComputedStyle , imageComputedStyle ) {
264+ return (
265+ < ElementWrapper configManager = { this . props . configManager } json = { this . payload } isFirst = { this . props . isFirst }
266+ style = { wrapperComputedStyle }
267+ onPageLayout = { this . onPageLayoutHandler } >
268+ < Image style = { imageComputedStyle }
269+ accessible = { true }
270+ accessibilityLabel = { this . payload . altText }
271+ resizeMode = { Constants . AlignStretch }
272+ source = { { uri : imageUrl } } />
273+ </ ElementWrapper >
274+ ) ;
275+ }
276+
250277 render ( ) {
251278 this . parseHostConfig ( ) ;
252279
@@ -285,15 +312,7 @@ export class Img extends React.Component {
285312 let containerContent = ( < InputContextConsumer >
286313 { ( { addResourceInformation } ) => {
287314 this . addResourceInformation = addResourceInformation ;
288- return < ElementWrapper configManager = { this . props . configManager } json = { this . payload } isFirst = { this . props . isFirst }
289- style = { wrapperComputedStyle }
290- onPageLayout = { this . onPageLayoutHandler } >
291-
292- < Image style = { imageComputedStyle }
293- accessible = { this . payload . altText ? true : false }
294- accessibilityLabel = { this . payload . altText }
295- source = { { uri : imageUrl } } />
296- </ ElementWrapper >
315+ return this . getImageComponent ( imageUrl , wrapperComputedStyle , imageComputedStyle ) ;
297316 } }
298317 </ InputContextConsumer > ) ;
299318
@@ -309,6 +328,15 @@ export class Img extends React.Component {
309328}
310329
311330const styles = StyleSheet . create ( {
331+ imageLeftAlignment : {
332+ alignSelf : Constants . FlexStart ,
333+ } ,
334+ imageCenterAlignment : {
335+ alignSelf : Constants . CenterString ,
336+ } ,
337+ imageRightAlignment : {
338+ alignSelf : Constants . FlexEnd ,
339+ } ,
312340 leftAlignment : {
313341 alignItems : Constants . FlexStart ,
314342 } ,
@@ -323,10 +351,13 @@ const styles = StyleSheet.create({
323351 } ,
324352 imageStretch : {
325353 alignSelf : Constants . AlignStretch ,
354+ aspectRatio : 1 ,
355+ height : Constants . Auto ,
326356 resizeMode : Constants . AlignStretch ,
357+ width : '100%' ,
327358 } ,
328359 imageAuto : {
329360 alignSelf : Constants . CenterString ,
330- resizeMode : ContainResizeMode ,
361+ resizeMode : Constants . AlignStretch ,
331362 } ,
332363} ) ;
0 commit comments