mirror of
https://github.com/status-im/react-native.git
synced 2025-01-16 12:34:17 +00:00
b672294858
Summary: public The +[RCTConvert UIImage:] function, while convenient, is inherently limited by being synchronous, which means that it cannot be used to load remote images, and may not be efficient for local images either. It's also unable to access the bridge, which means that it cannot take advantage of the modular image-loading pipeline. This diff introduces a new RCTImageSource class which can be used to pass image source objects over the bridge and defer loading until later. I've also added automatic application of the `resolveAssetSource()` function based on prop type, and fixed up the image logic in NavigatorIOS and TabBarIOS. Reviewed By: javache Differential Revision: D2631541 fb-gh-sync-id: 6604635e8bb5394425102487f1ee7cd729321877
123 lines
3.9 KiB
JavaScript
123 lines
3.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule requireNativeComponent
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
|
|
var UIManager = require('UIManager');
|
|
var UnimplementedView = require('UnimplementedView');
|
|
|
|
var createReactNativeComponentClass = require('createReactNativeComponentClass');
|
|
|
|
var insetsDiffer = require('insetsDiffer');
|
|
var pointsDiffer = require('pointsDiffer');
|
|
var matricesDiffer = require('matricesDiffer');
|
|
var processColor = require('processColor');
|
|
var resolveAssetSource = require('resolveAssetSource');
|
|
var sizesDiffer = require('sizesDiffer');
|
|
var verifyPropTypes = require('verifyPropTypes');
|
|
var warning = require('warning');
|
|
|
|
/**
|
|
* Used to create React components that directly wrap native component
|
|
* implementations. Config information is extracted from data exported from the
|
|
* UIManager module. You should also wrap the native component in a
|
|
* hand-written component with full propTypes definitions and other
|
|
* documentation - pass the hand-written component in as `componentInterface` to
|
|
* verify all the native props are documented via `propTypes`.
|
|
*
|
|
* If some native props shouldn't be exposed in the wrapper interface, you can
|
|
* pass null for `componentInterface` and call `verifyPropTypes` directly
|
|
* with `nativePropsToIgnore`;
|
|
*
|
|
* Common types are lined up with the appropriate prop differs with
|
|
* `TypeToDifferMap`. Non-scalar types not in the map default to `deepDiffer`.
|
|
*/
|
|
import type { ComponentInterface } from 'verifyPropTypes';
|
|
|
|
function requireNativeComponent(
|
|
viewName: string,
|
|
componentInterface?: ?ComponentInterface,
|
|
extraConfig?: ?{nativeOnly?: Object},
|
|
): Function {
|
|
var viewConfig = UIManager[viewName];
|
|
if (!viewConfig || !viewConfig.NativeProps) {
|
|
warning(false, 'Native component for "%s" does not exist', viewName);
|
|
return UnimplementedView;
|
|
}
|
|
var nativeProps = {
|
|
...UIManager.RCTView.NativeProps,
|
|
...viewConfig.NativeProps,
|
|
};
|
|
viewConfig.uiViewClassName = viewName;
|
|
viewConfig.validAttributes = {};
|
|
viewConfig.propTypes = componentInterface && componentInterface.propTypes;
|
|
for (var key in nativeProps) {
|
|
var useAttribute = false;
|
|
var attribute = {};
|
|
|
|
var differ = TypeToDifferMap[nativeProps[key]];
|
|
if (differ) {
|
|
attribute.diff = differ;
|
|
useAttribute = true;
|
|
}
|
|
|
|
var processor = TypeToProcessorMap[nativeProps[key]];
|
|
if (processor) {
|
|
attribute.process = processor;
|
|
useAttribute = true;
|
|
}
|
|
|
|
viewConfig.validAttributes[key] = useAttribute ? attribute : true;
|
|
}
|
|
|
|
// Unfortunately, the current set up puts the style properties on the top
|
|
// level props object. We also need to add the nested form for API
|
|
// compatibility. This allows these props on both the top level and the
|
|
// nested style level. TODO: Move these to nested declarations on the
|
|
// native side.
|
|
viewConfig.validAttributes.style = ReactNativeStyleAttributes;
|
|
|
|
if (__DEV__) {
|
|
componentInterface && verifyPropTypes(
|
|
componentInterface,
|
|
viewConfig,
|
|
extraConfig && extraConfig.nativeOnly
|
|
);
|
|
}
|
|
return createReactNativeComponentClass(viewConfig);
|
|
}
|
|
|
|
var TypeToDifferMap = {
|
|
// iOS Types
|
|
CATransform3D: matricesDiffer,
|
|
CGPoint: pointsDiffer,
|
|
CGSize: sizesDiffer,
|
|
UIEdgeInsets: insetsDiffer,
|
|
// Android Types
|
|
// (not yet implemented)
|
|
};
|
|
|
|
var TypeToProcessorMap = {
|
|
// iOS Types
|
|
CGColor: processColor,
|
|
CGColorArray: processColor,
|
|
UIColor: processColor,
|
|
UIColorArray: processColor,
|
|
CGImage: resolveAssetSource,
|
|
UIImage: resolveAssetSource,
|
|
RCTImageSource: resolveAssetSource,
|
|
// Android Types
|
|
Color: processColor,
|
|
};
|
|
|
|
module.exports = requireNativeComponent;
|