@gravity-ui/dashkit 8.4.0 → 8.5.0-beta.1

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.
@@ -23,10 +23,12 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
23
23
  reloadItems(): void;
24
24
  _onStart: () => void;
25
25
  _onStop: (group: any, newLayout: any) => void;
26
- _onDropDragOver: (e: any) => any;
26
+ _onDropDragOver: (e: any, gridProperties: any, layout: any) => any;
27
27
  _onDrop: (group: any, newLayout: any, item: any, e: any) => false | undefined;
28
28
  renderTemporaryPlaceholder(): JSX.Element | null;
29
- renderGroup(group: any, renderLayout: any, renderItems: any, offset?: number): JSX.Element;
29
+ caclculateEmptyHorizontalSpace(layout: any, cols: any): any;
30
+ getElementLayout(layout: any, properties: any): any;
31
+ renderGroup(group: any, renderLayout: any, renderItems: any, offset: number | undefined, groupGridProperties: any): JSX.Element;
30
32
  render(): any;
31
33
  }
32
34
  import React from 'react';
@@ -46,12 +46,20 @@ class GridLayout extends react_1.default.PureComponent {
46
46
  }
47
47
  this.setState({ isDragging: false });
48
48
  };
49
- this._onDropDragOver = (e) => {
49
+ this._onDropDragOver = (e, gridProperties, layout) => {
50
50
  const { editMode, dragOverPlugin, onDropDragOver } = this.context;
51
51
  if (!editMode || !dragOverPlugin) {
52
52
  return false;
53
53
  }
54
- return onDropDragOver(e);
54
+ let maxW = gridProperties.cols;
55
+ const maxH = gridProperties.maxRows;
56
+ if (gridProperties.compactType === constants_1.COMPACT_TYPE_HORIZONTAL_NOWRAP) {
57
+ maxW = this.caclculateEmptyHorizontalSpace(layout, gridProperties.cols);
58
+ }
59
+ if (maxW === 0 || maxH === 0) {
60
+ return false;
61
+ }
62
+ return onDropDragOver(e, { maxH, maxW });
55
63
  };
56
64
  this._onDrop = (group, newLayout, item, e) => {
57
65
  if (!item) {
@@ -131,14 +139,33 @@ class GridLayout extends react_1.default.PureComponent {
131
139
  const { type } = temporaryLayout.dragProps;
132
140
  return (react_1.default.createElement(GridItem_1.default, { key: id, id: id, item: { id, type, data: {} }, layout: temporaryLayout.data, adjustWidgetLayout: this.adjustWidgetLayout, isDragging: this.state.isDragging, isPlaceholder: true, noOverlay: noOverlay, withCustomHandle: Boolean(draggableHandleClassName), overlayControls: this.props.overlayControls }));
133
141
  }
134
- renderGroup(group, renderLayout, renderItems, offset = 0) {
142
+ caclculateEmptyHorizontalSpace(layout, cols) {
143
+ return layout.reduce((memo, item) => memo - item.w, cols);
144
+ }
145
+ getElementLayout(layout, properties) {
146
+ let maxH;
147
+ if (properties.maxRows) {
148
+ maxH = properties.maxRows;
149
+ }
150
+ const leftSpace = this.caclculateEmptyHorizontalSpace(layout, properties.cols);
151
+ return layout.map((item) => (Object.assign(Object.assign({}, item), { maxW: item.w + leftSpace, maxH })));
152
+ }
153
+ renderGroup(group, renderLayout, renderItems, offset = 0, groupGridProperties) {
135
154
  const { registerManager, editMode, noOverlay, focusable, draggableHandleClassName, outerDnDEnable, } = this.context;
136
- return (react_1.default.createElement(ReactGridLayout_1.Layout, Object.assign({}, registerManager.gridLayout, { key: `group_${group}`, layout: renderLayout, isDraggable: editMode, isResizable: editMode, onDragStart: this._onStart, onDragStop: (...args) => this._onStop(group, ...args), onResizeStart: this._onStart, onResizeStop: (...args) => this._onStop(group, ...args) }, (draggableHandleClassName
155
+ const properties = groupGridProperties
156
+ ? groupGridProperties(Object.assign({}, registerManager.gridLayout))
157
+ : registerManager.gridLayout;
158
+ let { compactType } = properties;
159
+ if (compactType === constants_1.COMPACT_TYPE_HORIZONTAL_NOWRAP) {
160
+ renderLayout = this.getElementLayout(renderLayout, properties);
161
+ compactType = 'horizontal';
162
+ }
163
+ return (react_1.default.createElement(ReactGridLayout_1.Layout, Object.assign({}, properties, { compactType: compactType, layout: renderLayout, key: `group_${group}`, isDraggable: editMode, isResizable: editMode, onDragStart: this._onStart, onDragStop: (...args) => this._onStop(group, ...args), onResizeStart: this._onStart, onResizeStop: (...args) => this._onStop(group, ...args) }, (draggableHandleClassName
137
164
  ? { draggableHandle: `.${draggableHandleClassName}` }
138
165
  : null), (outerDnDEnable
139
166
  ? {
140
167
  isDroppable: true,
141
- onDropDragOver: this._onDropDragOver,
168
+ onDropDragOver: (e) => this._onDropDragOver(e, properties, renderLayout),
142
169
  onDrop: (...args) => this._onDrop(group, ...args),
143
170
  }
144
171
  : null), { draggableCancel: `.${constants_1.OVERLAY_CONTROLS_CLASS_NAME}` }),
@@ -194,7 +221,7 @@ class GridLayout extends react_1.default.PureComponent {
194
221
  layout = groupedLayout[id] || [];
195
222
  items = itemsByGroup[id] || [];
196
223
  }
197
- const element = this.renderGroup(id, layout, items, offset);
224
+ const element = this.renderGroup(id, layout, items, offset, group.gridProperties);
198
225
  offset += items.length;
199
226
  if (group.render) {
200
227
  return group.render(id, element, {
@@ -11,3 +11,4 @@ export declare const MenuItems: {
11
11
  export declare const DEFAULT_WIDGET_HEIGHT = 3;
12
12
  export declare const DEFAULT_WIDGET_WIDTH = 3;
13
13
  export declare const DEFAULT_GROUP = "__default";
14
+ export declare const COMPACT_TYPE_HORIZONTAL_NOWRAP = "horizontal-nowrap";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_GROUP = exports.DEFAULT_WIDGET_WIDTH = exports.DEFAULT_WIDGET_HEIGHT = exports.MenuItems = exports.TEMPORARY_ITEM_ID = exports.OVERLAY_CLASS_NAME = exports.OVERLAY_CONTROLS_CLASS_NAME = exports.DEFAULT_NAMESPACE = exports.COPIED_WIDGET_STORE_KEY = void 0;
3
+ exports.COMPACT_TYPE_HORIZONTAL_NOWRAP = exports.DEFAULT_GROUP = exports.DEFAULT_WIDGET_WIDTH = exports.DEFAULT_WIDGET_HEIGHT = exports.MenuItems = exports.TEMPORARY_ITEM_ID = exports.OVERLAY_CLASS_NAME = exports.OVERLAY_CONTROLS_CLASS_NAME = exports.DEFAULT_NAMESPACE = exports.COPIED_WIDGET_STORE_KEY = void 0;
4
4
  exports.COPIED_WIDGET_STORE_KEY = 'dashCopiedItem';
5
5
  exports.DEFAULT_NAMESPACE = 'default';
6
6
  exports.OVERLAY_CONTROLS_CLASS_NAME = 'dashkit-overlay-controls';
@@ -14,3 +14,4 @@ exports.MenuItems = {
14
14
  exports.DEFAULT_WIDGET_HEIGHT = 3;
15
15
  exports.DEFAULT_WIDGET_WIDTH = 3;
16
16
  exports.DEFAULT_GROUP = '__default';
17
+ exports.COMPACT_TYPE_HORIZONTAL_NOWRAP = 'horizontal-nowrap';
@@ -136,7 +136,15 @@ function useMemoStateContext(props) {
136
136
  const resultLayout = react_1.default.useMemo(() => {
137
137
  return props.layout.map((item) => {
138
138
  if (item.i in adjustedLayouts.current) {
139
- return Object.assign(Object.assign({}, adjustedLayouts.current[item.i]), { w: item.w, x: item.x, y: item.y });
139
+ // eslint-disable-next-line no-unused-vars
140
+ const _a = adjustedLayouts.current[item.i] || {}, { parent } = _a, adjustedItem = tslib_1.__rest(_a, ["parent"]);
141
+ adjustedItem.w = item.w;
142
+ adjustedItem.x = item.x;
143
+ adjustedItem.y = item.y;
144
+ if (item.parent) {
145
+ adjustedItem.parent = item.parent;
146
+ }
147
+ return adjustedItem;
140
148
  }
141
149
  else {
142
150
  return Object.assign({}, item);
@@ -161,7 +169,7 @@ function useMemoStateContext(props) {
161
169
  return null;
162
170
  }
163
171
  }, [dragProps, props.registerManager]);
164
- const onDropDragOver = react_1.default.useCallback(() => {
172
+ const onDropDragOver = react_1.default.useCallback((_e, gridProps = {}) => {
165
173
  if (temporaryLayout) {
166
174
  resetTemporaryLayout();
167
175
  return false;
@@ -169,7 +177,10 @@ function useMemoStateContext(props) {
169
177
  if (dragOverPlugin) {
170
178
  const { defaultLayout } = dragOverPlugin;
171
179
  const { h = (defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.h) || common_1.DEFAULT_WIDGET_HEIGHT, w = (defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.w) || common_1.DEFAULT_WIDGET_WIDTH, } = dragProps.layout || {};
172
- return { h, w };
180
+ return {
181
+ h: gridProps.maxH ? Math.min(h, gridProps.maxH) : h,
182
+ w: gridProps.maxW ? Math.min(w, gridProps.maxW) : w,
183
+ };
173
184
  }
174
185
  return false;
175
186
  }, [resetTemporaryLayout, temporaryLayout, dragOverPlugin, dragProps]);
@@ -1,3 +1,4 @@
1
+ /// <reference types="react-grid-layout" />
1
2
  /// <reference types="react" />
2
3
  import type { Config, ConfigItem, ConfigLayout } from '../shared';
3
4
  export interface AddConfigItem extends Omit<ConfigItem, 'id' | 'namespace'> {
@@ -18,7 +19,11 @@ export interface DashkitGroupRenderProps {
18
19
  items: ConfigItem[];
19
20
  layout: ConfigLayout[];
20
21
  }
22
+ export type ReactGridLayoutProps = Omit<ReactGridLayout.ReactGridLayoutProps, 'children' | 'compactType' | 'innerRef' | 'key' | 'layout' | 'isDraggable' | 'isResizable' | 'onDragStart' | 'onDragStop' | 'onResizeStart' | 'onResizeStop' | 'draggableHandle' | 'isDroppable' | 'onDropDragOver' | 'onDrop' | 'draggableCancel'> & {
23
+ compactType?: ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
24
+ };
21
25
  export interface DashKitGroup {
22
26
  id?: string;
23
27
  render?: (id: string, children: React.ReactNode, props: DashkitGroupRenderProps) => React.ReactNode;
28
+ gridProperties?: (props: ReactGridLayoutProps) => ReactGridLayoutProps;
24
29
  }
@@ -23,10 +23,12 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
23
23
  reloadItems(): void;
24
24
  _onStart: () => void;
25
25
  _onStop: (group: any, newLayout: any) => void;
26
- _onDropDragOver: (e: any) => any;
26
+ _onDropDragOver: (e: any, gridProperties: any, layout: any) => any;
27
27
  _onDrop: (group: any, newLayout: any, item: any, e: any) => false | undefined;
28
28
  renderTemporaryPlaceholder(): JSX.Element | null;
29
- renderGroup(group: any, renderLayout: any, renderItems: any, offset?: number): JSX.Element;
29
+ caclculateEmptyHorizontalSpace(layout: any, cols: any): any;
30
+ getElementLayout(layout: any, properties: any): any;
31
+ renderGroup(group: any, renderLayout: any, renderItems: any, offset: number | undefined, groupGridProperties: any): JSX.Element;
30
32
  render(): any;
31
33
  }
32
34
  import React from 'react';
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { DEFAULT_GROUP, OVERLAY_CONTROLS_CLASS_NAME, TEMPORARY_ITEM_ID } from '../../constants';
2
+ import { COMPACT_TYPE_HORIZONTAL_NOWRAP, DEFAULT_GROUP, OVERLAY_CONTROLS_CLASS_NAME, TEMPORARY_ITEM_ID, } from '../../constants';
3
3
  import { DashKitContext } from '../../context/DashKitContext';
4
4
  import GridItem from '../GridItem/GridItem';
5
5
  import { Layout } from './ReactGridLayout';
@@ -43,12 +43,20 @@ class GridLayout extends React.PureComponent {
43
43
  }
44
44
  this.setState({ isDragging: false });
45
45
  };
46
- this._onDropDragOver = (e) => {
46
+ this._onDropDragOver = (e, gridProperties, layout) => {
47
47
  const { editMode, dragOverPlugin, onDropDragOver } = this.context;
48
48
  if (!editMode || !dragOverPlugin) {
49
49
  return false;
50
50
  }
51
- return onDropDragOver(e);
51
+ let maxW = gridProperties.cols;
52
+ const maxH = gridProperties.maxRows;
53
+ if (gridProperties.compactType === COMPACT_TYPE_HORIZONTAL_NOWRAP) {
54
+ maxW = this.caclculateEmptyHorizontalSpace(layout, gridProperties.cols);
55
+ }
56
+ if (maxW === 0 || maxH === 0) {
57
+ return false;
58
+ }
59
+ return onDropDragOver(e, { maxH, maxW });
52
60
  };
53
61
  this._onDrop = (group, newLayout, item, e) => {
54
62
  if (!item) {
@@ -128,14 +136,33 @@ class GridLayout extends React.PureComponent {
128
136
  const { type } = temporaryLayout.dragProps;
129
137
  return (React.createElement(GridItem, { key: id, id: id, item: { id, type, data: {} }, layout: temporaryLayout.data, adjustWidgetLayout: this.adjustWidgetLayout, isDragging: this.state.isDragging, isPlaceholder: true, noOverlay: noOverlay, withCustomHandle: Boolean(draggableHandleClassName), overlayControls: this.props.overlayControls }));
130
138
  }
131
- renderGroup(group, renderLayout, renderItems, offset = 0) {
139
+ caclculateEmptyHorizontalSpace(layout, cols) {
140
+ return layout.reduce((memo, item) => memo - item.w, cols);
141
+ }
142
+ getElementLayout(layout, properties) {
143
+ let maxH;
144
+ if (properties.maxRows) {
145
+ maxH = properties.maxRows;
146
+ }
147
+ const leftSpace = this.caclculateEmptyHorizontalSpace(layout, properties.cols);
148
+ return layout.map((item) => (Object.assign(Object.assign({}, item), { maxW: item.w + leftSpace, maxH })));
149
+ }
150
+ renderGroup(group, renderLayout, renderItems, offset = 0, groupGridProperties) {
132
151
  const { registerManager, editMode, noOverlay, focusable, draggableHandleClassName, outerDnDEnable, } = this.context;
133
- return (React.createElement(Layout, Object.assign({}, registerManager.gridLayout, { key: `group_${group}`, layout: renderLayout, isDraggable: editMode, isResizable: editMode, onDragStart: this._onStart, onDragStop: (...args) => this._onStop(group, ...args), onResizeStart: this._onStart, onResizeStop: (...args) => this._onStop(group, ...args) }, (draggableHandleClassName
152
+ const properties = groupGridProperties
153
+ ? groupGridProperties(Object.assign({}, registerManager.gridLayout))
154
+ : registerManager.gridLayout;
155
+ let { compactType } = properties;
156
+ if (compactType === COMPACT_TYPE_HORIZONTAL_NOWRAP) {
157
+ renderLayout = this.getElementLayout(renderLayout, properties);
158
+ compactType = 'horizontal';
159
+ }
160
+ return (React.createElement(Layout, Object.assign({}, properties, { compactType: compactType, layout: renderLayout, key: `group_${group}`, isDraggable: editMode, isResizable: editMode, onDragStart: this._onStart, onDragStop: (...args) => this._onStop(group, ...args), onResizeStart: this._onStart, onResizeStop: (...args) => this._onStop(group, ...args) }, (draggableHandleClassName
134
161
  ? { draggableHandle: `.${draggableHandleClassName}` }
135
162
  : null), (outerDnDEnable
136
163
  ? {
137
164
  isDroppable: true,
138
- onDropDragOver: this._onDropDragOver,
165
+ onDropDragOver: (e) => this._onDropDragOver(e, properties, renderLayout),
139
166
  onDrop: (...args) => this._onDrop(group, ...args),
140
167
  }
141
168
  : null), { draggableCancel: `.${OVERLAY_CONTROLS_CLASS_NAME}` }),
@@ -191,7 +218,7 @@ class GridLayout extends React.PureComponent {
191
218
  layout = groupedLayout[id] || [];
192
219
  items = itemsByGroup[id] || [];
193
220
  }
194
- const element = this.renderGroup(id, layout, items, offset);
221
+ const element = this.renderGroup(id, layout, items, offset, group.gridProperties);
195
222
  offset += items.length;
196
223
  if (group.render) {
197
224
  return group.render(id, element, {
@@ -11,3 +11,4 @@ export declare const MenuItems: {
11
11
  export declare const DEFAULT_WIDGET_HEIGHT = 3;
12
12
  export declare const DEFAULT_WIDGET_WIDTH = 3;
13
13
  export declare const DEFAULT_GROUP = "__default";
14
+ export declare const COMPACT_TYPE_HORIZONTAL_NOWRAP = "horizontal-nowrap";
@@ -11,3 +11,4 @@ export const MenuItems = {
11
11
  export const DEFAULT_WIDGET_HEIGHT = 3;
12
12
  export const DEFAULT_WIDGET_WIDTH = 3;
13
13
  export const DEFAULT_GROUP = '__default';
14
+ export const COMPACT_TYPE_HORIZONTAL_NOWRAP = 'horizontal-nowrap';
@@ -1,3 +1,4 @@
1
+ import { __rest } from "tslib";
1
2
  import React from 'react';
2
3
  import isEqual from 'lodash/isEqual';
3
4
  import pick from 'lodash/pick';
@@ -132,7 +133,15 @@ function useMemoStateContext(props) {
132
133
  const resultLayout = React.useMemo(() => {
133
134
  return props.layout.map((item) => {
134
135
  if (item.i in adjustedLayouts.current) {
135
- return Object.assign(Object.assign({}, adjustedLayouts.current[item.i]), { w: item.w, x: item.x, y: item.y });
136
+ // eslint-disable-next-line no-unused-vars
137
+ const _a = adjustedLayouts.current[item.i] || {}, { parent } = _a, adjustedItem = __rest(_a, ["parent"]);
138
+ adjustedItem.w = item.w;
139
+ adjustedItem.x = item.x;
140
+ adjustedItem.y = item.y;
141
+ if (item.parent) {
142
+ adjustedItem.parent = item.parent;
143
+ }
144
+ return adjustedItem;
136
145
  }
137
146
  else {
138
147
  return Object.assign({}, item);
@@ -157,7 +166,7 @@ function useMemoStateContext(props) {
157
166
  return null;
158
167
  }
159
168
  }, [dragProps, props.registerManager]);
160
- const onDropDragOver = React.useCallback(() => {
169
+ const onDropDragOver = React.useCallback((_e, gridProps = {}) => {
161
170
  if (temporaryLayout) {
162
171
  resetTemporaryLayout();
163
172
  return false;
@@ -165,7 +174,10 @@ function useMemoStateContext(props) {
165
174
  if (dragOverPlugin) {
166
175
  const { defaultLayout } = dragOverPlugin;
167
176
  const { h = (defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.h) || DEFAULT_WIDGET_HEIGHT, w = (defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.w) || DEFAULT_WIDGET_WIDTH, } = dragProps.layout || {};
168
- return { h, w };
177
+ return {
178
+ h: gridProps.maxH ? Math.min(h, gridProps.maxH) : h,
179
+ w: gridProps.maxW ? Math.min(w, gridProps.maxW) : w,
180
+ };
169
181
  }
170
182
  return false;
171
183
  }, [resetTemporaryLayout, temporaryLayout, dragOverPlugin, dragProps]);
@@ -1,3 +1,4 @@
1
+ /// <reference types="react-grid-layout" />
1
2
  /// <reference types="react" />
2
3
  import type { Config, ConfigItem, ConfigLayout } from '../shared';
3
4
  export interface AddConfigItem extends Omit<ConfigItem, 'id' | 'namespace'> {
@@ -18,7 +19,11 @@ export interface DashkitGroupRenderProps {
18
19
  items: ConfigItem[];
19
20
  layout: ConfigLayout[];
20
21
  }
22
+ export type ReactGridLayoutProps = Omit<ReactGridLayout.ReactGridLayoutProps, 'children' | 'compactType' | 'innerRef' | 'key' | 'layout' | 'isDraggable' | 'isResizable' | 'onDragStart' | 'onDragStop' | 'onResizeStart' | 'onResizeStop' | 'draggableHandle' | 'isDroppable' | 'onDropDragOver' | 'onDrop' | 'draggableCancel'> & {
23
+ compactType?: ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
24
+ };
21
25
  export interface DashKitGroup {
22
26
  id?: string;
23
27
  render?: (id: string, children: React.ReactNode, props: DashkitGroupRenderProps) => React.ReactNode;
28
+ gridProperties?: (props: ReactGridLayoutProps) => ReactGridLayoutProps;
24
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dashkit",
3
- "version": "8.4.0",
3
+ "version": "8.5.0-beta.1",
4
4
  "description": "Library for rendering dashboard grid layout",
5
5
  "license": "MIT",
6
6
  "repository": {