@gravity-ui/dashkit 8.17.1 → 8.17.3

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.
@@ -81,33 +81,6 @@ class GridItem extends react_1.default.PureComponent {
81
81
  this.controller = null;
82
82
  });
83
83
  };
84
- this.onMountChange = (isMounted) => {
85
- var _a, _b, _c, _d, _e, _f;
86
- if (isMounted) {
87
- this._inited = true;
88
- (_b = (_a = this.props).onItemMountChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item, {
89
- isAsync: this._isAsyncItem,
90
- isMounted: isMounted,
91
- });
92
- if (!this._isAsyncItem) {
93
- (_d = (_c = this.props).onItemRender) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.item);
94
- }
95
- }
96
- else {
97
- (_f = (_e = this.props).onItemMountChange) === null || _f === void 0 ? void 0 : _f.call(_e, this.props.item, {
98
- isAsync: this._isAsyncItem,
99
- isMounted: isMounted,
100
- });
101
- }
102
- };
103
- this.onBeforeLoad = () => {
104
- this._isAsyncItem = true;
105
- return this.onLoad;
106
- };
107
- this.onLoad = () => {
108
- var _a, _b;
109
- (_b = (_a = this.props).onItemRender) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item);
110
- };
111
84
  }
112
85
  renderOverlay() {
113
86
  const { isPlaceholder } = this.props;
@@ -152,7 +125,7 @@ class GridItem extends react_1.default.PureComponent {
152
125
  }
153
126
  : {})),
154
127
  react_1.default.createElement("div", { className: b('item', { editMode: editMode && !_editActive && !noOverlay }) },
155
- react_1.default.createElement(Item_1.default, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef, onMountChange: this.onMountChange, onBeforeLoad: this.onBeforeLoad })),
128
+ react_1.default.createElement(Item_1.default, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef, onItemMountChange: this.props.onItemMountChange, onItemRender: this.props.onItemRender })),
156
129
  !noOverlay && this.renderOverlay(),
157
130
  preparedChildren));
158
131
  }
@@ -166,6 +139,8 @@ GridItem.propTypes = {
166
139
  forwardedRef: prop_types_1.default.any,
167
140
  forwardedPluginRef: prop_types_1.default.any,
168
141
  isPlaceholder: prop_types_1.default.bool,
142
+ onItemMountChange: prop_types_1.default.func,
143
+ onItemRender: prop_types_1.default.func,
169
144
  // from react-grid-layout:
170
145
  children: prop_types_1.default.node,
171
146
  className: prop_types_1.default.string,
@@ -61,9 +61,9 @@ declare const _default: {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
- onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
+ onItemRender: PropTypes.Requireable<(...args: any[]) => any>;
65
+ onItemMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
66
  forwardedPluginRef: PropTypes.Requireable<any>;
66
- onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
67
67
  };
68
68
  contextType: React.Context<any>;
69
69
  };
@@ -7,26 +7,56 @@ const prepareItem_1 = require("../../hocs/prepareItem");
7
7
  const cn_1 = require("../../utils/cn");
8
8
  const b = (0, cn_1.cn)('dashkit-item');
9
9
  // TODO: getDerivedStateFromError и заглушка с ошибкой
