1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _prodInvariant = require('./reactProdInvariant'),
|
12 | _assign = require('object-assign');
|
13 |
|
14 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
15 |
|
16 | var React = require('react/lib/React');
|
17 | var ReactCompositeComponent = require('./ReactCompositeComponent');
|
18 | var ReactDefaultBatchingStrategy = require('./ReactDefaultBatchingStrategy');
|
19 | var ReactReconciler = require('./ReactReconciler');
|
20 | var ReactReconcileTransaction = require('./ReactReconcileTransaction');
|
21 | var ReactUpdates = require('./ReactUpdates');
|
22 |
|
23 | var emptyObject = require('fbjs/lib/emptyObject');
|
24 | var getNextDebugID = require('react/lib/getNextDebugID');
|
25 | var invariant = require('fbjs/lib/invariant');
|
26 |
|
27 | function injectDefaults() {
|
28 | ReactUpdates.injection.injectReconcileTransaction(ReactReconcileTransaction);
|
29 | ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
|
30 | }
|
31 |
|
32 | var NoopInternalComponent = function () {
|
33 | function NoopInternalComponent(element) {
|
34 | _classCallCheck(this, NoopInternalComponent);
|
35 |
|
36 | this._renderedOutput = element;
|
37 | this._currentElement = element;
|
38 |
|
39 | if (process.env.NODE_ENV !== 'production') {
|
40 | this._debugID = getNextDebugID();
|
41 | }
|
42 | }
|
43 |
|
44 | NoopInternalComponent.prototype.mountComponent = function mountComponent() {};
|
45 |
|
46 | NoopInternalComponent.prototype.receiveComponent = function receiveComponent(element) {
|
47 | this._renderedOutput = element;
|
48 | this._currentElement = element;
|
49 | };
|
50 |
|
51 | NoopInternalComponent.prototype.unmountComponent = function unmountComponent() {};
|
52 |
|
53 | NoopInternalComponent.prototype.getHostNode = function getHostNode() {
|
54 | return undefined;
|
55 | };
|
56 |
|
57 | NoopInternalComponent.prototype.getPublicInstance = function getPublicInstance() {
|
58 | return null;
|
59 | };
|
60 |
|
61 | return NoopInternalComponent;
|
62 | }();
|
63 |
|
64 | var ShallowComponentWrapper = function (element) {
|
65 |
|
66 | if (process.env.NODE_ENV !== 'production') {
|
67 | this._debugID = getNextDebugID();
|
68 | }
|
69 |
|
70 | this.construct(element);
|
71 | };
|
72 | _assign(ShallowComponentWrapper.prototype, ReactCompositeComponent, {
|
73 | _constructComponent: ReactCompositeComponent._constructComponentWithoutOwner,
|
74 | _instantiateReactComponent: function (element) {
|
75 | return new NoopInternalComponent(element);
|
76 | },
|
77 | _replaceNodeWithMarkup: function () {},
|
78 | _renderValidatedComponent: ReactCompositeComponent._renderValidatedComponentWithoutOwnerOrContext
|
79 | });
|
80 |
|
81 | function _batchedRender(renderer, element, context) {
|
82 | var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
|
83 | renderer._render(element, transaction, context);
|
84 | ReactUpdates.ReactReconcileTransaction.release(transaction);
|
85 | }
|
86 |
|
87 | var ReactShallowRenderer = function () {
|
88 | function ReactShallowRenderer() {
|
89 | _classCallCheck(this, ReactShallowRenderer);
|
90 |
|
91 | this._instance = null;
|
92 | }
|
93 |
|
94 | ReactShallowRenderer.prototype.getMountedInstance = function getMountedInstance() {
|
95 | return this._instance ? this._instance._instance : null;
|
96 | };
|
97 |
|
98 | ReactShallowRenderer.prototype.render = function render(element, context) {
|
99 |
|
100 |
|
101 |
|
102 | injectDefaults();
|
103 |
|
104 | !React.isValidElement(element) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactShallowRenderer render(): Invalid component element.%s', typeof element === 'function' ? ' Instead of passing a component class, make sure to instantiate ' + 'it by passing it to React.createElement.' : '') : _prodInvariant('12', typeof element === 'function' ? ' Instead of passing a component class, make sure to instantiate ' + 'it by passing it to React.createElement.' : '') : void 0;
|
105 | !(typeof element.type !== 'string') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactShallowRenderer render(): Shallow rendering works only with custom components, not primitives (%s). Instead of calling `.render(el)` and inspecting the rendered output, look at `el.props` directly instead.', element.type) : _prodInvariant('13', element.type) : void 0;
|
106 |
|
107 | if (!context) {
|
108 | context = emptyObject;
|
109 | }
|
110 | ReactUpdates.batchedUpdates(_batchedRender, this, element, context);
|
111 |
|
112 | return this.getRenderOutput();
|
113 | };
|
114 |
|
115 | ReactShallowRenderer.prototype.getRenderOutput = function getRenderOutput() {
|
116 | return this._instance && this._instance._renderedComponent && this._instance._renderedComponent._renderedOutput || null;
|
117 | };
|
118 |
|
119 | ReactShallowRenderer.prototype.unmount = function unmount() {
|
120 | if (this._instance) {
|
121 | ReactReconciler.unmountComponent(this._instance, false);
|
122 | }
|
123 | };
|
124 |
|
125 | ReactShallowRenderer.prototype.unstable_batchedUpdates = function unstable_batchedUpdates(callback, bookkeeping) {
|
126 |
|
127 | injectDefaults();
|
128 | return ReactUpdates.batchedUpdates(callback, bookkeeping);
|
129 | };
|
130 |
|
131 | ReactShallowRenderer.prototype._render = function _render(element, transaction, context) {
|
132 | if (this._instance) {
|
133 | ReactReconciler.receiveComponent(this._instance, element, transaction, context);
|
134 | } else {
|
135 | var instance = new ShallowComponentWrapper(element);
|
136 | ReactReconciler.mountComponent(instance, transaction, null, null, context, 0);
|
137 | this._instance = instance;
|
138 | }
|
139 | };
|
140 |
|
141 | return ReactShallowRenderer;
|
142 | }();
|
143 |
|
144 | ReactShallowRenderer.createRenderer = function () {
|
145 | return new ReactShallowRenderer();
|
146 | };
|
147 |
|
148 | module.exports = ReactShallowRenderer; |
\ | No newline at end of file |