1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant'),
|
14 | _assign = require('object-assign');
|
15 |
|
16 | var ReactComponent = require('./ReactComponent');
|
17 | var ReactElement = require('./ReactElement');
|
18 | var ReactPropTypeLocationNames = require('./ReactPropTypeLocationNames');
|
19 | var ReactNoopUpdateQueue = require('./ReactNoopUpdateQueue');
|
20 |
|
21 | var emptyObject = require('fbjs/lib/emptyObject');
|
22 | var invariant = require('fbjs/lib/invariant');
|
23 | var warning = require('fbjs/lib/warning');
|
24 |
|
25 | var MIXINS_KEY = 'mixins';
|
26 |
|
27 |
|
28 |
|
29 | function identity(fn) {
|
30 | return fn;
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var injectedMixins = [];
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | var ReactClassInterface = {
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | mixins: 'DEFINE_MANY',
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | statics: 'DEFINE_MANY',
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | propTypes: 'DEFINE_MANY',
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | contextTypes: 'DEFINE_MANY',
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | childContextTypes: 'DEFINE_MANY',
|
104 |
|
105 |
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | getDefaultProps: 'DEFINE_MANY_MERGED',
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | getInitialState: 'DEFINE_MANY_MERGED',
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 | getChildContext: 'DEFINE_MANY_MERGED',
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | render: 'DEFINE_ONCE',
|
158 |
|
159 |
|
160 |
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | componentWillMount: 'DEFINE_MANY',
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | componentDidMount: 'DEFINE_MANY',
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | componentWillReceiveProps: 'DEFINE_MANY',
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | shouldComponentUpdate: 'DEFINE_ONCE',
|
224 |
|
225 | |
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | componentWillUpdate: 'DEFINE_MANY',
|
241 |
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | componentDidUpdate: 'DEFINE_MANY',
|
255 |
|
256 | |
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | componentWillUnmount: 'DEFINE_MANY',
|
268 |
|
269 |
|
270 |
|
271 | |
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | updateComponent: 'OVERRIDE_BASE'
|
282 |
|
283 | };
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | var RESERVED_SPEC_KEYS = {
|
295 | displayName: function (Constructor, displayName) {
|
296 | Constructor.displayName = displayName;
|
297 | },
|
298 | mixins: function (Constructor, mixins) {
|
299 | if (mixins) {
|
300 | for (var i = 0; i < mixins.length; i++) {
|
301 | mixSpecIntoComponent(Constructor, mixins[i]);
|
302 | }
|
303 | }
|
304 | },
|
305 | childContextTypes: function (Constructor, childContextTypes) {
|
306 | if (process.env.NODE_ENV !== 'production') {
|
307 | validateTypeDef(Constructor, childContextTypes, 'childContext');
|
308 | }
|
309 | Constructor.childContextTypes = _assign({}, Constructor.childContextTypes, childContextTypes);
|
310 | },
|
311 | contextTypes: function (Constructor, contextTypes) {
|
312 | if (process.env.NODE_ENV !== 'production') {
|
313 | validateTypeDef(Constructor, contextTypes, 'context');
|
314 | }
|
315 | Constructor.contextTypes = _assign({}, Constructor.contextTypes, contextTypes);
|
316 | },
|
317 | |
318 |
|
319 |
|
320 |
|
321 | getDefaultProps: function (Constructor, getDefaultProps) {
|
322 | if (Constructor.getDefaultProps) {
|
323 | Constructor.getDefaultProps = createMergedResultFunction(Constructor.getDefaultProps, getDefaultProps);
|
324 | } else {
|
325 | Constructor.getDefaultProps = getDefaultProps;
|
326 | }
|
327 | },
|
328 | propTypes: function (Constructor, propTypes) {
|
329 | if (process.env.NODE_ENV !== 'production') {
|
330 | validateTypeDef(Constructor, propTypes, 'prop');
|
331 | }
|
332 | Constructor.propTypes = _assign({}, Constructor.propTypes, propTypes);
|
333 | },
|
334 | statics: function (Constructor, statics) {
|
335 | mixStaticSpecIntoComponent(Constructor, statics);
|
336 | },
|
337 | autobind: function () {} };
|
338 |
|
339 | function validateTypeDef(Constructor, typeDef, location) {
|
340 | for (var propName in typeDef) {
|
341 | if (typeDef.hasOwnProperty(propName)) {
|
342 |
|
343 |
|
344 | process.env.NODE_ENV !== 'production' ? warning(typeof typeDef[propName] === 'function', '%s: %s type `%s` is invalid; it must be a function, usually from ' + 'React.PropTypes.', Constructor.displayName || 'ReactClass', ReactPropTypeLocationNames[location], propName) : void 0;
|
345 | }
|
346 | }
|
347 | }
|
348 |
|
349 | function validateMethodOverride(isAlreadyDefined, name) {
|
350 | var specPolicy = ReactClassInterface.hasOwnProperty(name) ? ReactClassInterface[name] : null;
|
351 |
|
352 |
|
353 | if (ReactClassMixin.hasOwnProperty(name)) {
|
354 | !(specPolicy === 'OVERRIDE_BASE') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.', name) : _prodInvariant('73', name) : void 0;
|
355 | }
|
356 |
|
357 |
|
358 | if (isAlreadyDefined) {
|
359 | !(specPolicy === 'DEFINE_MANY' || specPolicy === 'DEFINE_MANY_MERGED') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.', name) : _prodInvariant('74', name) : void 0;
|
360 | }
|
361 | }
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | function mixSpecIntoComponent(Constructor, spec) {
|
368 | if (!spec) {
|
369 | if (process.env.NODE_ENV !== 'production') {
|
370 | var typeofSpec = typeof spec;
|
371 | var isMixinValid = typeofSpec === 'object' && spec !== null;
|
372 |
|
373 | process.env.NODE_ENV !== 'production' ? warning(isMixinValid, '%s: You\'re attempting to include a mixin that is either null ' + 'or not an object. Check the mixins included by the component, ' + 'as well as any mixins they include themselves. ' + 'Expected object but got %s.', Constructor.displayName || 'ReactClass', spec === null ? null : typeofSpec) : void 0;
|
374 | }
|
375 |
|
376 | return;
|
377 | }
|
378 |
|
379 | !(typeof spec !== 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClass: You\'re attempting to use a component class or function as a mixin. Instead, just use a regular object.') : _prodInvariant('75') : void 0;
|
380 | !!ReactElement.isValidElement(spec) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClass: You\'re attempting to use a component as a mixin. Instead, just use a regular object.') : _prodInvariant('76') : void 0;
|
381 |
|
382 | var proto = Constructor.prototype;
|
383 | var autoBindPairs = proto.__reactAutoBindPairs;
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | if (spec.hasOwnProperty(MIXINS_KEY)) {
|
389 | RESERVED_SPEC_KEYS.mixins(Constructor, spec.mixins);
|
390 | }
|
391 |
|
392 | for (var name in spec) {
|
393 | if (!spec.hasOwnProperty(name)) {
|
394 | continue;
|
395 | }
|
396 |
|
397 | if (name === MIXINS_KEY) {
|
398 |
|
399 | continue;
|
400 | }
|
401 |
|
402 | var property = spec[name];
|
403 | var isAlreadyDefined = proto.hasOwnProperty(name);
|
404 | validateMethodOverride(isAlreadyDefined, name);
|
405 |
|
406 | if (RESERVED_SPEC_KEYS.hasOwnProperty(name)) {
|
407 | RESERVED_SPEC_KEYS[name](Constructor, property);
|
408 | } else {
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | var isReactClassMethod = ReactClassInterface.hasOwnProperty(name);
|
414 | var isFunction = typeof property === 'function';
|
415 | var shouldAutoBind = isFunction && !isReactClassMethod && !isAlreadyDefined && spec.autobind !== false;
|
416 |
|
417 | if (shouldAutoBind) {
|
418 | autoBindPairs.push(name, property);
|
419 | proto[name] = property;
|
420 | } else {
|
421 | if (isAlreadyDefined) {
|
422 | var specPolicy = ReactClassInterface[name];
|
423 |
|
424 |
|
425 | !(isReactClassMethod && (specPolicy === 'DEFINE_MANY_MERGED' || specPolicy === 'DEFINE_MANY')) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.', specPolicy, name) : _prodInvariant('77', specPolicy, name) : void 0;
|
426 |
|
427 |
|
428 |
|
429 | if (specPolicy === 'DEFINE_MANY_MERGED') {
|
430 | proto[name] = createMergedResultFunction(proto[name], property);
|
431 | } else if (specPolicy === 'DEFINE_MANY') {
|
432 | proto[name] = createChainedFunction(proto[name], property);
|
433 | }
|
434 | } else {
|
435 | proto[name] = property;
|
436 | if (process.env.NODE_ENV !== 'production') {
|
437 |
|
438 |
|
439 | if (typeof property === 'function' && spec.displayName) {
|
440 | proto[name].displayName = spec.displayName + '_' + name;
|
441 | }
|
442 | }
|
443 | }
|
444 | }
|
445 | }
|
446 | }
|
447 | }
|
448 |
|
449 | function mixStaticSpecIntoComponent(Constructor, statics) {
|
450 | if (!statics) {
|
451 | return;
|
452 | }
|
453 | for (var name in statics) {
|
454 | var property = statics[name];
|
455 | if (!statics.hasOwnProperty(name)) {
|
456 | continue;
|
457 | }
|
458 |
|
459 | var isReserved = name in RESERVED_SPEC_KEYS;
|
460 | !!isReserved ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.', name) : _prodInvariant('78', name) : void 0;
|
461 |
|
462 | var isInherited = name in Constructor;
|
463 | !!isInherited ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.', name) : _prodInvariant('79', name) : void 0;
|
464 | Constructor[name] = property;
|
465 | }
|
466 | }
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | function mergeIntoWithNoDuplicateKeys(one, two) {
|
476 | !(one && two && typeof one === 'object' && typeof two === 'object') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.') : _prodInvariant('80') : void 0;
|
477 |
|
478 | for (var key in two) {
|
479 | if (two.hasOwnProperty(key)) {
|
480 | !(one[key] === undefined) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.', key) : _prodInvariant('81', key) : void 0;
|
481 | one[key] = two[key];
|
482 | }
|
483 | }
|
484 | return one;
|
485 | }
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 | function createMergedResultFunction(one, two) {
|
496 | return function mergedResult() {
|
497 | var a = one.apply(this, arguments);
|
498 | var b = two.apply(this, arguments);
|
499 | if (a == null) {
|
500 | return b;
|
501 | } else if (b == null) {
|
502 | return a;
|
503 | }
|
504 | var c = {};
|
505 | mergeIntoWithNoDuplicateKeys(c, a);
|
506 | mergeIntoWithNoDuplicateKeys(c, b);
|
507 | return c;
|
508 | };
|
509 | }
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 | function createChainedFunction(one, two) {
|
520 | return function chainedFunction() {
|
521 | one.apply(this, arguments);
|
522 | two.apply(this, arguments);
|
523 | };
|
524 | }
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 | function bindAutoBindMethod(component, method) {
|
534 | var boundMethod = method.bind(component);
|
535 | if (process.env.NODE_ENV !== 'production') {
|
536 | boundMethod.__reactBoundContext = component;
|
537 | boundMethod.__reactBoundMethod = method;
|
538 | boundMethod.__reactBoundArguments = null;
|
539 | var componentName = component.constructor.displayName;
|
540 | var _bind = boundMethod.bind;
|
541 | boundMethod.bind = function (newThis) {
|
542 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
543 | args[_key - 1] = arguments[_key];
|
544 | }
|
545 |
|
546 |
|
547 |
|
548 |
|
549 | if (newThis !== component && newThis !== null) {
|
550 | process.env.NODE_ENV !== 'production' ? warning(false, 'bind(): React component methods may only be bound to the ' + 'component instance. See %s', componentName) : void 0;
|
551 | } else if (!args.length) {
|
552 | process.env.NODE_ENV !== 'production' ? warning(false, 'bind(): You are binding a component method to the component. ' + 'React does this for you automatically in a high-performance ' + 'way, so you can safely remove this call. See %s', componentName) : void 0;
|
553 | return boundMethod;
|
554 | }
|
555 | var reboundMethod = _bind.apply(boundMethod, arguments);
|
556 | reboundMethod.__reactBoundContext = component;
|
557 | reboundMethod.__reactBoundMethod = method;
|
558 | reboundMethod.__reactBoundArguments = args;
|
559 | return reboundMethod;
|
560 | };
|
561 | }
|
562 | return boundMethod;
|
563 | }
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 | function bindAutoBindMethods(component) {
|
571 | var pairs = component.__reactAutoBindPairs;
|
572 | for (var i = 0; i < pairs.length; i += 2) {
|
573 | var autoBindKey = pairs[i];
|
574 | var method = pairs[i + 1];
|
575 | component[autoBindKey] = bindAutoBindMethod(component, method);
|
576 | }
|
577 | }
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | var ReactClassMixin = {
|
584 |
|
585 | |
586 |
|
587 |
|
588 |
|
589 | replaceState: function (newState, callback) {
|
590 | this.updater.enqueueReplaceState(this, newState);
|
591 | if (callback) {
|
592 | this.updater.enqueueCallback(this, callback, 'replaceState');
|
593 | }
|
594 | },
|
595 |
|
596 | |
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 | isMounted: function () {
|
603 | return this.updater.isMounted(this);
|
604 | }
|
605 | };
|
606 |
|
607 | var ReactClassComponent = function () {};
|
608 | _assign(ReactClassComponent.prototype, ReactComponent.prototype, ReactClassMixin);
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 | var ReactClass = {
|
616 |
|
617 | |
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 | createClass: function (spec) {
|
626 |
|
627 |
|
628 |
|
629 | var Constructor = identity(function (props, context, updater) {
|
630 |
|
631 |
|
632 |
|
633 | if (process.env.NODE_ENV !== 'production') {
|
634 | process.env.NODE_ENV !== 'production' ? warning(this instanceof Constructor, 'Something is calling a React component directly. Use a factory or ' + 'JSX instead. See: https://fb.me/react-legacyfactory') : void 0;
|
635 | }
|
636 |
|
637 |
|
638 | if (this.__reactAutoBindPairs.length) {
|
639 | bindAutoBindMethods(this);
|
640 | }
|
641 |
|
642 | this.props = props;
|
643 | this.context = context;
|
644 | this.refs = emptyObject;
|
645 | this.updater = updater || ReactNoopUpdateQueue;
|
646 |
|
647 | this.state = null;
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | var initialState = this.getInitialState ? this.getInitialState() : null;
|
653 | if (process.env.NODE_ENV !== 'production') {
|
654 |
|
655 | if (initialState === undefined && this.getInitialState._isMockFunction) {
|
656 |
|
657 |
|
658 | initialState = null;
|
659 | }
|
660 | }
|
661 | !(typeof initialState === 'object' && !Array.isArray(initialState)) ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s.getInitialState(): must return an object or null', Constructor.displayName || 'ReactCompositeComponent') : _prodInvariant('82', Constructor.displayName || 'ReactCompositeComponent') : void 0;
|
662 |
|
663 | this.state = initialState;
|
664 | });
|
665 | Constructor.prototype = new ReactClassComponent();
|
666 | Constructor.prototype.constructor = Constructor;
|
667 | Constructor.prototype.__reactAutoBindPairs = [];
|
668 |
|
669 | injectedMixins.forEach(mixSpecIntoComponent.bind(null, Constructor));
|
670 |
|
671 | mixSpecIntoComponent(Constructor, spec);
|
672 |
|
673 |
|
674 | if (Constructor.getDefaultProps) {
|
675 | Constructor.defaultProps = Constructor.getDefaultProps();
|
676 | }
|
677 |
|
678 | if (process.env.NODE_ENV !== 'production') {
|
679 |
|
680 |
|
681 |
|
682 |
|
683 | if (Constructor.getDefaultProps) {
|
684 | Constructor.getDefaultProps.isReactClassApproved = {};
|
685 | }
|
686 | if (Constructor.prototype.getInitialState) {
|
687 | Constructor.prototype.getInitialState.isReactClassApproved = {};
|
688 | }
|
689 | }
|
690 |
|
691 | !Constructor.prototype.render ? process.env.NODE_ENV !== 'production' ? invariant(false, 'createClass(...): Class specification must implement a `render` method.') : _prodInvariant('83') : void 0;
|
692 |
|
693 | if (process.env.NODE_ENV !== 'production') {
|
694 | process.env.NODE_ENV !== 'production' ? warning(!Constructor.prototype.componentShouldUpdate, '%s has a method called ' + 'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' + 'The name is phrased as a question because the function is ' + 'expected to return a value.', spec.displayName || 'A component') : void 0;
|
695 | process.env.NODE_ENV !== 'production' ? warning(!Constructor.prototype.componentWillRecieveProps, '%s has a method called ' + 'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?', spec.displayName || 'A component') : void 0;
|
696 | }
|
697 |
|
698 |
|
699 | for (var methodName in ReactClassInterface) {
|
700 | if (!Constructor.prototype[methodName]) {
|
701 | Constructor.prototype[methodName] = null;
|
702 | }
|
703 | }
|
704 |
|
705 | return Constructor;
|
706 | },
|
707 |
|
708 | injection: {
|
709 | injectMixin: function (mixin) {
|
710 | injectedMixins.push(mixin);
|
711 | }
|
712 | }
|
713 |
|
714 | };
|
715 |
|
716 | module.exports = ReactClass; |
\ | No newline at end of file |