10
- const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef, onMountChange, }) => {
10
+ const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef, onItemRender, onItemMountChange, item, }) => {
11
11
  var _a, _b;
12
+ // to avoid too frequent re-creation of functions that do not affect the rendering
13
+ const isAsyncItemRef = react_1.default.useRef(false);
14
+ const itemRef = react_1.default.useRef(item);
15
+ const onItemRenderRef = react_1.default.useRef(onItemRender);
16
+ const onItemMountChangeRef = react_1.default.useRef(onItemMountChange);
17
+ itemRef.current = item;
18
+ onItemRenderRef.current = onItemRender;
19
+ onItemMountChangeRef.current = onItemMountChange;
12
20
  const isRegisteredType = registerManager.check(type);
13
21
  react_1.default.useLayoutEffect(() => {
22
+ var _a, _b;
14
23
  if (isRegisteredType && !isPlaceholder) {
15
- onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(true);
24
+ (_a = onItemMountChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemMountChangeRef, itemRef.current, {
25
+ isAsync: isAsyncItemRef.current,
26
+ isMounted: true,
27
+ });
28
+ if (!isAsyncItemRef.current) {
29
+ (_b = onItemRenderRef.current) === null || _b === void 0 ? void 0 : _b.call(onItemRenderRef, itemRef.current);
30
+ }
16
31
  return () => {
17
- onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(false);
32
+ var _a;
33
+ (_a = onItemMountChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemMountChangeRef, itemRef.current, {
34
+ isAsync: isAsyncItemRef.current,
35
+ isMounted: false,
36
+ });
18
37
  };
19
38
  }
20
39
  }, []);
40
+ const onLoad = react_1.default.useCallback(() => {
41
+ var _a;
42
+ (_a = onItemRenderRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemRenderRef, itemRef.current);
43
+ }, []);
44
+ const onBeforeLoad = react_1.default.useCallback(() => {
45
+ isAsyncItemRef.current = true;
46
+ return onLoad;
47
+ }, [onLoad]);
48
+ const itemRendererProps = react_1.default.useMemo(() => {
49
+ return Object.assign(Object.assign({}, rendererProps), { onBeforeLoad });
50
+ }, [rendererProps, onBeforeLoad]);
21
51
  if (!isRegisteredType) {
22
52
  console.warn(`type [${type}] не зарегистрирован`);
23
53
  return null;
24
54
  }
25
55
  if (isPlaceholder) {
26
56
  return (react_1.default.createElement("div", { className: b('placeholder') }, ((_b = (_a = registerManager
27
- .getItem(type)).placeholderRenderer) === null || _b === void 0 ? void 0 : _b.call(_a, rendererProps, forwardedPluginRef)) || null));
57
+ .getItem(type)).placeholderRenderer) === null || _b === void 0 ? void 0 : _b.call(_a, itemRendererProps, forwardedPluginRef)) || null));
28
58
  }
29
- return (react_1.default.createElement("div", { className: b('renderer') }, registerManager.getItem(type).renderer(rendererProps, forwardedPluginRef)));
59
+ return (react_1.default.createElement("div", { className: b('renderer') }, registerManager.getItem(type).renderer(itemRendererProps, forwardedPluginRef)));
30
60
  };
