UNPKG

react-state-hoc

Version:

A React higher-order component for abstracting state away

95 lines (83 loc) 4.04 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var 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]; }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __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; }; function bindMapSetStateToProps(mapSetStateToProps, setState, initialProps) { if (typeof mapSetStateToProps === 'function') { return mapSetStateToProps(initialProps)(setState); } if (typeof mapSetStateToProps === 'object') { return Object.keys(mapSetStateToProps).reduce(function (mappedProps, propName) { var stateFactory = mapSetStateToProps[propName]; mappedProps[propName] = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return setState(stateFactory.apply(void 0, args)); }; return mappedProps; }, {}); } return {}; } var defaultMapSetStateToProps = function () { return function (setState) { return ({ setState: setState }); }; }; var defaultMergeProps = function (props, state, stateCreators) { return (__assign({}, props, state, stateCreators)); }; function withState(initialState, mapSetStateToProps, mergeProps) { return function (BaseComponent) { return _a = /** @class */ (function (_super) { __extends(StateHoc, _super); function StateHoc(props) { var _this = _super.call(this, props) || this; _this.state = typeof initialState === 'function' ? initialState(props) : initialState; _this.setState = _this.setState.bind(_this); _this.stateCreators = bindMapSetStateToProps(mapSetStateToProps || defaultMapSetStateToProps, _this.setState, _this.props); _this.merge = mergeProps || defaultMergeProps; return _this; } StateHoc.prototype.render = function () { var _a = this, state = _a.state, props = _a.props, stateCreators = _a.stateCreators, merge = _a.merge; return React.createElement(BaseComponent, merge(props, state, stateCreators)); }; return StateHoc; }(React.Component)), _a.displayName = 'WithState', _a; var _a; }; } exports.withState = withState; exports.default = withState;