UNPKG

@up-group-ui/react-controls

Version:
51 lines 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Toggleable = void 0; var tslib_1 = require("tslib"); var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = (0, tslib_1.__importDefault)(require("react")); var utils_1 = require("utils"); var initialState = { show: false }; var Toggleable = (function (_super) { (0, tslib_1.__extends)(Toggleable, _super); function Toggleable() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = initialState; _this.reset = function () { return _this.internalSetState(initialState, function () { }); }; _this.getApi = function () { return { show: _this.state.show, toggle: _this.toggle }; }; _this.internalSetState = function (changes, callback) { _this.setState(function (state) { var changesObject = typeof changes === 'function' ? changes(state) : changes; var reducedChanges = _this.props.stateReducer ? _this.props.stateReducer(state, changesObject) : changesObject; return reducedChanges; }, callback); }; _this.toggle = function (event) { return _this.internalSetState(updateShowState, function () { return (_this.props.onToggle ? _this.props.onToggle(_this.state.show) : null); }); }; return _this; } Toggleable.prototype.render = function () { var _a = this.props, InjectedComponent = _a.component, children = _a.children, render = _a.render, props = _a.props; var renderProps = this.getApi(); if (InjectedComponent) { return ((0, jsx_runtime_1.jsx)(InjectedComponent, (0, tslib_1.__assign)({}, props, renderProps, { children: children }), void 0)); } if (render) { return render(renderProps); } if (children != null && (0, utils_1.isFunction)(children)) { var childrenAsFunction = children; return childrenAsFunction(renderProps); } return null; }; return Toggleable; }(react_1.default.Component)); exports.Toggleable = Toggleable; var updateShowState = function (prevState) { return ({ show: !prevState.show, }); }; //# sourceMappingURL=Toggleable.js.map