31
61
  Item.propTypes = {
32
62
  forwardedPluginRef: prop_types_1.default.any,
@@ -34,7 +64,8 @@ Item.propTypes = {
34
64
  registerManager: prop_types_1.default.object,
35
65
  type: prop_types_1.default.string,
36
66
  isPlaceholder: prop_types_1.default.bool,
37
- onMountChange: prop_types_1.default.func,
38
- onBeforeLoad: prop_types_1.default.func,
67
+ onItemRender: prop_types_1.default.func,
68
+ onItemMountChange: prop_types_1.default.func,
69
+ item: prop_types_1.default.object,
39
70
  };
40
71
  exports.default = (0, prepareItem_1.prepareItem)(Item);
@@ -3,19 +3,21 @@ export default class MobileLayout extends React.PureComponent<any, any, any> {
3
3
  constructor(props: any);
4
4
  constructor(props: any, context: any);
5
5
  pluginsRefs: any[];
6
+ sortedLayoutItems: any;
7
+ _memoLayout: any;
8
+ _memoForwardedPluginRef: any[];
9
+ _memoAdjustWidgetLayout: any[];
6
10
  state: {
7
11
  activeTabId: null;
8
12
  indexesOfItemsWithActiveAutoheight: {};
9
13
  };
10
14
  componentDidUpdate(prevProps: any, prevState: any): void;
15
+ getSortedLayoutItems(): any;
16
+ getMemoForwardRefCallback: (refIndex: any) => any;
11
17
  adjustWidgetLayout: (index: any, { needSetDefault }: {
12
18
  needSetDefault: any;
13
19
  }) => void;
14
- onMountChange: (isMounted: any) => void;
15
- _inited: boolean | undefined;
16
- onBeforeLoad: () => () => void;
17
- _isAsyncItem: boolean | undefined;
18
- onLoad: () => void;
20
+ getMemoAdjustWidgetLayout: (index: any) => any;
19
21
  render(): JSX.Element;
20
22
  }
21
23
  import React from 'react';
@@ -11,10 +11,21 @@ class MobileLayout extends react_1.default.PureComponent {
11
11
  constructor() {
12
12
  super(...arguments);
13
13
  this.pluginsRefs = [];
14
+ this._memoLayout = this.context.layout;
15
+ this._memoForwardedPluginRef = [];
16
+ this._memoAdjustWidgetLayout = [];
14
17
  this.state = {
15
18
  activeTabId: null,
16
19
  indexesOfItemsWithActiveAutoheight: {},
17
20
  };
21
+ this.getMemoForwardRefCallback = (refIndex) => {
22
+ if (!this._memoForwardedPluginRef[refIndex]) {
23
+ this._memoForwardedPluginRef[refIndex] = (pluginRef) => {
24
+ this.pluginsRefs[refIndex] = pluginRef;
25
+ };
26
+ }
27
+ return this._memoForwardedPluginRef[refIndex];
28
+ };
18
29
  this.adjustWidgetLayout = (index, { needSetDefault }) => {
19
30
  if (needSetDefault) {
20
31
  const indexesOfItemsWithActiveAutoheight = Object.assign({}, this.state.indexesOfItemsWithActiveAutoheight);
@@ -27,32 +38,11 @@ class MobileLayout extends react_1.default.PureComponent {
27
38
  });
28
39
  }
29
40
  };
30
- this.onMountChange = (isMounted) => {
31
- var _a, _b, _c, _d, _e, _f;
32
- if (isMounted) {
33
- this._inited = true;
34
- (_b = (_a = this.context).onItemMountChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item, {
35
- isAsync: this._isAsyncItem,
36
- isMounted: isMounted,
37
- });
38
- if (!this._isAsyncItem) {
39
- (_d = (_c = this.context).onItemRender) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.item);
40
- }
41
- }
42
- else {
43
- (_f = (_e = this.context).onItemMountChange) === null || _f === void 0 ? void 0 : _f.call(_e, this.props.item, {
44
- isAsync: this._isAsyncItem,
45
- isMounted: isMounted,
46
- });
41
+ this.getMemoAdjustWidgetLayout = (index) => {
42
+ if (!this._memoAdjustWidgetLayout[index]) {
43
+ this._memoAdjustWidgetLayout[index] = this.adjustWidgetLayout.bind(this, index);
47
44
  }
48
- };
49
- this.onBeforeLoad = () => {
50
- this._isAsyncItem = true;
51
- return this.onLoad;
52
- };
53
- this.onLoad = () => {
54
- var _a, _b;
55
- (_b = (_a = this.context).onItemRender) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item);
45
+ return this._memoAdjustWidgetLayout[index];
56
46
  };
57
47
  }
58
48
  componentDidUpdate(prevProps, prevState) {
@@ -63,17 +53,23 @@ class MobileLayout extends react_1.default.PureComponent {
63
53
  });
64
54
  }
65
55
  }
