1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | var ReactOwner = require('./ReactOwner');
|
13 |
|
14 | var ReactRef = {};
|
15 |
|
16 | function attachRef(ref, component, owner) {
|
17 | if (typeof ref === 'function') {
|
18 | ref(component.getPublicInstance());
|
19 | } else {
|
20 |
|
21 | ReactOwner.addComponentAsRefTo(component, ref, owner);
|
22 | }
|
23 | }
|
24 |
|
25 | function detachRef(ref, component, owner) {
|
26 | if (typeof ref === 'function') {
|
27 | ref(null);
|
28 | } else {
|
29 |
|
30 | ReactOwner.removeComponentAsRefFrom(component, ref, owner);
|
31 | }
|
32 | }
|
33 |
|
34 | ReactRef.attachRefs = function (instance, element) {
|
35 | if (element === null || typeof element !== 'object') {
|
36 | return;
|
37 | }
|
38 | var ref = element.ref;
|
39 | if (ref != null) {
|
40 | attachRef(ref, instance, element._owner);
|
41 | }
|
42 | };
|
43 |
|
44 | ReactRef.shouldUpdateRefs = function (prevElement, nextElement) {
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var prevRef = null;
|
58 | var prevOwner = null;
|
59 | if (prevElement !== null && typeof prevElement === 'object') {
|
60 | prevRef = prevElement.ref;
|
61 | prevOwner = prevElement._owner;
|
62 | }
|
63 |
|
64 | var nextRef = null;
|
65 | var nextOwner = null;
|
66 | if (nextElement !== null && typeof nextElement === 'object') {
|
67 | nextRef = nextElement.ref;
|
68 | nextOwner = nextElement._owner;
|
69 | }
|
70 |
|
71 | return prevRef !== nextRef ||
|
72 |
|
73 | typeof nextRef === 'string' && nextOwner !== prevOwner;
|
74 | };
|
75 |
|
76 | ReactRef.detachRefs = function (instance, element) {
|
77 | if (element === null || typeof element !== 'object') {
|
78 | return;
|
79 | }
|
80 | var ref = element.ref;
|
81 | if (ref != null) {
|
82 | detachRef(ref, instance, element._owner);
|
83 | }
|
84 | };
|
85 |
|
86 | module.exports = ReactRef; |
\ | No newline at end of file |