UNPKG

react-dayo

Version:

A Queue component for notification etc

131 lines 4.99 kB
var __extends = (this && this.__extends) || (function () { 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(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); }; import React from 'react'; import { DayoOperator, DayoSelector, Dispatcher, Event as DispatcherEvent, } from 'core-dayo'; import Queue from '../components/queue'; import Box from '../components/box'; export var defaultOptions = { to: 'top', position: 'center', maxLength: 5, }; /** * To create the `Dayo` component and also`dispatch` function to pass a seed into `Dayo` */ export var createDayo = function (userOptions) { if (userOptions === void 0) { userOptions = {}; } var options = __assign({}, defaultOptions, userOptions); var dispatcher = new Dispatcher(); /** * Interface */ var Dayo = /** @class */ (function (_super) { __extends(Dayo, _super); function Dayo() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.dispatcher = dispatcher; _this.operator = new DayoOperator(_this); _this.selector = new DayoSelector(_this); _this.state = { queue: [], }; /** * To be called when the cycle step is updated * * if the step of current is 'enter' then addind it into the queue, * or else just re-rendering with new state * */ _this.handleUpdateSeed = function (seedOnCycle) { if (seedOnCycle.cycle.isEnter()) { _this.operator.addSeed(seedOnCycle); _this.operator.skipOverflowSeeds({ maxLength: _this.getOption('maxLength'), }); } return _this.operator.rewriteQueueItem(seedOnCycle); }; /** * To be called when the cycle step is ended for removing a ended seed from the queue */ _this.handleDoneSeed = function (seed) { var queue = _this.state.queue; var nextQueue = queue.filter(function (item) { return item.id !== seed.id; }); _this.setQueue(nextQueue); }; return _this; } Dayo.prototype.getQueue = function () { return this.state.queue; }; Dayo.prototype.setQueue = function (queue) { this.setState({ queue: queue }); }; /** * To just return `option` */ Dayo.prototype.getOption = function (key) { return (this.props[key] || options[key]); }; /** * To be called when rendered in the dom */ Dayo.prototype.componentDidMount = function () { dispatcher.on(DispatcherEvent.UpdateSeed, this.handleUpdateSeed); dispatcher.on(DispatcherEvent.DoneSeed, this.handleDoneSeed); }; /** * Advance to the next cycle step at the css animation ended */ Dayo.prototype.onTransitionEnd = function (seedOnCycle) { return function () { if (seedOnCycle.cycle.isEntering()) { seedOnCycle.cycle.proceed(); return; } if (seedOnCycle.cycle.isExiting()) { seedOnCycle.cycle.proceed(); } }; }; /** * To just render component */ Dayo.prototype.render = function () { var _this = this; return (React.createElement(Queue, __assign({}, this.selector.getQueueComponentProps()), this.state.queue.map(function (seedOnCycle) { return (React.createElement(Box, __assign({ key: seedOnCycle.id }, _this.selector.getBoxComponentProps(seedOnCycle)), React.createElement("div", null, seedOnCycle.message))); }))); }; Dayo.defaultProps = options; return Dayo; }(React.Component)); return [Dayo, dispatcher.dispatch]; }; //# sourceMappingURL=dayo.js.map