56
+ getSortedLayoutItems() {
57
+ if (this.sortedLayoutItems && this.context.layout === this._memoLayout) {
58
+ return this.sortedLayoutItems;
59
+ }
60
+ this._memoLayout = this.context.layout;
61
+ const hasOrderId = Boolean(this.context.config.items.find((item) => item.orderId));
62
+ this.sortedLayoutItems = (0, helpers_1.getSortedConfigItems)(this.context.config, hasOrderId);
63
+ return this.sortedLayoutItems;
64
+ }
66
65
  render() {
67
66
  const { config, layout } = this.context;
68
67
  this.pluginsRefs.length = config.items.length;
69
- const hasOrderId = Boolean(config.items.find((item) => item.orderId));
70
- const sortedItems = (0, helpers_1.getSortedConfigItems)(config, hasOrderId);
68
+ const sortedItems = this.getSortedLayoutItems();
71
69
  return (react_1.default.createElement("div", { className: b() }, sortedItems.map((item, index) => {
72
70
  const isItemWithActiveAutoheight = index in this.state.indexesOfItemsWithActiveAutoheight;
73
71
  return (react_1.default.createElement("div", { className: b('item', { autoheight: isItemWithActiveAutoheight }), key: item.id },
74
- react_1.default.createElement(Item_1.default, { id: item.id, item: item, layout: layout, shouldItemUpdate: false, adjustWidgetLayout: this.adjustWidgetLayout.bind(this, index), forwardedPluginRef: (pluginRef) => {
75
- this.pluginsRefs[index] = pluginRef;
76
- }, onMountChange: this.onMountChange, onBeforeLoad: this.onBeforeLoad })));
72
+ react_1.default.createElement(Item_1.default, { id: item.id, item: item, layout: layout, shouldItemUpdate: false, adjustWidgetLayout: this.getMemoAdjustWidgetLayout(index), forwardedPluginRef: this.getMemoForwardRefCallback(index), onMountChange: this.onMountChange, onItemMountChange: this.context.onItemMountChange, onItemRender: this.context.onItemRender })));
77
73
  })));
78
74
  }
79
75
  }
@@ -61,9 +61,9 @@ export function prepareItem(Component: any): {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
- onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
+ onItemRender: PropTypes.Requireable<(...args: any[]) => any>;
65
+ onItemMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
66
  forwardedPluginRef: PropTypes.Requireable<any>;
66
- onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
67
67
  };
68
68
  contextType: React.Context<any>;
69
69
  };
@@ -15,7 +15,7 @@ function prepareItem(Component) {
15
15
  };
16
16
  this._currentRenderProps = {};
17
17
  this.getRenderProps = () => {
18
- const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder, onBeforeLoad, } = this.props;
18
+ const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder } = this.props;
19
19
  const { itemsState, itemsParams, registerManager, settings, context, editMode } = this.context;
20
20
  const { data, defaults, namespace } = item;
21
21
  const rendererProps = {
@@ -24,7 +24,6 @@ function prepareItem(Component) {
24
24
  params: itemsParams[id],
25
25
  state: itemsState[id],
26
26
  onStateAndParamsChange: this._onStateAndParamsChange,
27
- onBeforeLoad,
28
27
  width,
29
28
  height,
30
29
  id,
@@ -56,10 +55,10 @@ function prepareItem(Component) {
56
55
  return true;
57
56
  }
58
57
  render() {
59
- const { item, isPlaceholder, forwardedPluginRef, onMountChange } = this.props;
58
+ const { item, isPlaceholder, forwardedPluginRef, onItemMountChange, onItemRender } = this.props;
60
59
  const { registerManager } = this.context;
61
60
  const { type } = item;
62
- return (react_1.default.createElement(Component, { forwardedPluginRef: forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder, onMountChange: onMountChange }));
61
+ return (react_1.default.createElement(Component, { forwardedPluginRef: forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder, onItemMountChange: onItemMountChange, onItemRender: onItemRender, item: item }));
63
62
  }
64
63
  },
65
64
  _a.propTypes = {
@@ -72,9 +71,9 @@ function prepareItem(Component) {
72
71
  height: prop_types_1.default.number,
73
72
  transform: prop_types_1.default.string,
74
73
  isPlaceholder: prop_types_1.default.bool,
75
- onBeforeLoad: prop_types_1.default.func,
74
+ onItemRender: prop_types_1.default.func,
75
+ onItemMountChange: prop_types_1.default.func,
76
76
  forwardedPluginRef: prop_types_1.default.any,
77
- onMountChange: prop_types_1.default.func,
78
77
  },
79
78
  _a.contextType = DashKitContext_1.DashKitContext,
80
79
  _a;
@@ -79,33 +79,6 @@ class GridItem extends React.PureComponent {
79
79
  this.controller = null;
80
80
  });
81
81
  };
