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';
|
|
|
|
|
2016-10-27 13:54:26 +00:00
|
|
|
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
|
|
|
|
const UIManager = require('UIManager');
|
|
|
|
const UnimplementedView = require('UnimplementedView');
|
|
|
|
|
2016-11-04 12:40:26 +00:00
|
|
|
const createReactNativeComponentClass = require('createReactNativeComponentClass');
|
2016-10-27 13:54:26 +00:00
|
|
|
const insetsDiffer = require('insetsDiffer');
|
|
|
|
const matricesDiffer = require('matricesDiffer');
|
|
|
|
const pointsDiffer = require('pointsDiffer');
|
|
|
|
const processColor = require('processColor');
|
|
|
|
const resolveAssetSource = require('resolveAssetSource');
|
|
|
|
const sizesDiffer = require('sizesDiffer');
|
|
|
|
const verifyPropTypes = require('verifyPropTypes');
|
|
|
|
const warning = require('fbjs/lib/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-11-27 13:39:00 +00:00
|
|
|
* UIManager module. You should also wrap the native component in a
|
2015-04-17 01:17:19 +00:00
|
|
|
* hand-written component with full propTypes definitions and other
|
2015-07-25 01:31:55 +00:00
|
|
|
* documentation - pass the hand-written component in as `componentInterface` to
|
2015-04-17 01:17:19 +00:00
|
|
|
* verify all the native props are documented via `propTypes`.
|
|
|
|
*
|
|
|
|
* If some native props shouldn't be exposed in the wrapper interface, you can
|
2015-07-25 01:31:55 +00:00
|
|
|
* pass null for `componentInterface` and call `verifyPropTypes` directly
|
2015-04-17 01:17:19 +00:00
|
|
|
* 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-07-25 01:31:55 +00:00
|
|
|
import type { ComponentInterface } from 'verifyPropTypes';
|
|
|
|
|
2015-04-17 01:17:19 +00:00
|
|
|
function requireNativeComponent(
|
|
|
|
viewName: string,
|
2015-07-25 01:31:55 +00:00
|
|
|
componentInterface?: ?ComponentInterface,
|
|
|
|
extraConfig?: ?{nativeOnly?: Object},
|
2017-03-20 19:58:08 +00:00
|
|
|
): ReactClass<any> | string {
|
2016-10-27 13:54:26 +00:00
|
|
|
const viewConfig = UIManager[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;
|
|
|
|
}
|
2016-10-27 13:54:26 +00:00
|
|
|
|
2015-04-22 11:03:46 +00:00
|
|
|
viewConfig.uiViewClassName = viewName;
|
2015-04-17 00:14:11 +00:00
|
|
|
viewConfig.validAttributes = {};
|
2017-03-28 18:17:21 +00:00
|
|
|
|
|
|
|
// ReactNative `View.propTypes` have been deprecated in favor of
|
|
|
|
// `ViewPropTypes`. In their place a temporary getter has been added with a
|
|
|
|
// deprecated warning message. Avoid triggering that warning here by using
|
|
|
|
// temporary workaround, __propTypesSecretDontUseThesePlease.
|
|
|
|
// TODO (bvaughn) Revert this particular change any time after April 1
|
|
|
|
if (componentInterface) {
|
|
|
|
viewConfig.propTypes =
|
|
|
|
typeof componentInterface.__propTypesSecretDontUseThesePlease === 'object'
|
|
|
|
? componentInterface.__propTypesSecretDontUseThesePlease
|
|
|
|
: componentInterface.propTypes;
|
|
|
|
} else {
|
|
|
|
viewConfig.propTypes = null;
|
|
|
|
}
|
2015-09-17 15:36:08 +00:00
|
|
|
|
2016-10-27 13:54:26 +00:00
|
|
|
// The ViewConfig doesn't contain any props inherited from the view manager's
|
|
|
|
// superclass, so we manually merge in the RCTView ones. Other inheritance
|
|
|
|
// patterns are currenty not supported.
|
|
|
|
const nativeProps = {
|
|
|
|
...UIManager.RCTView.NativeProps,
|
|
|
|
...viewConfig.NativeProps,
|
|
|
|
};
|
|
|
|
for (const key in nativeProps) {
|
|
|
|
let useAttribute = false;
|
|
|
|
const attribute = {};
|
|
|
|
|
|
|
|
const differ = TypeToDifferMap[nativeProps[key]];
|
2015-09-17 15:36:08 +00:00
|
|
|
if (differ) {
|
|
|
|
attribute.diff = differ;
|
|
|
|
useAttribute = true;
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:54:26 +00:00
|
|
|
const processor = TypeToProcessorMap[nativeProps[key]];
|
2015-09-17 15:36:08 +00:00
|
|
|
if (processor) {
|
|
|
|
attribute.process = processor;
|
|
|
|
useAttribute = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
viewConfig.validAttributes[key] = useAttribute ? attribute : true;
|
2015-04-17 00:14:11 +00:00
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2015-04-17 01:17:19 +00:00
|
|
|
if (__DEV__) {
|
2015-07-25 01:31:55 +00:00
|
|
|
componentInterface && verifyPropTypes(
|
|
|
|
componentInterface,
|
|
|
|
viewConfig,
|
|
|
|
extraConfig && extraConfig.nativeOnly
|
|
|
|
);
|
2015-04-17 01:17:19 +00:00
|
|
|
}
|
2016-10-27 13:54:26 +00:00
|
|
|
|
2015-05-08 16:45:43 +00:00
|
|
|
return createReactNativeComponentClass(viewConfig);
|
2015-04-17 00:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:30:05 +00:00
|
|
|
const TypeToDifferMap = {
|
2015-04-17 00:14:11 +00:00
|
|
|
// iOS Types
|
|
|
|
CATransform3D: matricesDiffer,
|
|
|
|
CGPoint: pointsDiffer,
|
|
|
|
CGSize: sizesDiffer,
|
|
|
|
UIEdgeInsets: insetsDiffer,
|
|
|
|
// Android Types
|
|
|
|
// (not yet implemented)
|
|
|
|
};
|
|
|
|
|
2017-01-31 21:00:12 +00:00
|
|
|
function processColorArray(colors: ?Array<any>): ?Array<?number> {
|
Reimplement color processing
Summary:
**Problem:**
As I was trying to document what color formats we supported, I realized that our current implementation based on the open source project tinycolor supported some crazy things. A few examples that were all valid:
```
tinycolor('abc')
tinycolor(' #abc ')
tinycolor('##abc')
tinycolor('rgb 255 0 0')
tinycolor('RGBA(0, 1, 2)')
tinycolor('rgb (0, 1, 2)')
tinycolor('hsv(0, 1, 2)')
tinycolor({r: 10, g: 10, b: 10})
tinycolor('hsl(1%, 2, 3)')
tinycolor('rgb(1.0, 2.0, 3.0)')
tinycolor('rgb(1%, 2%, 3%)')
```
The integrations of tinycolor were also really bad. processColor added "support" for pure numbers and an array of colors!?? ColorPropTypes did some crazy trim().toString() and repeated a bad error message twice.
**Solution:**
While iteratively cleaning the file, I eventually ended up reimplementing it entierly. Major changes are:
- The API is now dead simple: returns null if it doesn't parse or returns the int32 representation of the color
- Stricter parsing of at
Closes https://github.com/facebook/react-native/pull/5529
Reviewed By: svcscm
Differential Revision: D2872015
Pulled By: nicklockwood
fb-gh-sync-id: df78244eefce6cf8e8ed2ea51f58d6b232de16f9
2016-01-29 17:11:53 +00:00
|
|
|
return colors && colors.map(processColor);
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:30:05 +00:00
|
|
|
const TypeToProcessorMap = {
|
2015-09-17 15:36:08 +00:00
|
|
|
// iOS Types
|
|
|
|
CGColor: processColor,
|
Reimplement color processing
Summary:
**Problem:**
As I was trying to document what color formats we supported, I realized that our current implementation based on the open source project tinycolor supported some crazy things. A few examples that were all valid:
```
tinycolor('abc')
tinycolor(' #abc ')
tinycolor('##abc')
tinycolor('rgb 255 0 0')
tinycolor('RGBA(0, 1, 2)')
tinycolor('rgb (0, 1, 2)')
tinycolor('hsv(0, 1, 2)')
tinycolor({r: 10, g: 10, b: 10})
tinycolor('hsl(1%, 2, 3)')
tinycolor('rgb(1.0, 2.0, 3.0)')
tinycolor('rgb(1%, 2%, 3%)')
```
The integrations of tinycolor were also really bad. processColor added "support" for pure numbers and an array of colors!?? ColorPropTypes did some crazy trim().toString() and repeated a bad error message twice.
**Solution:**
While iteratively cleaning the file, I eventually ended up reimplementing it entierly. Major changes are:
- The API is now dead simple: returns null if it doesn't parse or returns the int32 representation of the color
- Stricter parsing of at
Closes https://github.com/facebook/react-native/pull/5529
Reviewed By: svcscm
Differential Revision: D2872015
Pulled By: nicklockwood
fb-gh-sync-id: df78244eefce6cf8e8ed2ea51f58d6b232de16f9
2016-01-29 17:11:53 +00:00
|
|
|
CGColorArray: processColorArray,
|
2015-09-17 15:36:08 +00:00
|
|
|
UIColor: processColor,
|
Reimplement color processing
Summary:
**Problem:**
As I was trying to document what color formats we supported, I realized that our current implementation based on the open source project tinycolor supported some crazy things. A few examples that were all valid:
```
tinycolor('abc')
tinycolor(' #abc ')
tinycolor('##abc')
tinycolor('rgb 255 0 0')
tinycolor('RGBA(0, 1, 2)')
tinycolor('rgb (0, 1, 2)')
tinycolor('hsv(0, 1, 2)')
tinycolor({r: 10, g: 10, b: 10})
tinycolor('hsl(1%, 2, 3)')
tinycolor('rgb(1.0, 2.0, 3.0)')
tinycolor('rgb(1%, 2%, 3%)')
```
The integrations of tinycolor were also really bad. processColor added "support" for pure numbers and an array of colors!?? ColorPropTypes did some crazy trim().toString() and repeated a bad error message twice.
**Solution:**
While iteratively cleaning the file, I eventually ended up reimplementing it entierly. Major changes are:
- The API is now dead simple: returns null if it doesn't parse or returns the int32 representation of the color
- Stricter parsing of at
Closes https://github.com/facebook/react-native/pull/5529
Reviewed By: svcscm
Differential Revision: D2872015
Pulled By: nicklockwood
fb-gh-sync-id: df78244eefce6cf8e8ed2ea51f58d6b232de16f9
2016-01-29 17:11:53 +00:00
|
|
|
UIColorArray: processColorArray,
|
2015-12-08 11:29:08 +00:00
|
|
|
CGImage: resolveAssetSource,
|
|
|
|
UIImage: resolveAssetSource,
|
|
|
|
RCTImageSource: resolveAssetSource,
|
2015-09-17 15:36:08 +00:00
|
|
|
// Android Types
|
|
|
|
Color: processColor,
|
2016-02-02 15:11:41 +00:00
|
|
|
ColorArray: processColorArray,
|
2015-09-17 15:36:08 +00:00
|
|
|
};
|
|
|
|
|
2015-04-17 00:14:11 +00:00
|
|
|
module.exports = requireNativeComponent;
|