Skip to content

Commit

Permalink
chore: add ImageBackgroundPropType (#32099)
Browse files Browse the repository at this point in the history
Summary:
Added FlowType definition for ImageBackground component Props

## Changelog

[General] [Changed] - Add ImageBackground component type definition
[General] [Fixed] - ImageBackground now respects `imageStyle` width and height

Pull Request resolved: #32099

Test Plan:
- [ ] flow check passes
- [ ] rn-tester app runs

Reviewed By: charlesbdudley

Differential Revision: D30597737

Pulled By: yungsters

fbshipit-source-id: 9699ee2bb727a1a8f30d6ffe3a2845c8a134e89d
  • Loading branch information
Naturalclar authored and facebook-github-bot committed Sep 23, 2021
1 parent 67b62ad commit dbd5c3d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Libraries/Image/ImageBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

'use strict';

const Image = require('./Image');
const React = require('react');
const StyleSheet = require('../StyleSheet/StyleSheet');
const View = require('../Components/View/View');
import Image from './Image';
import * as React from 'react';
import StyleSheet from '../StyleSheet/StyleSheet';
import flattenStyle from '../StyleSheet/flattenStyle';
import View from '../Components/View/View';
import type {ImageBackgroundProps} from './ImageProps';

/**
* Very simple drop-in replacement for <Image> which supports nesting views.
Expand All @@ -39,7 +41,7 @@ const View = require('../Components/View/View');
* AppRegistry.registerComponent('DisplayAnImageBackground', () => DisplayAnImageBackground);
* ```
*/
class ImageBackground extends React.Component<$FlowFixMeProps> {
class ImageBackground extends React.Component<ImageBackgroundProps> {
setNativeProps(props: Object) {
// Work-around flow
const viewRef = this._viewRef;
Expand All @@ -56,7 +58,7 @@ class ImageBackground extends React.Component<$FlowFixMeProps> {

render(): React.Node {
const {children, style, imageStyle, imageRef, ...props} = this.props;

const flattenedStyle = flattenStyle(style);
return (
<View
accessibilityIgnoresInvertColors={true}
Expand All @@ -74,8 +76,8 @@ class ImageBackground extends React.Component<$FlowFixMeProps> {
// So, we have to proxy/reapply these styles explicitly for actual <Image> component.
// This workaround should be removed after implementing proper support of
// intrinsic content size of the <Image>.
width: style?.width,
height: style?.height,
width: flattenedStyle?.width,
height: flattenedStyle?.height,
},
imageStyle,
]}
Expand Down
28 changes: 28 additions & 0 deletions Libraries/Image/ImageProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {EdgeInsetsProp} from '../StyleSheet/EdgeInsetsPropType';
import type {ImageSource} from './ImageSource';
import type {ViewStyleProp, ImageStyleProp} from '../StyleSheet/StyleSheet';
import type {ViewProps} from '../Components/View/ViewPropTypes';
import type {Node, Ref} from 'react';
import typeof Image from './Image';

export type ImageLoadEvent = SyntheticEvent<
$ReadOnly<{|
Expand Down Expand Up @@ -171,3 +173,29 @@ export type ImageProps = {|
src?: empty,
children?: empty,
|};

export type ImageBackgroundProps = $ReadOnly<{|
...ImageProps,
children?: Node,

/**
* Style applied to the outer View component
*
* See https://reactnative.dev/docs/imagebackground#style
*/
style?: ?ViewStyleProp,

/**
* Style applied to the inner Image component
*
* See https://reactnative.dev/docs/imagebackground#imagestyle
*/
imageStyle?: ?ImageStyleProp,

/**
* Allows to set a reference to the inner Image component
*
* See https://reactnative.dev/docs/imagebackground#imageref
*/
imageRef?: Ref<Image>,
|}>;

0 comments on commit dbd5c3d

Please sign in to comment.