82
- this.onMountChange = (isMounted) => {
83
- var _a, _b, _c, _d, _e, _f;
84
- if (isMounted) {
85
- this._inited = true;
86
- (_b = (_a = this.props).onItemMountChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item, {
87
- isAsync: this._isAsyncItem,
88
- isMounted: isMounted,
89
- });
90
- if (!this._isAsyncItem) {
91
- (_d = (_c = this.props).onItemRender) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.item);
92
- }
93
- }
94
- else {
95
- (_f = (_e = this.props).onItemMountChange) === null || _f === void 0 ? void 0 : _f.call(_e, this.props.item, {
96
- isAsync: this._isAsyncItem,
97
- isMounted: isMounted,
98
- });
99
- }
100
- };
101
- this.onBeforeLoad = () => {
102
- this._isAsyncItem = true;
103
- return this.onLoad;
104
- };
105
- this.onLoad = () => {
106
- var _a, _b;
107
- (_b = (_a = this.props).onItemRender) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item);
108
- };
109
82
  }
110
83
  renderOverlay() {
111
84
  const { isPlaceholder } = this.props;
@@ -150,7 +123,7 @@ class GridItem extends React.PureComponent {
150
123
  }
151
124
  : {})),
152
125
  React.createElement("div", { className: b('item', { editMode: editMode && !_editActive && !noOverlay }) },
153
- React.createElement(Item, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef, onMountChange: this.onMountChange, onBeforeLoad: this.onBeforeLoad })),
126
+ React.createElement(Item, { id: this.props.id, item: this.props.item, shouldItemUpdate: !isDragging, width: width, height: height, transform: transform, isPlaceholder: isPlaceholder, adjustWidgetLayout: this.props.adjustWidgetLayout, layout: this.props.layout, forwardedPluginRef: this.props.forwardedPluginRef, onItemMountChange: this.props.onItemMountChange, onItemRender: this.props.onItemRender })),
154
127
  !noOverlay && this.renderOverlay(),
155
128
  preparedChildren));
156
129
  }
@@ -164,6 +137,8 @@ GridItem.propTypes = {
164
137
  forwardedRef: PropTypes.any,
165
138
  forwardedPluginRef: PropTypes.any,
166
139
  isPlaceholder: PropTypes.bool,
140
+ onItemMountChange: PropTypes.func,
141
+ onItemRender: PropTypes.func,
167
142
  // from react-grid-layout:
168
143
  children: PropTypes.node,
169
144
  className: PropTypes.string,
@@ -61,9 +61,9 @@ declare const _default: {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
- onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
+ onItemRender: PropTypes.Requireable<(...args: any[]) => any>;
65
+ onItemMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
66
  forwardedPluginRef: PropTypes.Requireable<any>;
66
- onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
67
67
  };
68
68
  contextType: React.Context<any>;
69
69
  };
@@ -5,26 +5,56 @@ import { cn } from '../../utils/cn';
5
5
  import './Item.css';
6
6
  const b = cn('dashkit-item');
7
7
  // TODO: getDerivedStateFromError и заглушка с ошибкой
