@egjs/react-infinitegrid
Version:
A React component that can arrange items infinitely according to the type of grids
310 lines (293 loc) • 12.5 kB
JavaScript
/*
Copyright (c) NAVER Corp.
name: @egjs/react-infinitegrid
license: MIT
author: NAVER Corp.
repository: https://github.com/naver/egjs-infinitegrid
version: 4.12.0
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react'), require('@egjs/infinitegrid')) :
typeof define === 'function' && define.amd ? define(['react', '@egjs/infinitegrid'], factory) :
(global = global || self, global.ReactInfiniteGrid = factory(global.React, global.InfiniteGrid));
}(this, (function (React, VanillaInfiniteGrid) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
};
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
}
function __decorate(decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var REACT_INFINITEGRID_EVENT_MAP = {
"onContentError": "contentError",
"onRenderComplete": "renderComplete",
"onRequestAppend": "requestAppend",
"onRequestPrepend": "requestPrepend",
"onChangeScroll": "changeScroll"
};
var REACT_INFINITEGRID_EVENTS = [];
for (var name in REACT_INFINITEGRID_EVENT_MAP) {
REACT_INFINITEGRID_EVENTS.push(name);
}
var REACT_INFINITEGRID_PROPS = __spreadArray(["tag", "placeholder", "status", "useFirstRender", "loading", "itemBy", "groupBy", "infoBy"], REACT_INFINITEGRID_EVENTS, true);
function isFunction(val) {
return typeof val === "function";
}
var InfiniteGrid = /*#__PURE__*/function (_super) {
__extends(InfiniteGrid, _super);
function InfiniteGrid() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._wrapperRef = React.createRef();
_this._containerRef = React.createRef();
return _this;
}
var __proto = InfiniteGrid.prototype;
__proto.render = function () {
var attributes = {};
var props = this.props;
var GridClass = this.constructor.GridClass;
var defaultOptions = GridClass.defaultOptions;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var Tag = props.tag || "div";
for (var name in props) {
if (name in defaultOptions || REACT_INFINITEGRID_PROPS.indexOf(name) > -1) {
continue;
}
attributes[name] = props[name];
}
return React.createElement(Tag, __assign({
ref: this._wrapperRef
}, attributes), this._renderContainer());
};
__proto.componentDidMount = function () {
var _this = this;
var GridClass = this.constructor.GridClass;
var defaultOptions = GridClass.defaultOptions;
var options = {};
var props = this.props;
var containerElement = this._containerRef.current;
for (var name in defaultOptions) {
if (name in props) {
options[name] = props[name];
}
}
if (containerElement) {
options.container = containerElement;
}
this._renderer = new VanillaInfiniteGrid.Renderer();
options.renderer = this._renderer;
var grid = new GridClass(this._wrapperRef.current, options);
var _loop_1 = function (eventName) {
var nativeEventName = REACT_INFINITEGRID_EVENT_MAP[eventName];
grid.on(nativeEventName, function (e) {
var callback = _this.props[eventName];
callback && callback(e);
});
};
for (var eventName in REACT_INFINITEGRID_EVENT_MAP) {
_loop_1(eventName);
}
this._grid = grid;
this._renderer.on("update", function () {
_this.setState({});
});
VanillaInfiniteGrid.mountRenderingItems(this._getItemInfos(), {
grid: grid,
useFirstRender: props.useFirstRender,
useLoading: props.loading,
usePlaceholder: props.placeholder,
horizontal: props.horizontal,
status: props.status
});
this._renderer.updated();
};
__proto.componentDidUpdate = function () {
var GridClass = this.constructor.GridClass;
var propertyTypes = GridClass.propertyTypes;
var props = this.props;
var grid = this._grid;
for (var name in propertyTypes) {
if (name in props) {
grid[name] = props[name];
}
}
this._renderer.updated();
};
__proto.componentWillUnmount = function () {
this._grid.destroy();
};
__proto._getItemInfos = function () {
var props = this.props;
var children = React.Children.toArray(props.children);
var attributePrefix = props.attributePrefix || VanillaInfiniteGrid.defaultOptions.attributePrefix;
var itemBy = props.itemBy || function (item) {
return item.key;
};
var groupBy = props.groupBy || function (item) {
return item.props["".concat(attributePrefix, "groupkey")];
};
var infoBy = props.infoBy || function () {
return {};
};
return children.map(function (child, i) {
var _a = infoBy(child, i) || {},
data = _a.data,
rest = __rest(_a, ["data"]);
return __assign(__assign({
groupKey: groupBy(child, i),
key: itemBy(child, i)
}, rest), {
data: __assign(__assign({}, data), {
jsx: child
})
});
});
};
__proto._renderContainer = function () {
var props = this.props;
var visibleChildren = this._getVisibleChildren();
var container = props.container;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var ContainerTag = props.containerTag || "div";
if (container === true) {
return React.createElement(ContainerTag, {
ref: this._containerRef
}, visibleChildren);
} else {
return visibleChildren;
}
};
__proto._getVisibleChildren = function () {
var props = this.props;
var placeholder = props.placeholder;
var loading = props.loading;
var visibleItems = VanillaInfiniteGrid.getRenderingItems(this._getItemInfos(), {
grid: this._grid,
status: props.status,
horizontal: props.horizontal,
useFirstRender: props.useFirstRender,
useLoading: props.loading,
usePlaceholder: props.placeholder
});
return visibleItems.map(function (item) {
if (item.type === VanillaInfiniteGrid.ITEM_TYPE.VIRTUAL) {
return React.cloneElement(isFunction(placeholder) ? placeholder(item) : placeholder, {
key: item.key
});
} else if (item.type === VanillaInfiniteGrid.ITEM_TYPE.LOADING) {
return React.cloneElement(isFunction(loading) ? loading(item) : loading, {
key: item.key
});
} else {
return item.data.jsx;
}
});
};
__decorate([VanillaInfiniteGrid.withInfiniteGridMethods], InfiniteGrid.prototype, "_grid", void 0);
return InfiniteGrid;
}(React.Component);
var MasonryInfiniteGrid = /*#__PURE__*/function (_super) {
__extends(MasonryInfiniteGrid, _super);
function MasonryInfiniteGrid() {
return _super !== null && _super.apply(this, arguments) || this;
}
MasonryInfiniteGrid.GridClass = VanillaInfiniteGrid.MasonryInfiniteGrid;
return MasonryInfiniteGrid;
}(InfiniteGrid);
var JustifiedInfiniteGrid = /*#__PURE__*/function (_super) {
__extends(JustifiedInfiniteGrid, _super);
function JustifiedInfiniteGrid() {
return _super !== null && _super.apply(this, arguments) || this;
}
JustifiedInfiniteGrid.GridClass = VanillaInfiniteGrid.JustifiedInfiniteGrid;
return JustifiedInfiniteGrid;
}(InfiniteGrid);
var FrameInfiniteGrid = /*#__PURE__*/function (_super) {
__extends(FrameInfiniteGrid, _super);
function FrameInfiniteGrid() {
return _super !== null && _super.apply(this, arguments) || this;
}
FrameInfiniteGrid.GridClass = VanillaInfiniteGrid.FrameInfiniteGrid;
return FrameInfiniteGrid;
}(InfiniteGrid);
var PackingInfiniteGrid = /*#__PURE__*/function (_super) {
__extends(PackingInfiniteGrid, _super);
function PackingInfiniteGrid() {
return _super !== null && _super.apply(this, arguments) || this;
}
PackingInfiniteGrid.GridClass = VanillaInfiniteGrid.PackingInfiniteGrid;
return PackingInfiniteGrid;
}(InfiniteGrid);
var modules = {
__proto__: null,
InfiniteGrid: InfiniteGrid,
MasonryInfiniteGrid: MasonryInfiniteGrid,
JustifiedInfiniteGrid: JustifiedInfiniteGrid,
FrameInfiniteGrid: FrameInfiniteGrid,
PackingInfiniteGrid: PackingInfiniteGrid
};
return modules;
})));
//# sourceMappingURL=infinitegrid.umd.js.map