Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactNativeComponent
10
*/
11
12
"use strict";
13
14
var assign = require('Object.assign');
15
var invariant = require('invariant');
16
17
var genericComponentClass = null;
18
// This registry keeps track of wrapper classes around native tags
19
var tagToComponentClass = {};
20
21
var ReactNativeComponentInjection = {
22
// This accepts a class that receives the tag string. This is a catch all
23
// that can render any kind of tag.
24
injectGenericComponentClass: function(componentClass) {
25
genericComponentClass = componentClass;
26
},
27
// This accepts a keyed object with classes as values. Each key represents a
28
// tag. That particular tag will use this class instead of the generic one.
29
injectComponentClasses: function(componentClasses) {
30
assign(tagToComponentClass, componentClasses);
31
}
32
};
33
34
/**
35
* Create an internal class for a specific tag.
36
*
37
* @param {string} tag The tag for which to create an internal instance.
38
* @param {any} props The props passed to the instance constructor.
39
* @return {ReactComponent} component The injected empty component.
40
*/
41
function createInstanceForTag(tag, props, parentType) {
42
var componentClass = tagToComponentClass[tag];
43
if (componentClass == null) {
44
invariant(
45
genericComponentClass,
46
'There is no registered component for the tag %s',
47
tag
48
);
49
return new genericComponentClass(tag, props);
50
}
51
if (parentType === tag) {
52
// Avoid recursion
53
invariant(
54
genericComponentClass,
55
'There is no registered component for the tag %s',
56
tag
57
);
58
return new genericComponentClass(tag, props);
59
}
60
// Unwrap legacy factories
61
return new componentClass.type(props);
62
}
63
64
var ReactNativeComponent = {
65
createInstanceForTag: createInstanceForTag,
66
injection: ReactNativeComponentInjection
67
};
68
69
module.exports = ReactNativeComponent;
70
71