8
- const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef, onMountChange, }) => {
8
+ const Item = ({ registerManager, rendererProps, type, isPlaceholder, forwardedPluginRef, onItemRender, onItemMountChange, item, }) => {
9
9
  var _a, _b;
10
+ // to avoid too frequent re-creation of functions that do not affect the rendering
11
+ const isAsyncItemRef = React.useRef(false);
12
+ const itemRef = React.useRef(item);
13
+ const onItemRenderRef = React.useRef(onItemRender);
14
+ const onItemMountChangeRef = React.useRef(onItemMountChange);
15
+ itemRef.current = item;
16
+ onItemRenderRef.current = onItemRender;
17
+ onItemMountChangeRef.current = onItemMountChange;
10
18
  const isRegisteredType = registerManager.check(type);
11
19
  React.useLayoutEffect(() => {
20
+ var _a, _b;
12
21
  if (isRegisteredType && !isPlaceholder) {
13
- onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(true);
22
+ (_a = onItemMountChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemMountChangeRef, itemRef.current, {
23
+ isAsync: isAsyncItemRef.current,
24
+ isMounted: true,
25
+ });
26
+ if (!isAsyncItemRef.current) {
27
+ (_b = onItemRenderRef.current) === null || _b === void 0 ? void 0 : _b.call(onItemRenderRef, itemRef.current);
28
+ }
14
29
  return () => {
15
- onMountChange === null || onMountChange === void 0 ? void 0 : onMountChange(false);
30
+ var _a;
31
+ (_a = onItemMountChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemMountChangeRef, itemRef.current, {
32
+ isAsync: isAsyncItemRef.current,
33
+ isMounted: false,
34
+ });
16
35
  };
17
36
  }
18
37
  }, []);
38
+ const onLoad = React.useCallback(() => {
39
+ var _a;
40
+ (_a = onItemRenderRef.current) === null || _a === void 0 ? void 0 : _a.call(onItemRenderRef, itemRef.current);
41
+ }, []);
42
+ const onBeforeLoad = React.useCallback(() => {
43
+ isAsyncItemRef.current = true;
44
+ return onLoad;
45
+ }, [onLoad]);
46
+ const itemRendererProps = React.useMemo(() => {
47
+ return Object.assign(Object.assign({}, rendererProps), { onBeforeLoad });
48
+ }, [rendererProps, onBeforeLoad]);
19
49
  if (!isRegisteredType) {
20
50
  console.warn(`type [${type}] не зарегистрирован`);
21
51
  return null;
22
52
  }
23
53
  if (isPlaceholder) {
24
54
  return (React.createElement("div", { className: b('placeholder') }, ((_b = (_a = registerManager
25
- .getItem(type)).placeholderRenderer) === null || _b === void 0 ? void 0 : _b.call(_a, rendererProps, forwardedPluginRef)) || null));
55
+ .getItem(type)).placeholderRenderer) === null || _b === void 0 ? void 0 : _b.call(_a, itemRendererProps, forwardedPluginRef)) || null));
26
56
  }
27
- return (React.createElement("div", { className: b('renderer') }, registerManager.getItem(type).renderer(rendererProps, forwardedPluginRef)));
57
+ return (React.createElement("div", { className: b('renderer') }, registerManager.getItem(type).renderer(itemRendererProps, forwardedPluginRef)));
28
58
  };
