RN: Replace context.isInAParentText w/ React.createContext

Reviewed By: sahrens

Differential Revision: D7895382

fbshipit-source-id: 4affcecd147b8e8c506e0d94f223bac3e6dfdf66
This commit is contained in:
Tim Yung
2018-05-09 00:47:46 -07:00
committed by Facebook Github Bot
parent 5d4c542c58
commit e1339bc183
5 changed files with 86 additions and 96 deletions

View File

@@ -7,6 +7,7 @@
* @flow
* @format
*/
'use strict';
var ImageResizeMode = require('ImageResizeMode');
@@ -18,6 +19,7 @@ var PropTypes = require('prop-types');
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
var StyleSheet = require('StyleSheet');
var StyleSheetPropType = require('StyleSheetPropType');
const TextAncestor = require('TextAncestor');
var ViewPropTypes = require('ViewPropTypes');
var createReactClass = require('create-react-class');
@@ -26,8 +28,6 @@ var merge = require('merge');
var requireNativeComponent = require('requireNativeComponent');
var resolveAssetSource = require('resolveAssetSource');
const {ViewContextTypes} = require('ViewContext');
var {ImageLoader} = NativeModules;
let _requestId = 1;
@@ -202,8 +202,6 @@ var Image = createReactClass({
validAttributes: ReactNativeViewAttributes.RCTView,
},
contextTypes: ViewContextTypes,
render: function() {
const source = resolveAssetSource(this.props.source);
const defaultSource = resolveAssetSource(this.props.defaultSource);
@@ -236,42 +234,44 @@ var Image = createReactClass({
);
}
if (source && (source.uri || Array.isArray(source))) {
let style;
let sources;
if (source.uri) {
const {width, height} = source;
style = flattenStyle([{width, height}, styles.base, this.props.style]);
sources = [{uri: source.uri}];
} else {
style = flattenStyle([styles.base, this.props.style]);
sources = source;
}
const {onLoadStart, onLoad, onLoadEnd, onError} = this.props;
const nativeProps = merge(this.props, {
style,
shouldNotifyLoadEvents: !!(
onLoadStart ||
onLoad ||
onLoadEnd ||
onError
),
src: sources,
headers: source.headers,
defaultSrc: defaultSource ? defaultSource.uri : null,
loadingIndicatorSrc: loadingIndicatorSource
? loadingIndicatorSource.uri
: null,
});
if (this.context.isInAParentText) {
return <RCTTextInlineImage {...nativeProps} />;
} else {
return <RKImage {...nativeProps} />;
}
if (!source || (!source.uri && !Array.isArray(source))) {
return null;
}
return null;
let style;
let sources;
if (source.uri) {
const {width, height} = source;
style = flattenStyle([{width, height}, styles.base, this.props.style]);
sources = [{uri: source.uri}];
} else {
style = flattenStyle([styles.base, this.props.style]);
sources = source;
}
const {onLoadStart, onLoad, onLoadEnd, onError} = this.props;
const nativeProps = merge(this.props, {
style,
shouldNotifyLoadEvents: !!(onLoadStart || onLoad || onLoadEnd || onError),
src: sources,
headers: source.headers,
defaultSrc: defaultSource ? defaultSource.uri : null,
loadingIndicatorSrc: loadingIndicatorSource
? loadingIndicatorSource.uri
: null,
});
return (
<TextAncestor.Consumer>
{hasTextAncestor =>
hasTextAncestor ? (
<RCTTextInlineImage {...nativeProps} />
) : (
<RKImage {...nativeProps} />
)
}
</TextAncestor.Consumer>
);
},
});