UNPKG

react-stateful-switch

Version:
69 lines 3.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importDefault(require("react")); const react_2 = require("react"); const lodash_1 = __importDefault(require("lodash")); class Switch extends react_2.PureComponent { constructor() { super(...arguments); this.getSelectedIndexes = () => { if (!lodash_1.default.isArray(this.props.selected)) { return [this.props.selected]; } return this.props.selected; }; this.getViews = () => { //Get views from props let views = this.props.views || this.props.children || []; if (!lodash_1.default.isArray(views)) views = [views]; return views; }; this.displayHistory = [this.props.selected]; } componentDidMount() { // console.log(`Adding ${this.props.selected} to history`); // this.displayHistory.push(this.props.selected) } // getSnapshotBeforeUpdate(prevProps: SwitchProps, prevState) { // console.log(`Adding ${prevProps.selected} to history`); // this.displayHistory.push(prevProps.selected) // } render() { let views = this.getViews(); let selectedIndexes = this.getSelectedIndexes(); let cachedIndexes = this.props.maxHidden != null ? lodash_1.default.slice(lodash_1.default.reverse(lodash_1.default.uniq(lodash_1.default.reverse(lodash_1.default.flatten(this.displayHistory)))), -1 * this.props.maxHidden) : lodash_1.default.range(views.length); if (this.props.maxHidden === 0) cachedIndexes = []; // const renderedIndexes = _.uniq(_.flatten(cachedIndexes.concat(selectedIndexes))) // console.log(`$(Sel: ${selectedIndexes} / Cached: ${cachedIndexes}), Hist: ${this.displayHistory}`); let renderedViews = lodash_1.default.range(views.length).map((index) => { const view = views[index]; let selectedIndexes = this.getSelectedIndexes(); const selected = lodash_1.default.some(selectedIndexes, (selectedIndex) => index === selectedIndex); if (!selected && !cachedIndexes.includes(index)) { return null; } const display = selected ? 'contents' : 'none'; const View = view; const specificProps = this.props.props; return react_1.default.createElement("div", { key: index, style: { display } }, view); }).filter(rv => rv != null); if (renderedViews === []) return null; if (this.props.selected != null && this.props.selected != lodash_1.default.last(this.displayHistory)) this.displayHistory.push(this.props.selected); return react_1.default.createElement(react_1.default.Fragment, null, renderedViews); } } Switch.defaultProps = { selected: null, maxHidden: null }; exports.default = Switch; //# sourceMappingURL=Switch.js.map