29
59
  Item.propTypes = {
30
60
  forwardedPluginRef: PropTypes.any,
@@ -32,7 +62,8 @@ Item.propTypes = {
32
62
  registerManager: PropTypes.object,
33
63
  type: PropTypes.string,
34
64
  isPlaceholder: PropTypes.bool,
35
- onMountChange: PropTypes.func,
36
- onBeforeLoad: PropTypes.func,
65
+ onItemRender: PropTypes.func,
66
+ onItemMountChange: PropTypes.func,
67
+ item: PropTypes.object,
37
68
  };
38
69
  export default prepareItem(Item);
@@ -3,19 +3,21 @@ export default class MobileLayout extends React.PureComponent<any, any, any> {
3
3
  constructor(props: any);
4
4
  constructor(props: any, context: any);
5
5
  pluginsRefs: any[];
6
+ sortedLayoutItems: any;
7
+ _memoLayout: any;
8
+ _memoForwardedPluginRef: any[];
9
+ _memoAdjustWidgetLayout: any[];
6
10
  state: {
7
11
  activeTabId: null;
8
12
  indexesOfItemsWithActiveAutoheight: {};
9
13
  };
10
14
  componentDidUpdate(prevProps: any, prevState: any): void;
15
+ getSortedLayoutItems(): any;
16
+ getMemoForwardRefCallback: (refIndex: any) => any;
11
17
  adjustWidgetLayout: (index: any, { needSetDefault }: {
12
18
  needSetDefault: any;
13
19
  }) => void;
14
- onMountChange: (isMounted: any) => void;
15
- _inited: boolean | undefined;
16
- onBeforeLoad: () => () => void;
17
- _isAsyncItem: boolean | undefined;
18
- onLoad: () => void;
20
+ getMemoAdjustWidgetLayout: (index: any) => any;
19
21
  render(): JSX.Element;
20
22
  }
21
23
  import React from 'react';
@@ -9,10 +9,21 @@ class MobileLayout extends React.PureComponent {
9
9
  constructor() {
10
10
  super(...arguments);
11
11
  this.pluginsRefs = [];
12
+ this._memoLayout = this.context.layout;
13
+ this._memoForwardedPluginRef = [];
14
+ this._memoAdjustWidgetLayout = [];
12
15
  this.state = {
13
16
  activeTabId: null,
14
17
  indexesOfItemsWithActiveAutoheight: {},
15
18
  };
19
+ this.getMemoForwardRefCallback = (refIndex) => {
20
+ if (!this._memoForwardedPluginRef[refIndex]) {
21
+ this._memoForwardedPluginRef[refIndex] = (pluginRef) => {
22
+ this.pluginsRefs[refIndex] = pluginRef;
23
+ };
24
+ }
25
+ return this._memoForwardedPluginRef[refIndex];
26
+ };
16
27
  this.adjustWidgetLayout = (index, { needSetDefault }) => {
17
28
  if (needSetDefault) {
18
29
  const indexesOfItemsWithActiveAutoheight = Object.assign({}, this.state.indexesOfItemsWithActiveAutoheight);
@@ -25,32 +36,11 @@ class MobileLayout extends React.PureComponent {
25
36
  });
26
37
  }
27
38
  };
28
- this.onMountChange = (isMounted) => {
29
- var _a, _b, _c, _d, _e, _f;
30
- if (isMounted) {
31
- this._inited = true;
32
- (_b = (_a = this.context).onItemMountChange) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item, {
33
- isAsync: this._isAsyncItem,
34
- isMounted: isMounted,
35
- });
36
- if (!this._isAsyncItem) {
37
- (_d = (_c = this.context).onItemRender) === null || _d === void 0 ? void 0 : _d.call(_c, this.props.item);
38
- }
39
- }
40
- else {
41
- (_f = (_e = this.context).onItemMountChange) === null || _f === void 0 ? void 0 : _f.call(_e, this.props.item, {
42
- isAsync: this._isAsyncItem,
43
- isMounted: isMounted,
44
- });
39
+ this.getMemoAdjustWidgetLayout = (index) => {
40
+ if (!this._memoAdjustWidgetLayout[index]) {
41
+ this._memoAdjustWidgetLayout[index] = this.adjustWidgetLayout.bind(this, index);
45
42
  }
46
- };
47
- this.onBeforeLoad = () => {
48
- this._isAsyncItem = true;
49
- return this.onLoad;
50
- };
51
- this.onLoad = () => {
52
- var _a, _b;
53
- (_b = (_a = this.context).onItemRender) === null || _b === void 0 ? void 0 : _b.call(_a, this.props.item);
43
+ return this._memoAdjustWidgetLayout[index];
54
44
  };
55
45
  }
56
46
  componentDidUpdate(prevProps, prevState) {
@@ -61,17 +51,23 @@ class MobileLayout extends React.PureComponent {
61
51
  });
62
52
  }
63
53
  }
54
+ getSortedLayoutItems() {
55
+ if (this.sortedLayoutItems && this.context.layout === this._memoLayout) {
56
+ return this.sortedLayoutItems;
57
+ }
58
+ this._memoLayout = this.context.layout;
59
+ const hasOrderId = Boolean(this.context.config.items.find((item) => item.orderId));
60
+ this.sortedLayoutItems = getSortedConfigItems(this.context.config, hasOrderId);
61
+ return this.sortedLayoutItems;
62
+ }
64
63
  render() {
65
64
  const { config, layout } = this.context;
66
65
  this.pluginsRefs.length = config.items.length;
67
- const hasOrderId = Boolean(config.items.find((item) => item.orderId));
68
- const sortedItems = getSortedConfigItems(config, hasOrderId);
66
+ const sortedItems = this.getSortedLayoutItems();
69
67
  return (React.createElement("div", { className: b() }, sortedItems.map((item, index) => {
70
68
  const isItemWithActiveAutoheight = index in this.state.indexesOfItemsWithActiveAutoheight;
71
69
  return (React.createElement("div", { className: b('item', { autoheight: isItemWithActiveAutoheight }), key: item.id },
72
- React.createElement(Item, { id: item.id, item: item, layout: layout, shouldItemUpdate: false, adjustWidgetLayout: this.adjustWidgetLayout.bind(this, index), forwardedPluginRef: (pluginRef) => {
73
- this.pluginsRefs[index] = pluginRef;
74
- }, onMountChange: this.onMountChange, onBeforeLoad: this.onBeforeLoad })));
70
+ React.createElement(Item, { id: item.id, item: item, layout: layout, shouldItemUpdate: false, adjustWidgetLayout: this.getMemoAdjustWidgetLayout(index), forwardedPluginRef: this.getMemoForwardRefCallback(index), onMountChange: this.onMountChange, onItemMountChange: this.context.onItemMountChange, onItemRender: this.context.onItemRender })));
75
71
  })));
