2015-04-17 00:14:11 +00:00
|
|
|
/**
|
|
|
|
* 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 RCTUIManager = require('NativeModules').UIManager;
|
|
|
|
var UnimplementedView = require('UnimplementedView');
|
|
|
|
|
2015-05-08 16:45:43 +00:00
|
|
|
var createReactNativeComponentClass = require('createReactNativeComponentClass');
|
2015-04-17 00:14:11 +00:00
|
|
|
var deepDiffer = require('deepDiffer');
|
|
|
|
var insetsDiffer = require('insetsDiffer');
|
|
|
|
var pointsDiffer = require('pointsDiffer');
|
|
|
|
var matricesDiffer = require('matricesDiffer');
|
|
|
|
var sizesDiffer = require('sizesDiffer');
|
2015-04-17 01:17:19 +00:00
|
|
|
var verifyPropTypes = require('verifyPropTypes');
|
2015-05-22 23:16:47 +00:00
|
|
|
var warning = require('warning');
|
2015-04-17 00:14:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to create React components that directly wrap native component
|
|
|
|
* implementations. Config information is extracted from data exported from the
|
2015-04-17 01:17:19 +00:00
|
|
|
* RCTUIManager 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 `wrapperComponent` 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 `wrapperComponent` and call `verifyPropTypes` directly
|
|
|
|
* with `nativePropsToIgnore`;
|
2015-04-17 00:14:11 +00:00
|
|
|
*
|
|
|
|
* Common types are lined up with the appropriate prop differs with
|
|
|
|
* `TypeToDifferMap`. Non-scalar types not in the map default to `deepDiffer`.
|
|
|
|
*/
|
2015-04-17 01:17:19 +00:00
|
|
|
function requireNativeComponent(
|
|
|
|
viewName: string,
|
|
|
|
wrapperComponent: ?Function
|
|
|
|
): Function {
|
2015-04-22 11:03:46 +00:00
|
|
|
var viewConfig = RCTUIManager[viewName];
|
2015-05-27 02:25:11 +00:00
|
|
|
if (!viewConfig || !viewConfig.NativeProps) {
|
2015-05-22 23:16:47 +00:00
|
|
|
warning(false, 'Native component for "%s" does not exist', viewName);
|
2015-04-17 00:14:11 +00:00
|
|
|
return UnimplementedView;
|
|
|
|
}
|
|
|
|
var nativeProps = {
|
2015-05-27 02:25:11 +00:00
|
|
|
...RCTUIManager.RCTView.NativeProps,
|
|
|
|
...viewConfig.NativeProps,
|
2015-04-17 00:14:11 +00:00
|
|
|
};
|
2015-04-22 11:03:46 +00:00
|
|
|
viewConfig.uiViewClassName = viewName;
|
2015-04-17 00:14:11 +00:00
|
|
|
viewConfig.validAttributes = {};
|
|
|
|
for (var key in nativeProps) {
|
|
|
|
// TODO: deep diff by default in diffRawProperties instead of setting it here
|
2015-04-22 11:03:46 +00:00
|
|
|
var differ = TypeToDifferMap[nativeProps[key]] || deepDiffer;
|
2015-04-17 00:14:11 +00:00
|
|
|
viewConfig.validAttributes[key] = {diff: differ};
|
|
|
|
}
|
2015-04-17 01:17:19 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
wrapperComponent && verifyPropTypes(wrapperComponent, viewConfig);
|
|
|
|
}
|
2015-05-08 16:45:43 +00:00
|
|
|
return createReactNativeComponentClass(viewConfig);
|
2015-04-17 00:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var TypeToDifferMap = {
|
|
|
|
// iOS Types
|
|
|
|
CATransform3D: matricesDiffer,
|
|
|
|
CGPoint: pointsDiffer,
|
|
|
|
CGSize: sizesDiffer,
|
|
|
|
UIEdgeInsets: insetsDiffer,
|
|
|
|
// Android Types
|
|
|
|
// (not yet implemented)
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = requireNativeComponent;
|