source: imaps-frontend/node_modules/react-tiles-dnd/esm/tile-layout/TilesContainer.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.5 KB
Line 
1import { __rest, __read, __assign } from 'tslib';
2import React, { useRef, useState, useEffect, useMemo } from 'react';
3import { useTileTable } from './useTileTable.js';
4import { useMeasure } from 'react-use';
5import styles from './TilesContainer.module.css.js';
6import { useComposeRef } from '../utils/useComposedRef.js';
7import { jc } from '../utils/joinclasses.js';
8import { isEqual } from 'lodash-es';
9
10var TileUI = React.memo(function (_a) {
11 var renderTile = _a.renderTile, props = __rest(_a, ["renderTile"]);
12 return renderTile(props);
13});
14var isTilesContainerForcedSizeProps = function (props) { return !!props.forceTileWidth; };
15var isTilesContainerColsProps = function (props) { return !!props.columns; };
16var defaultTileSize = function () { return ({ rowSpan: 1, colSpan: 1 }); };
17var lastKey = 0;
18var dataWithKeys = function (data, dataWithKeys) {
19 return data.map(function (item) {
20 var existingItem = dataWithKeys.find(function (k) { return k.item === item; });
21 return existingItem || { item: item, key: lastKey++ };
22 });
23};
24var TilesContainer = function (props) {
25 var containerRef = useRef(null);
26 var _a = __read(useMeasure(), 2), measureRef = _a[0], measure = _a[1];
27 var forceTileWidth = isTilesContainerForcedSizeProps(props)
28 ? props.forceTileWidth
29 : 0;
30 var columns = isTilesContainerColsProps(props)
31 ? props.columns
32 : forceTileWidth
33 ? Math.floor(measure.width / forceTileWidth)
34 : 4;
35 var onReorderTiles = props.onReorderTiles, _b = props.ratio, ratio = _b === void 0 ? 1 : _b, forceTileHeight = props.forceTileHeight, propsData = props.data, acceptsDrop = props.acceptsDrop, onTileDrop = props.onTileDrop, _c = props.tileSize, tileSize = _c === void 0 ? defaultTileSize : _c, renderTile = props.renderTile, renderInsertIndicator = props.renderInsertIndicator, _d = props.activeBorderSize, activeBorderSize = _d === void 0 ? 24 : _d, disabled = props.disabled, _e = props.strategy, strategy = _e === void 0 ? 'move' : _e;
36 var tileWidth = forceTileWidth || measure.width / columns;
37 var tileHeight = forceTileHeight || tileWidth / ratio;
38 var _f = __read(useState(function () {
39 return dataWithKeys(propsData, []);
40 }), 2), data = _f[0], setData = _f[1];
41 useEffect(function () { return setData(function (curr) { return dataWithKeys(propsData, curr); }); }, [propsData, setData]);
42 //extract the tiles from the props
43 var propsTiles = useMemo(function () {
44 var tiles = [];
45 data.forEach(function (_a) {
46 var tile = _a.item, key = _a.key;
47 return tiles.push(__assign(__assign({ key: key }, tileSize(tile)), { data: tile }));
48 });
49 return tiles;
50 }, [data, tileSize]);
51 var _g = useTileTable({
52 columns: columns,
53 strategy: strategy,
54 enabled: !disabled,
55 elementHeight: tileHeight,
56 elementWidth: tileWidth,
57 activeBorderSize: activeBorderSize,
58 currentTiles: propsTiles,
59 canAcceptDrop: function (source, target) {
60 return acceptsDrop ? acceptsDrop(source.data, target.data) : false;
61 },
62 changeTilesOrder: function (tiles) {
63 var newData = tiles.map(function (tile) { return tile.data; });
64 var actualData = data.map(function (tile) { return tile.item; });
65 if (!isEqual(actualData, newData)) {
66 setData(function (curr) { return dataWithKeys(newData, curr); });
67 onReorderTiles && onReorderTiles(newData);
68 }
69 },
70 didDrop: function (source, target) {
71 setData(function (tiles) { return tiles.filter(function (tile) { return tile.item !== source.data; }); });
72 onTileDrop && onTileDrop(source.data, target.data);
73 },
74 }), bind = _g.bind, renderTileProps = _g.renderTileProps, tableHeight = _g.tableHeight, insertIndicatorPosition = _g.insertIndicatorPosition;
75 var insertIndicator = useMemo(function () {
76 if (!insertIndicatorPosition || !renderInsertIndicator) {
77 return null;
78 }
79 var x = insertIndicatorPosition.x, y = insertIndicatorPosition.y;
80 return (React.createElement("div", { className: styles.indicator, style: {
81 top: y + "px",
82 left: x + "px",
83 height: tileHeight + "px",
84 width: tileWidth + "px",
85 } }, renderInsertIndicator()));
86 }, [insertIndicatorPosition, renderInsertIndicator, tileHeight, tileWidth]);
87 var tiles = useMemo(function () {
88 return renderTileProps.map(function (_a) {
89 var x = _a.x, y = _a.y, key = _a.key, props = __rest(_a, ["x", "y", "key"]);
90 return (React.createElement("div", __assign({ className: jc(styles.tile, props.isDragging && styles.dragging), key: "K-" + key, style: {
91 top: y + "px",
92 left: x + "px",
93 width: props.colSpan * tileWidth + "px",
94 height: props.rowSpan * tileHeight + "px",
95 } }, bind(props.data)),
96 React.createElement(TileUI, __assign({}, props, { renderTile: renderTile }))));
97 });
98 }, [renderTileProps, renderTile, bind, tileWidth, tileHeight]);
99 return (React.createElement("div", { className: styles.container, ref: useComposeRef(containerRef, measureRef), style: { height: tableHeight + "px", minWidth: tileWidth + "px" } },
100 insertIndicator,
101 tiles));
102};
103
104export { TilesContainer };
105//# sourceMappingURL=TilesContainer.js.map
Note: See TracBrowser for help on using the repository browser.