76
72
  }
77
73
  }
@@ -61,9 +61,9 @@ export function prepareItem(Component: any): {
61
61
  height: PropTypes.Requireable<number>;
62
62
  transform: PropTypes.Requireable<string>;
63
63
  isPlaceholder: PropTypes.Requireable<boolean>;
64
- onBeforeLoad: PropTypes.Requireable<(...args: any[]) => any>;
64
+ onItemRender: PropTypes.Requireable<(...args: any[]) => any>;
65
+ onItemMountChange: PropTypes.Requireable<(...args: any[]) => any>;
65
66
  forwardedPluginRef: PropTypes.Requireable<any>;
66
- onMountChange: PropTypes.Requireable<(...args: any[]) => any>;
67
67
  };
68
68
  contextType: React.Context<any>;
69
69
  };
@@ -11,7 +11,7 @@ export function prepareItem(Component) {
11
11
  };
12
12
  this._currentRenderProps = {};
13
13
  this.getRenderProps = () => {
14
- const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder, onBeforeLoad, } = this.props;
14
+ const { id, width, height, item, adjustWidgetLayout, layout, isPlaceholder } = this.props;
15
15
  const { itemsState, itemsParams, registerManager, settings, context, editMode } = this.context;
16
16
  const { data, defaults, namespace } = item;
17
17
  const rendererProps = {
@@ -20,7 +20,6 @@ export function prepareItem(Component) {
20
20
  params: itemsParams[id],
21
21
  state: itemsState[id],
22
22
  onStateAndParamsChange: this._onStateAndParamsChange,
23
- onBeforeLoad,
24
23
  width,
25
24
  height,
26
25
  id,
@@ -52,10 +51,10 @@ export function prepareItem(Component) {
52
51
  return true;
53
52
  }
54
53
  render() {
55
- const { item, isPlaceholder, forwardedPluginRef, onMountChange } = this.props;
54
+ const { item, isPlaceholder, forwardedPluginRef, onItemMountChange, onItemRender } = this.props;
56
55
  const { registerManager } = this.context;
57
56
  const { type } = item;
58
- return (React.createElement(Component, { forwardedPluginRef: forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder, onMountChange: onMountChange }));
57
+ return (React.createElement(Component, { forwardedPluginRef: forwardedPluginRef, rendererProps: this.getRenderProps(), registerManager: registerManager, type: type, isPlaceholder: isPlaceholder, onItemMountChange: onItemMountChange, onItemRender: onItemRender, item: item }));
59
58
  }
60
59
  },
61
60
  _a.propTypes = {
@@ -68,9 +67,9 @@ export function prepareItem(Component) {
68
67
  height: PropTypes.number,
69
68
  transform: PropTypes.string,
70
69
  isPlaceholder: PropTypes.bool,
71
- onBeforeLoad: PropTypes.func,
70
+ onItemRender: PropTypes.func,
71
+ onItemMountChange: PropTypes.func,
72
72
  forwardedPluginRef: PropTypes.any,
73
- onMountChange: PropTypes.func,
74
73
  },
75
74
  _a.contextType = DashKitContext,
76
75
  _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dashkit",
3
- "version": "8.17.1",
3
+ "version": "8.17.3",
4
4
  "description": "Library for rendering dashboard grid layout",
5
5
  "license": "MIT",
6
6
  "repository": {