123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /**
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- *
- * @flow
- * @format
- */
- 'use strict';
- const Image = require('./Image');
- const React = require('react');
- const StyleSheet = require('../StyleSheet/StyleSheet');
- const View = require('../Components/View/View');
- /**
- * Very simple drop-in replacement for <Image> which supports nesting views.
- *
- * ```ReactNativeWebPlayer
- * import React, { Component } from 'react';
- * import { AppRegistry, View, ImageBackground, Text } from 'react-native';
- *
- * class DisplayAnImageBackground extends Component {
- * render() {
- * return (
- * <ImageBackground
- * style={{width: 50, height: 50}}
- * source={{uri: 'https://reactnative.dev/img/opengraph.png'}}
- * >
- * <Text>React</Text>
- * </ImageBackground>
- * );
- * }
- * }
- *
- * // App registration and rendering
- * AppRegistry.registerComponent('DisplayAnImageBackground', () => DisplayAnImageBackground);
- * ```
- */
- class ImageBackground extends React.Component<$FlowFixMeProps> {
- setNativeProps(props: Object) {
- // Work-around flow
- const viewRef = this._viewRef;
- if (viewRef) {
- viewRef.setNativeProps(props);
- }
- }
- _viewRef: ?React.ElementRef<typeof View> = null;
- _captureRef = ref => {
- this._viewRef = ref;
- };
- render(): React.Node {
- const {children, style, imageStyle, imageRef, ...props} = this.props;
- return (
- <View
- accessibilityIgnoresInvertColors={true}
- style={style}
- ref={this._captureRef}>
- <Image
- {...props}
- style={[
- StyleSheet.absoluteFill,
- {
- // Temporary Workaround:
- // Current (imperfect yet) implementation of <Image> overwrites width and height styles
- // (which is not quite correct), and these styles conflict with explicitly set styles
- // of <ImageBackground> and with our internal layout model here.
- // 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,
- },
- imageStyle,
- ]}
- ref={imageRef}
- />
- {children}
- </View>
- );
- }
- }
- module.exports = ImageBackground;
|