react 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -228
- package/ReactJSErrors.js +40 -0
- package/addons.js +4 -0
- package/lib/$.js +46 -0
- package/lib/CSSCore.js +114 -0
- package/lib/CSSProperty.js +90 -0
- package/lib/CSSPropertyOperations.js +97 -0
- package/lib/CallbackRegistry.js +91 -0
- package/lib/ChangeEventPlugin.js +365 -0
- package/lib/CompositionEventPlugin.js +212 -0
- package/lib/DOMChildrenOperations.js +135 -0
- package/lib/DOMProperty.js +266 -0
- package/lib/DOMPropertyOperations.js +168 -0
- package/lib/Danger.js +186 -0
- package/lib/DefaultDOMPropertyConfig.js +187 -0
- package/lib/DefaultEventPluginOrder.js +44 -0
- package/lib/EnterLeaveEventPlugin.js +112 -0
- package/lib/EventConstants.js +73 -0
- package/lib/EventListener.js +61 -0
- package/lib/EventPluginHub.js +190 -0
- package/lib/EventPluginRegistry.js +237 -0
- package/lib/EventPluginUtils.js +185 -0
- package/lib/EventPropagators.js +179 -0
- package/lib/ExecutionEnvironment.js +41 -0
- package/lib/LinkedStateMixin.js +46 -0
- package/lib/LinkedValueMixin.js +68 -0
- package/lib/MobileSafariClickEventPlugin.js +63 -0
- package/lib/PooledClass.js +113 -0
- package/lib/React.js +71 -0
- package/lib/ReactChildren.js +132 -0
- package/lib/ReactComponent.js +515 -0
- package/lib/ReactComponentBrowserEnvironment.js +140 -0
- package/lib/ReactComponentEnvironment.js +24 -0
- package/lib/ReactCompositeComponent.js +1020 -0
- package/lib/ReactCurrentOwner.js +39 -0
- package/lib/ReactDOM.js +194 -0
- package/lib/ReactDOMButton.js +64 -0
- package/lib/ReactDOMComponent.js +374 -0
- package/lib/ReactDOMForm.js +52 -0
- package/lib/ReactDOMIDOperations.js +173 -0
- package/lib/ReactDOMInput.js +169 -0
- package/lib/ReactDOMOption.js +50 -0
- package/lib/ReactDOMSelect.js +160 -0
- package/lib/ReactDOMSelection.js +189 -0
- package/lib/ReactDOMTextarea.js +136 -0
- package/lib/ReactDefaultBatchingStrategy.js +75 -0
- package/lib/ReactDefaultInjection.js +91 -0
- package/lib/ReactDefaultPerf.js +407 -0
- package/lib/ReactErrorUtils.js +46 -0
- package/lib/ReactEventEmitter.js +341 -0
- package/lib/ReactEventEmitterMixin.js +89 -0
- package/lib/ReactEventTopLevelCallback.js +89 -0
- package/lib/ReactInputSelection.js +140 -0
- package/lib/ReactInstanceHandles.js +322 -0
- package/lib/ReactLink.js +54 -0
- package/lib/ReactMarkupChecksum.js +53 -0
- package/lib/ReactMount.js +617 -0
- package/lib/ReactMountReady.js +95 -0
- package/lib/ReactMultiChild.js +441 -0
- package/lib/ReactMultiChildUpdateTypes.js +36 -0
- package/lib/ReactOwner.js +146 -0
- package/lib/ReactPerf.js +88 -0
- package/lib/ReactPropTransferer.js +128 -0
- package/lib/ReactPropTypes.js +158 -0
- package/lib/ReactReconcileTransaction.js +161 -0
- package/lib/ReactServerRendering.js +62 -0
- package/lib/ReactStateSetters.js +111 -0
- package/lib/ReactTextComponent.js +94 -0
- package/lib/ReactTransitionEvents.js +97 -0
- package/lib/ReactTransitionGroup.js +112 -0
- package/lib/ReactTransitionKeySet.js +111 -0
- package/lib/ReactTransitionableChild.js +152 -0
- package/lib/ReactUpdates.js +145 -0
- package/lib/ReactWithAddons.js +41 -0
- package/lib/SelectEventPlugin.js +217 -0
- package/lib/SimpleEventPlugin.js +365 -0
- package/lib/SyntheticClipboardEvent.js +45 -0
- package/lib/SyntheticCompositionEvent.js +51 -0
- package/lib/SyntheticEvent.js +163 -0
- package/lib/SyntheticFocusEvent.js +44 -0
- package/lib/SyntheticKeyboardEvent.js +56 -0
- package/lib/SyntheticMouseEvent.js +85 -0
- package/lib/SyntheticTouchEvent.js +50 -0
- package/lib/SyntheticUIEvent.js +45 -0
- package/lib/SyntheticWheelEvent.js +63 -0
- package/lib/Transaction.js +251 -0
- package/lib/ViewportMetrics.js +37 -0
- package/lib/accumulate.js +54 -0
- package/lib/adler32.js +39 -0
- package/lib/containsNode.js +49 -0
- package/lib/copyProperties.js +54 -0
- package/lib/createArrayFrom.js +94 -0
- package/lib/createNodesFromMarkup.js +93 -0
- package/lib/createObjectFrom.js +61 -0
- package/lib/cx.js +44 -0
- package/lib/dangerousStyleValue.js +57 -0
- package/lib/emptyFunction.js +43 -0
- package/lib/escapeTextForBrowser.js +47 -0
- package/lib/ex.js +49 -0
- package/lib/filterAttributes.js +45 -0
- package/lib/flattenChildren.js +54 -0
- package/lib/forEachAccumulated.js +36 -0
- package/lib/ge.js +76 -0
- package/lib/getActiveElement.js +33 -0
- package/lib/getEventTarget.js +36 -0
- package/lib/getMarkupWrap.js +108 -0
- package/lib/getNodeForCharacterOffset.js +80 -0
- package/lib/getReactRootElementInContainer.js +40 -0
- package/lib/getTextContentAccessor.js +40 -0
- package/lib/getUnboundedScrollPosition.js +45 -0
- package/lib/hyphenate.js +35 -0
- package/lib/invariant.js +54 -0
- package/lib/isEventSupported.js +74 -0
- package/lib/isNode.js +33 -0
- package/lib/isTextInputElement.js +49 -0
- package/lib/isTextNode.js +30 -0
- package/lib/joinClasses.js +44 -0
- package/lib/keyMirror.js +58 -0
- package/lib/keyOf.js +41 -0
- package/lib/memoizeStringOnly.js +39 -0
- package/lib/merge.js +37 -0
- package/lib/mergeHelpers.js +137 -0
- package/lib/mergeInto.js +45 -0
- package/lib/mixInto.js +34 -0
- package/lib/mutateHTMLNodeWithMarkup.js +100 -0
- package/lib/objMap.js +47 -0
- package/lib/objMapKeyVal.js +47 -0
- package/lib/performanceNow.js +42 -0
- package/lib/shallowEqual.js +49 -0
- package/lib/traverseAllChildren.js +127 -0
- package/package.json +33 -31
- package/react.js +4 -0
- package/.npmignore +0 -7
- package/.travis.yml +0 -7
- package/Jakefile.js +0 -39
- package/LICENSE +0 -19
- package/browser-test/dist.html +0 -90
- package/browser-test/index.html +0 -86
- package/browser-test/min.html +0 -90
- package/dist/react.js +0 -3141
- package/dist/react.min.js +0 -22
- package/doc/advanced.md +0 -175
- package/doc/color-def.graffle +0 -938
- package/doc/color-def.png +0 -0
- package/doc/simple.dot +0 -25
- package/doc/simple.png +0 -0
- package/examples/longer-example.js +0 -41
- package/examples/simple.js +0 -45
- package/examples/using-ast-directly.js +0 -30
- package/examples/using-events1.js +0 -79
- package/examples/using-log-events.js +0 -43
- package/lib/base-task.js +0 -120
- package/lib/cb-task.js +0 -84
- package/lib/core.js +0 -138
- package/lib/dsl.js +0 -138
- package/lib/error.js +0 -55
- package/lib/event-collector.js +0 -81
- package/lib/event-manager.js +0 -89
- package/lib/eventemitter.js +0 -20
- package/lib/finalcb-first-task.js +0 -68
- package/lib/finalcb-task.js +0 -65
- package/lib/id.js +0 -22
- package/lib/input-parser.js +0 -56
- package/lib/log-events.js +0 -101
- package/lib/parse.js +0 -41
- package/lib/promise-resolve.js +0 -50
- package/lib/promise-task.js +0 -93
- package/lib/react.js +0 -59
- package/lib/ret-task.js +0 -71
- package/lib/sprintf.js +0 -18
- package/lib/status.js +0 -14
- package/lib/task.js +0 -251
- package/lib/track-tasks.js +0 -74
- package/lib/validate.js +0 -159
- package/lib/vcon.js +0 -113
- package/lib/when-task.js +0 -84
- package/src/dist.build.requirejs +0 -20
- package/test/ast.mocha.js +0 -136
- package/test/cb-task.mocha.js +0 -220
- package/test/core-deferred.mocha.js +0 -143
- package/test/core-when.mocha.js +0 -96
- package/test/core.mocha.js +0 -589
- package/test/dsl.mocha.js +0 -352
- package/test/event-manager.mocha.js +0 -119
- package/test/exec-options.mocha.js +0 -48
- package/test/finalcb-task.mocha.js +0 -58
- package/test/input-parser.mocha.js +0 -86
- package/test/log-events.mocha.js +0 -88
- package/test/mocha.opts +0 -2
- package/test/module-use.mocha.js +0 -164
- package/test/promise-auto-resolve.mocha.js +0 -68
- package/test/ret-task.mocha.js +0 -220
- package/test/task.mocha.js +0 -42
- package/test/validate-cb-task.mocha.js +0 -100
- package/test/validate-ret-task.mocha.js +0 -110
- package/test/validate.mocha.js +0 -324
- package/test/vcon.mocha.js +0 -193
- package/vendor/chai/chai.js +0 -4251
- package/vendor/jquery/jquery-1.7.1.js +0 -9266
- package/vendor/jquery/jquery-1.7.1.min.js +0 -4
- package/vendor/node/util.js +0 -531
- package/vendor/requirejs/require.js +0 -2045
- package/vendor/requirejs/require.min.js +0 -36
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013 Facebook, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @providesModule ReactMultiChildUpdateTypes
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
var keyMirror = require("./keyMirror");
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* When a component's children are updated, a series of update configuration
|
|
23
|
+
* objects are created in order to batch and serialize the required changes.
|
|
24
|
+
*
|
|
25
|
+
* Enumerates all the possible types of update configurations.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
var ReactMultiChildUpdateTypes = keyMirror({
|
|
30
|
+
INSERT_MARKUP: null,
|
|
31
|
+
MOVE_EXISTING: null,
|
|
32
|
+
REMOVE_NODE: null,
|
|
33
|
+
TEXT_CONTENT: null
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
module.exports = ReactMultiChildUpdateTypes;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013 Facebook, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @providesModule ReactOwner
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
"use strict";
|
|
20
|
+
|
|
21
|
+
var invariant = require("./invariant");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* ReactOwners are capable of storing references to owned components.
|
|
25
|
+
*
|
|
26
|
+
* All components are capable of //being// referenced by owner components, but
|
|
27
|
+
* only ReactOwner components are capable of //referencing// owned components.
|
|
28
|
+
* The named reference is known as a "ref".
|
|
29
|
+
*
|
|
30
|
+
* Refs are available when mounted and updated during reconciliation.
|
|
31
|
+
*
|
|
32
|
+
* var MyComponent = React.createClass({
|
|
33
|
+
* render: function() {
|
|
34
|
+
* return (
|
|
35
|
+
* <div onClick={this.handleClick}>
|
|
36
|
+
* <CustomComponent ref="custom" />
|
|
37
|
+
* </div>
|
|
38
|
+
* );
|
|
39
|
+
* },
|
|
40
|
+
* handleClick: function() {
|
|
41
|
+
* this.refs.custom.handleClick();
|
|
42
|
+
* },
|
|
43
|
+
* componentDidMount: function() {
|
|
44
|
+
* this.refs.custom.initialize();
|
|
45
|
+
* }
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* Refs should rarely be used. When refs are used, they should only be done to
|
|
49
|
+
* control data that is not handled by React's data flow.
|
|
50
|
+
*
|
|
51
|
+
* @class ReactOwner
|
|
52
|
+
*/
|
|
53
|
+
var ReactOwner = {
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {?object} object
|
|
57
|
+
* @return {boolean} True if `object` is a valid owner.
|
|
58
|
+
* @final
|
|
59
|
+
*/
|
|
60
|
+
isValidOwner: function(object) {
|
|
61
|
+
return !!(
|
|
62
|
+
object &&
|
|
63
|
+
typeof object.attachRef === 'function' &&
|
|
64
|
+
typeof object.detachRef === 'function'
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Adds a component by ref to an owner component.
|
|
70
|
+
*
|
|
71
|
+
* @param {ReactComponent} component Component to reference.
|
|
72
|
+
* @param {string} ref Name by which to refer to the component.
|
|
73
|
+
* @param {ReactOwner} owner Component on which to record the ref.
|
|
74
|
+
* @final
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
addComponentAsRefTo: function(component, ref, owner) {
|
|
78
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
79
|
+
ReactOwner.isValidOwner(owner),
|
|
80
|
+
'addComponentAsRefTo(...): Only a ReactOwner can have refs.'
|
|
81
|
+
) : invariant(ReactOwner.isValidOwner(owner)));
|
|
82
|
+
owner.attachRef(ref, component);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Removes a component by ref from an owner component.
|
|
87
|
+
*
|
|
88
|
+
* @param {ReactComponent} component Component to dereference.
|
|
89
|
+
* @param {string} ref Name of the ref to remove.
|
|
90
|
+
* @param {ReactOwner} owner Component on which the ref is recorded.
|
|
91
|
+
* @final
|
|
92
|
+
* @internal
|
|
93
|
+
*/
|
|
94
|
+
removeComponentAsRefFrom: function(component, ref, owner) {
|
|
95
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
96
|
+
ReactOwner.isValidOwner(owner),
|
|
97
|
+
'removeComponentAsRefFrom(...): Only a ReactOwner can have refs.'
|
|
98
|
+
) : invariant(ReactOwner.isValidOwner(owner)));
|
|
99
|
+
// Check that `component` is still the current ref because we do not want to
|
|
100
|
+
// detach the ref if another component stole it.
|
|
101
|
+
if (owner.refs[ref] === component) {
|
|
102
|
+
owner.detachRef(ref);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A ReactComponent must mix this in to have refs.
|
|
108
|
+
*
|
|
109
|
+
* @lends {ReactOwner.prototype}
|
|
110
|
+
*/
|
|
111
|
+
Mixin: {
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Lazily allocates the refs object and stores `component` as `ref`.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} ref Reference name.
|
|
117
|
+
* @param {component} component Component to store as `ref`.
|
|
118
|
+
* @final
|
|
119
|
+
* @private
|
|
120
|
+
*/
|
|
121
|
+
attachRef: function(ref, component) {
|
|
122
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
123
|
+
component.isOwnedBy(this),
|
|
124
|
+
'attachRef(%s, ...): Only a component\'s owner can store a ref to it.',
|
|
125
|
+
ref
|
|
126
|
+
) : invariant(component.isOwnedBy(this)));
|
|
127
|
+
var refs = this.refs || (this.refs = {});
|
|
128
|
+
refs[ref] = component;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Detaches a reference name.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} ref Name to dereference.
|
|
135
|
+
* @final
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
138
|
+
detachRef: function(ref) {
|
|
139
|
+
delete this.refs[ref];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
module.exports = ReactOwner;
|
package/lib/ReactPerf.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013 Facebook, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @providesModule ReactPerf
|
|
17
|
+
* @typechecks static-only
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
"use strict";
|
|
21
|
+
|
|
22
|
+
var ReactPerf = {
|
|
23
|
+
/**
|
|
24
|
+
* Boolean to enable/disable measurement. Set to false by default to prevent
|
|
25
|
+
* accidental logging and perf loss.
|
|
26
|
+
*/
|
|
27
|
+
enableMeasure: false,
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Holds onto the measure function in use. By default, don't measure
|
|
31
|
+
* anything, but we'll override this if we inject a measure function.
|
|
32
|
+
*/
|
|
33
|
+
storedMeasure: _noMeasure,
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Use this to wrap methods you want to measure.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} objName
|
|
39
|
+
* @param {string} fnName
|
|
40
|
+
* @param {function} func
|
|
41
|
+
* @return {function}
|
|
42
|
+
*/
|
|
43
|
+
measure: function(objName, fnName, func) {
|
|
44
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
45
|
+
var measuredFunc = null;
|
|
46
|
+
return function() {
|
|
47
|
+
if (ReactPerf.enableMeasure) {
|
|
48
|
+
if (!measuredFunc) {
|
|
49
|
+
measuredFunc = ReactPerf.storedMeasure(objName, fnName, func);
|
|
50
|
+
}
|
|
51
|
+
return measuredFunc.apply(this, arguments);
|
|
52
|
+
}
|
|
53
|
+
return func.apply(this, arguments);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return func;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
injection: {
|
|
60
|
+
/**
|
|
61
|
+
* @param {function} measure
|
|
62
|
+
*/
|
|
63
|
+
injectMeasure: function(measure) {
|
|
64
|
+
ReactPerf.storedMeasure = measure;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
70
|
+
var ExecutionEnvironment = require("./ExecutionEnvironment");
|
|
71
|
+
var url = (ExecutionEnvironment.canUseDOM && window.location.href) || '';
|
|
72
|
+
ReactPerf.enableMeasure = ReactPerf.enableMeasure ||
|
|
73
|
+
(/[?&]react_perf\b/).test(url);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Simply passes through the measured function, without measuring it.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} objName
|
|
80
|
+
* @param {string} fnName
|
|
81
|
+
* @param {function} func
|
|
82
|
+
* @return {function}
|
|
83
|
+
*/
|
|
84
|
+
function _noMeasure(objName, fnName, func) {
|
|
85
|
+
return func;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = ReactPerf;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013 Facebook, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @providesModule ReactPropTransferer
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
"use strict";
|
|
20
|
+
|
|
21
|
+
var emptyFunction = require("./emptyFunction");
|
|
22
|
+
var invariant = require("./invariant");
|
|
23
|
+
var joinClasses = require("./joinClasses");
|
|
24
|
+
var merge = require("./merge");
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a transfer strategy that will merge prop values using the supplied
|
|
28
|
+
* `mergeStrategy`. If a prop was previously unset, this just sets it.
|
|
29
|
+
*
|
|
30
|
+
* @param {function} mergeStrategy
|
|
31
|
+
* @return {function}
|
|
32
|
+
*/
|
|
33
|
+
function createTransferStrategy(mergeStrategy) {
|
|
34
|
+
return function(props, key, value) {
|
|
35
|
+
if (!props.hasOwnProperty(key)) {
|
|
36
|
+
props[key] = value;
|
|
37
|
+
} else {
|
|
38
|
+
props[key] = mergeStrategy(props[key], value);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Transfer strategies dictate how props are transferred by `transferPropsTo`.
|
|
45
|
+
*/
|
|
46
|
+
var TransferStrategies = {
|
|
47
|
+
/**
|
|
48
|
+
* Never transfer `children`.
|
|
49
|
+
*/
|
|
50
|
+
children: emptyFunction,
|
|
51
|
+
/**
|
|
52
|
+
* Transfer the `className` prop by merging them.
|
|
53
|
+
*/
|
|
54
|
+
className: createTransferStrategy(joinClasses),
|
|
55
|
+
/**
|
|
56
|
+
* Never transfer the `ref` prop.
|
|
57
|
+
*/
|
|
58
|
+
ref: emptyFunction,
|
|
59
|
+
/**
|
|
60
|
+
* Transfer the `style` prop (which is an object) by merging them.
|
|
61
|
+
*/
|
|
62
|
+
style: createTransferStrategy(merge)
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* ReactPropTransferer are capable of transferring props to another component
|
|
67
|
+
* using a `transferPropsTo` method.
|
|
68
|
+
*
|
|
69
|
+
* @class ReactPropTransferer
|
|
70
|
+
*/
|
|
71
|
+
var ReactPropTransferer = {
|
|
72
|
+
|
|
73
|
+
TransferStrategies: TransferStrategies,
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @lends {ReactPropTransferer.prototype}
|
|
77
|
+
*/
|
|
78
|
+
Mixin: {
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Transfer props from this component to a target component.
|
|
82
|
+
*
|
|
83
|
+
* Props that do not have an explicit transfer strategy will be transferred
|
|
84
|
+
* only if the target component does not already have the prop set.
|
|
85
|
+
*
|
|
86
|
+
* This is usually used to pass down props to a returned root component.
|
|
87
|
+
*
|
|
88
|
+
* @param {ReactComponent} component Component receiving the properties.
|
|
89
|
+
* @return {ReactComponent} The supplied `component`.
|
|
90
|
+
* @final
|
|
91
|
+
* @protected
|
|
92
|
+
*/
|
|
93
|
+
transferPropsTo: function(component) {
|
|
94
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
95
|
+
component.props.__owner__ === this,
|
|
96
|
+
'%s: You can\'t call transferPropsTo() on a component that you ' +
|
|
97
|
+
'don\'t own, %s. This usually means you are calling ' +
|
|
98
|
+
'transferPropsTo() on a component passed in as props or children.',
|
|
99
|
+
this.constructor.displayName,
|
|
100
|
+
component.constructor.displayName
|
|
101
|
+
) : invariant(component.props.__owner__ === this));
|
|
102
|
+
|
|
103
|
+
var props = {};
|
|
104
|
+
for (var thatKey in component.props) {
|
|
105
|
+
if (component.props.hasOwnProperty(thatKey)) {
|
|
106
|
+
props[thatKey] = component.props[thatKey];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (var thisKey in this.props) {
|
|
110
|
+
if (!this.props.hasOwnProperty(thisKey)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
var transferStrategy = TransferStrategies[thisKey];
|
|
114
|
+
if (transferStrategy) {
|
|
115
|
+
transferStrategy(props, thisKey, this.props[thisKey]);
|
|
116
|
+
} else if (!props.hasOwnProperty(thisKey)) {
|
|
117
|
+
props[thisKey] = this.props[thisKey];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
component.props = props;
|
|
121
|
+
return component;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
module.exports = ReactPropTransferer;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013 Facebook, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
* @providesModule ReactPropTypes
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
"use strict";
|
|
20
|
+
|
|
21
|
+
var createObjectFrom = require("./createObjectFrom");
|
|
22
|
+
var invariant = require("./invariant");
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Collection of methods that allow declaration and validation of props that are
|
|
26
|
+
* supplied to React components. Example usage:
|
|
27
|
+
*
|
|
28
|
+
* var Props = require('ReactPropTypes');
|
|
29
|
+
* var MyArticle = React.createClass({
|
|
30
|
+
* propTypes: {
|
|
31
|
+
* // An optional string prop named "description".
|
|
32
|
+
* description: Props.string,
|
|
33
|
+
*
|
|
34
|
+
* // A required enum prop named "category".
|
|
35
|
+
* category: Props.oneOf(['News','Photos']).isRequired,
|
|
36
|
+
*
|
|
37
|
+
* // A prop named "dialog" that requires an instance of Dialog.
|
|
38
|
+
* dialog: Props.instanceOf(Dialog).isRequired
|
|
39
|
+
* },
|
|
40
|
+
* render: function() { ... }
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* A more formal specification of how these methods are used:
|
|
44
|
+
*
|
|
45
|
+
* type := array|bool|func|object|number|string|oneOf([...])|instanceOf(...)
|
|
46
|
+
* decl := ReactPropTypes.{type}(.isRequired)?
|
|
47
|
+
*
|
|
48
|
+
* Each and every declaration produces a function with the same signature. This
|
|
49
|
+
* allows the creation of custom validation functions. For example:
|
|
50
|
+
*
|
|
51
|
+
* var Props = require('ReactPropTypes');
|
|
52
|
+
* var MyLink = React.createClass({
|
|
53
|
+
* propTypes: {
|
|
54
|
+
* // An optional string or URI prop named "href".
|
|
55
|
+
* href: function(props, propName, componentName) {
|
|
56
|
+
* var propValue = props[propName];
|
|
57
|
+
* invariant(
|
|
58
|
+
* propValue == null ||
|
|
59
|
+
* typeof propValue === 'string' ||
|
|
60
|
+
* propValue instanceof URI,
|
|
61
|
+
* 'Invalid `%s` supplied to `%s`, expected string or URI.',
|
|
62
|
+
* propName,
|
|
63
|
+
* componentName
|
|
64
|
+
* );
|
|
65
|
+
* }
|
|
66
|
+
* },
|
|
67
|
+
* render: function() { ... }
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
var Props = {
|
|
73
|
+
|
|
74
|
+
array: createPrimitiveTypeChecker('array'),
|
|
75
|
+
bool: createPrimitiveTypeChecker('boolean'),
|
|
76
|
+
func: createPrimitiveTypeChecker('function'),
|
|
77
|
+
number: createPrimitiveTypeChecker('number'),
|
|
78
|
+
object: createPrimitiveTypeChecker('object'),
|
|
79
|
+
string: createPrimitiveTypeChecker('string'),
|
|
80
|
+
|
|
81
|
+
oneOf: createEnumTypeChecker,
|
|
82
|
+
|
|
83
|
+
instanceOf: createInstanceTypeChecker
|
|
84
|
+
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
var ANONYMOUS = '<<anonymous>>';
|
|
88
|
+
|
|
89
|
+
function createPrimitiveTypeChecker(expectedType) {
|
|
90
|
+
function validatePrimitiveType(propValue, propName, componentName) {
|
|
91
|
+
var propType = typeof propValue;
|
|
92
|
+
if (propType === 'object' && Array.isArray(propValue)) {
|
|
93
|
+
propType = 'array';
|
|
94
|
+
}
|
|
95
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
96
|
+
propType === expectedType,
|
|
97
|
+
'Invalid prop `%s` of type `%s` supplied to `%s`, expected `%s`.',
|
|
98
|
+
propName,
|
|
99
|
+
propType,
|
|
100
|
+
componentName,
|
|
101
|
+
expectedType
|
|
102
|
+
) : invariant(propType === expectedType));
|
|
103
|
+
}
|
|
104
|
+
return createChainableTypeChecker(validatePrimitiveType);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function createEnumTypeChecker(expectedValues) {
|
|
108
|
+
var expectedEnum = createObjectFrom(expectedValues);
|
|
109
|
+
function validateEnumType(propValue, propName, componentName) {
|
|
110
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
111
|
+
expectedEnum[propValue],
|
|
112
|
+
'Invalid prop `%s` supplied to `%s`, expected one of %s.',
|
|
113
|
+
propName,
|
|
114
|
+
componentName,
|
|
115
|
+
JSON.stringify(Object.keys(expectedEnum))
|
|
116
|
+
) : invariant(expectedEnum[propValue]));
|
|
117
|
+
}
|
|
118
|
+
return createChainableTypeChecker(validateEnumType);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function createInstanceTypeChecker(expectedClass) {
|
|
122
|
+
function validateInstanceType(propValue, propName, componentName) {
|
|
123
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
124
|
+
propValue instanceof expectedClass,
|
|
125
|
+
'Invalid prop `%s` supplied to `%s`, expected instance of `%s`.',
|
|
126
|
+
propName,
|
|
127
|
+
componentName,
|
|
128
|
+
expectedClass.name || ANONYMOUS
|
|
129
|
+
) : invariant(propValue instanceof expectedClass));
|
|
130
|
+
}
|
|
131
|
+
return createChainableTypeChecker(validateInstanceType);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function createChainableTypeChecker(validate) {
|
|
135
|
+
function createTypeChecker(isRequired) {
|
|
136
|
+
function checkType(props, propName, componentName) {
|
|
137
|
+
var propValue = props[propName];
|
|
138
|
+
if (propValue != null) {
|
|
139
|
+
// Only validate if there is a value to check.
|
|
140
|
+
validate(propValue, propName, componentName || ANONYMOUS);
|
|
141
|
+
} else {
|
|
142
|
+
("production" !== process.env.NODE_ENV ? invariant(
|
|
143
|
+
!isRequired,
|
|
144
|
+
'Required prop `%s` was not specified in `%s`.',
|
|
145
|
+
propName,
|
|
146
|
+
componentName || ANONYMOUS
|
|
147
|
+
) : invariant(!isRequired));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!isRequired) {
|
|
151
|
+
checkType.isRequired = createTypeChecker(true);
|
|
152
|
+
}
|
|
153
|
+
return checkType;
|
|
154
|
+
}
|
|
155
|
+
return createTypeChecker(false);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = Props;
|