1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _assign = require('object-assign');
|
14 |
|
15 | var ReactCurrentOwner = require('./ReactCurrentOwner');
|
16 |
|
17 | var warning = require('fbjs/lib/warning');
|
18 | var canDefineProperty = require('./canDefineProperty');
|
19 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
20 |
|
21 | var REACT_ELEMENT_TYPE = require('./ReactElementSymbol');
|
22 |
|
23 | var RESERVED_PROPS = {
|
24 | key: true,
|
25 | ref: true,
|
26 | __self: true,
|
27 | __source: true
|
28 | };
|
29 |
|
30 | var specialPropKeyWarningShown, specialPropRefWarningShown;
|
31 |
|
32 | function hasValidRef(config) {
|
33 | if (process.env.NODE_ENV !== 'production') {
|
34 | if (hasOwnProperty.call(config, 'ref')) {
|
35 | var getter = Object.getOwnPropertyDescriptor(config, 'ref').get;
|
36 | if (getter && getter.isReactWarning) {
|
37 | return false;
|
38 | }
|
39 | }
|
40 | }
|
41 | return config.ref !== undefined;
|
42 | }
|
43 |
|
44 | function hasValidKey(config) {
|
45 | if (process.env.NODE_ENV !== 'production') {
|
46 | if (hasOwnProperty.call(config, 'key')) {
|
47 | var getter = Object.getOwnPropertyDescriptor(config, 'key').get;
|
48 | if (getter && getter.isReactWarning) {
|
49 | return false;
|
50 | }
|
51 | }
|
52 | }
|
53 | return config.key !== undefined;
|
54 | }
|
55 |
|
56 | function defineKeyPropWarningGetter(props, displayName) {
|
57 | var warnAboutAccessingKey = function () {
|
58 | if (!specialPropKeyWarningShown) {
|
59 | specialPropKeyWarningShown = true;
|
60 | process.env.NODE_ENV !== 'production' ? warning(false, '%s: `key` is not a prop. Trying to access it will result ' + 'in `undefined` being returned. If you need to access the same ' + 'value within the child component, you should pass it as a different ' + 'prop. (https://fb.me/react-special-props)', displayName) : void 0;
|
61 | }
|
62 | };
|
63 | warnAboutAccessingKey.isReactWarning = true;
|
64 | Object.defineProperty(props, 'key', {
|
65 | get: warnAboutAccessingKey,
|
66 | configurable: true
|
67 | });
|
68 | }
|
69 |
|
70 | function defineRefPropWarningGetter(props, displayName) {
|
71 | var warnAboutAccessingRef = function () {
|
72 | if (!specialPropRefWarningShown) {
|
73 | specialPropRefWarningShown = true;
|
74 | process.env.NODE_ENV !== 'production' ? warning(false, '%s: `ref` is not a prop. Trying to access it will result ' + 'in `undefined` being returned. If you need to access the same ' + 'value within the child component, you should pass it as a different ' + 'prop. (https://fb.me/react-special-props)', displayName) : void 0;
|
75 | }
|
76 | };
|
77 | warnAboutAccessingRef.isReactWarning = true;
|
78 | Object.defineProperty(props, 'ref', {
|
79 | get: warnAboutAccessingRef,
|
80 | configurable: true
|
81 | });
|
82 | }
|
83 |
|
84 | /**
|
85 | * Factory method to create a new React element. This no longer adheres to
|
86 | * the class pattern, so do not use new to call it. Also, no instanceof check
|
87 | * will work. Instead test $$typeof field against Symbol.for('react.element') to check
|
88 | * if something is a React Element.
|
89 | *
|
90 | * @param {*} type
|
91 | * @param {*} key
|
92 | * @param {string|object} ref
|
93 | * @param {*} self A *temporary* helper to detect places where `this` is
|
94 | * different from the `owner` when React.createElement is called, so that we
|
95 | * can warn. We want to get rid of owner and replace string `ref`s with arrow
|
96 | * functions, and as long as `this` and owner are the same, there will be no
|
97 | * change in behavior.
|
98 | * @param {*} source An annotation object (added by a transpiler or otherwise)
|
99 | * indicating filename, line number, and/or other information.
|
100 | * @param {*} owner
|
101 | * @param {*} props
|
102 | * @internal
|
103 | */
|
104 | var ReactElement = function (type, key, ref, self, source, owner, props) {
|
105 | var element = {
|
106 |
|
107 | $$typeof: REACT_ELEMENT_TYPE,
|
108 |
|
109 |
|
110 | type: type,
|
111 | key: key,
|
112 | ref: ref,
|
113 | props: props,
|
114 |
|
115 |
|
116 | _owner: owner
|
117 | };
|
118 |
|
119 | if (process.env.NODE_ENV !== 'production') {
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | element._store = {};
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | if (canDefineProperty) {
|
131 | Object.defineProperty(element._store, 'validated', {
|
132 | configurable: false,
|
133 | enumerable: false,
|
134 | writable: true,
|
135 | value: false
|
136 | });
|
137 |
|
138 | Object.defineProperty(element, '_self', {
|
139 | configurable: false,
|
140 | enumerable: false,
|
141 | writable: false,
|
142 | value: self
|
143 | });
|
144 |
|
145 |
|
146 | Object.defineProperty(element, '_source', {
|
147 | configurable: false,
|
148 | enumerable: false,
|
149 | writable: false,
|
150 | value: source
|
151 | });
|
152 | } else {
|
153 | element._store.validated = false;
|
154 | element._self = self;
|
155 | element._source = source;
|
156 | }
|
157 | if (Object.freeze) {
|
158 | Object.freeze(element.props);
|
159 | Object.freeze(element);
|
160 | }
|
161 | }
|
162 |
|
163 | return element;
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | ReactElement.createElement = function (type, config, children) {
|
171 | var propName;
|
172 |
|
173 |
|
174 | var props = {};
|
175 |
|
176 | var key = null;
|
177 | var ref = null;
|
178 | var self = null;
|
179 | var source = null;
|
180 |
|
181 | if (config != null) {
|
182 | if (hasValidRef(config)) {
|
183 | ref = config.ref;
|
184 | }
|
185 | if (hasValidKey(config)) {
|
186 | key = '' + config.key;
|
187 | }
|
188 |
|
189 | self = config.__self === undefined ? null : config.__self;
|
190 | source = config.__source === undefined ? null : config.__source;
|
191 |
|
192 | for (propName in config) {
|
193 | if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
|
194 | props[propName] = config[propName];
|
195 | }
|
196 | }
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 | var childrenLength = arguments.length - 2;
|
202 | if (childrenLength === 1) {
|
203 | props.children = children;
|
204 | } else if (childrenLength > 1) {
|
205 | var childArray = Array(childrenLength);
|
206 | for (var i = 0; i < childrenLength; i++) {
|
207 | childArray[i] = arguments[i + 2];
|
208 | }
|
209 | if (process.env.NODE_ENV !== 'production') {
|
210 | if (Object.freeze) {
|
211 | Object.freeze(childArray);
|
212 | }
|
213 | }
|
214 | props.children = childArray;
|
215 | }
|
216 |
|
217 |
|
218 | if (type && type.defaultProps) {
|
219 | var defaultProps = type.defaultProps;
|
220 | for (propName in defaultProps) {
|
221 | if (props[propName] === undefined) {
|
222 | props[propName] = defaultProps[propName];
|
223 | }
|
224 | }
|
225 | }
|
226 | if (process.env.NODE_ENV !== 'production') {
|
227 | if (key || ref) {
|
228 | if (typeof props.$$typeof === 'undefined' || props.$$typeof !== REACT_ELEMENT_TYPE) {
|
229 | var displayName = typeof type === 'function' ? type.displayName || type.name || 'Unknown' : type;
|
230 | if (key) {
|
231 | defineKeyPropWarningGetter(props, displayName);
|
232 | }
|
233 | if (ref) {
|
234 | defineRefPropWarningGetter(props, displayName);
|
235 | }
|
236 | }
|
237 | }
|
238 | }
|
239 | return ReactElement(type, key, ref, self, source, ReactCurrentOwner.current, props);
|
240 | };
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | ReactElement.createFactory = function (type) {
|
247 | var factory = ReactElement.createElement.bind(null, type);
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | factory.type = type;
|
254 | return factory;
|
255 | };
|
256 |
|
257 | ReactElement.cloneAndReplaceKey = function (oldElement, newKey) {
|
258 | var newElement = ReactElement(oldElement.type, newKey, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props);
|
259 |
|
260 | return newElement;
|
261 | };
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | ReactElement.cloneElement = function (element, config, children) {
|
268 | var propName;
|
269 |
|
270 |
|
271 | var props = _assign({}, element.props);
|
272 |
|
273 |
|
274 | var key = element.key;
|
275 | var ref = element.ref;
|
276 |
|
277 | var self = element._self;
|
278 |
|
279 |
|
280 |
|
281 | var source = element._source;
|
282 |
|
283 |
|
284 | var owner = element._owner;
|
285 |
|
286 | if (config != null) {
|
287 | if (hasValidRef(config)) {
|
288 |
|
289 | ref = config.ref;
|
290 | owner = ReactCurrentOwner.current;
|
291 | }
|
292 | if (hasValidKey(config)) {
|
293 | key = '' + config.key;
|
294 | }
|
295 |
|
296 |
|
297 | var defaultProps;
|
298 | if (element.type && element.type.defaultProps) {
|
299 | defaultProps = element.type.defaultProps;
|
300 | }
|
301 | for (propName in config) {
|
302 | if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
|
303 | if (config[propName] === undefined && defaultProps !== undefined) {
|
304 |
|
305 | props[propName] = defaultProps[propName];
|
306 | } else {
|
307 | props[propName] = config[propName];
|
308 | }
|
309 | }
|
310 | }
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 | var childrenLength = arguments.length - 2;
|
316 | if (childrenLength === 1) {
|
317 | props.children = children;
|
318 | } else if (childrenLength > 1) {
|
319 | var childArray = Array(childrenLength);
|
320 | for (var i = 0; i < childrenLength; i++) {
|
321 | childArray[i] = arguments[i + 2];
|
322 | }
|
323 | props.children = childArray;
|
324 | }
|
325 |
|
326 | return ReactElement(element.type, key, ref, self, source, owner, props);
|
327 | };
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | ReactElement.isValidElement = function (object) {
|
337 | return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
|
338 | };
|
339 |
|
340 | module.exports = ReactElement; |
\ | No newline at end of file |