UNPKG

6.73 kBJavaScriptView Raw
1// ag-grid-react v25.3.0
2"use strict";
3var __extends = (this && this.__extends) || (function () {
4 var extendStatics = function (d, b) {
5 extendStatics = Object.setPrototypeOf ||
6 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
8 return extendStatics(d, b);
9 };
10 return function (d, b) {
11 extendStatics(d, b);
12 function __() { this.constructor = d; }
13 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14 };
15})();
16Object.defineProperty(exports, "__esModule", { value: true });
17var react_1 = require("react");
18var react_dom_1 = require("react-dom");
19var ag_grid_community_1 = require("ag-grid-community");
20var reactComponent_1 = require("./reactComponent");
21var keyGenerator_1 = require("./keyGenerator");
22var server_1 = require("react-dom/server");
23var LegacyReactComponent = /** @class */ (function (_super) {
24 __extends(LegacyReactComponent, _super);
25 function LegacyReactComponent() {
26 var _this = _super !== null && _super.apply(this, arguments) || this;
27 _this.staticMarkup = null;
28 _this.staticRenderTime = 0;
29 return _this;
30 }
31 LegacyReactComponent.prototype.init = function (params) {
32 var _this = this;
33 this.eParentElement = this.createParentElement(params);
34 this.renderStaticMarkup(params);
35 return new ag_grid_community_1.AgPromise(function (resolve) { return _this.createReactComponent(params, resolve); });
36 };
37 LegacyReactComponent.prototype.createReactComponent = function (params, resolve) {
38 var _this = this;
39 // regular components (ie not functional)
40 if (!this.isStatelessComponent()) {
41 // grab hold of the actual instance created
42 params.ref = function (element) {
43 _this.componentInstance = element;
44 _this.addParentContainerStyleAndClasses();
45 _this.removeStaticMarkup();
46 };
47 }
48 var reactComponent = react_1.createElement(this.reactComponent, params);
49 var portal = react_dom_1.createPortal(reactComponent, this.eParentElement, keyGenerator_1.default() // fixed deltaRowModeRefreshCompRenderer
50 );
51 this.portal = portal;
52 this.parentComponent.mountReactPortal(portal, this, function (value) {
53 resolve(value);
54 // functional/stateless components have a slightly different lifecycle (no refs) so we'll clean them up
55 // here
56 if (_this.isStatelessComponent()) {
57 if (_this.isSlowRenderer()) {
58 _this.removeStaticMarkup();
59 }
60 setTimeout(function () {
61 _this.removeStaticMarkup();
62 });
63 }
64 });
65 };
66 LegacyReactComponent.prototype.fallbackMethodAvailable = function (name) {
67 return false;
68 };
69 LegacyReactComponent.prototype.fallbackMethod = function (name, params) { };
70 LegacyReactComponent.prototype.isSlowRenderer = function () {
71 return this.staticRenderTime >= LegacyReactComponent.SLOW_RENDERERING_THRESHOLD;
72 };
73 LegacyReactComponent.prototype.isNullValue = function () {
74 return this.staticMarkup === '';
75 };
76 /*
77 * Attempt to render the component as static markup if possible
78 * What this does is eliminate any visible flicker for the user in the scenario where a component is destroyed and
79 * recreated with exactly the same data (ie with force refresh)
80 * Note: Some use cases will throw an error (ie when using Context) so if an error occurs just ignore it any move on
81 */
82 LegacyReactComponent.prototype.renderStaticMarkup = function (params) {
83 if (this.parentComponent.isDisableStaticMarkup() || (this.componentType.isCellRenderer && !this.componentType.isCellRenderer())) {
84 return;
85 }
86 var originalConsoleError = console.error;
87 var reactComponent = react_1.createElement(this.reactComponent, params);
88 try {
89 // if a user is doing anything that uses useLayoutEffect (like material ui) then it will throw and we
90 // can't do anything to stop it; this is just a warning and has no effect on anything so just suppress it
91 // for this single operation
92 console.error = function () {
93 };
94 var start = Date.now();
95 var staticMarkup = server_1.renderToStaticMarkup(reactComponent);
96 this.staticRenderTime = Date.now() - start;
97 console.error = originalConsoleError;
98 // if the render method returns null the result will be an empty string
99 if (staticMarkup === '') {
100 this.staticMarkup = staticMarkup;
101 }
102 else {
103 if (staticMarkup) {
104 // we wrap the content as if there is "trailing" text etc it's not easy to safely remove
105 // the same is true for memoized renderers, renderers that that return simple strings or NaN etc
106 this.staticMarkup = document.createElement('span');
107 this.staticMarkup.innerHTML = staticMarkup;
108 this.eParentElement.appendChild(this.staticMarkup);
109 }
110 }
111 }
112 catch (e) {
113 // we tried - this can happen with certain (rare) edge cases
114 }
115 finally {
116 console.error = originalConsoleError;
117 }
118 };
119 LegacyReactComponent.prototype.removeStaticMarkup = function () {
120 if (this.parentComponent.isDisableStaticMarkup() || !this.componentType.isCellRenderer()) {
121 return;
122 }
123 if (this.staticMarkup) {
124 if (this.staticMarkup.remove) {
125 // everyone else in the world
126 this.staticMarkup.remove();
127 this.staticMarkup = null;
128 }
129 else if (this.eParentElement.removeChild) {
130 // ie11...
131 this.eParentElement.removeChild(this.staticMarkup);
132 this.staticMarkup = null;
133 }
134 }
135 };
136 LegacyReactComponent.prototype.rendered = function () {
137 return this.isNullValue() ||
138 !!this.staticMarkup || (this.isStatelessComponent() && this.statelessComponentRendered()) ||
139 !!(!this.isStatelessComponent() && this.getFrameworkComponentInstance());
140 };
141 LegacyReactComponent.SLOW_RENDERERING_THRESHOLD = 3;
142 return LegacyReactComponent;
143}(reactComponent_1.ReactComponent));
144exports.LegacyReactComponent = LegacyReactComponent;