UNPKG

storybook-addon-react-renders

Version:

Storybook addon for renders analysis of React components.

136 lines (107 loc) 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WithRendersEvents = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _constants = require("../constants"); var _utils = require("../utils"); var _defaultOptions = require("../defaultOptions"); var _defaultOptions2 = _interopRequireDefault(_defaultOptions); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var WithRendersEvents = exports.WithRendersEvents = function (_React$Component) { _inherits(WithRendersEvents, _React$Component); function WithRendersEvents(props) { _classCallCheck(this, WithRendersEvents); var _this = _possibleConstructorReturn(this, (WithRendersEvents.__proto__ || Object.getPrototypeOf(WithRendersEvents)).call(this, props)); _this.state = { events: [], options: _defaultOptions2.default }; _this.onAddEvents = _this.onAddEvents.bind(_this); _this.changeOptions = _this.changeOptions.bind(_this); _this.notifyUnreadEvents = _this.notifyUnreadEvents.bind(_this); return _this; } _createClass(WithRendersEvents, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var _props = this.props, channel = _props.channel, api = _props.api; // Listen to the notes and render it. channel.on(_constants.EVENTS.EVENTS, this.onAddEvents); channel.on(_constants.EVENTS.INITIALIZATION, this.changeOptions); // Clear the current notes on every story change. this.stopListeningOnStory = api.onStory(function () { _this2.setState({ events: [], options: _defaultOptions2.default }); }); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _props2 = this.props, active = _props2.active, channel = _props2.channel; if (prevProps.active !== active && active) { channel.emit(_constants.EVENTS.OPEN_PANEL); } } }, { key: "changeOptions", value: function changeOptions(options) { this.setState({ options: _extends({}, _defaultOptions2.default, options) }); } }, { key: "onAddEvents", value: function onAddEvents(events) { var _this3 = this; var filteredEvents = (0, _utils.filterEventsWithOptions)({ events: events, options: this.state.options }); this.setState(function (previousState) { return { events: previousState.events.concat(filteredEvents) }; }, function () { return _this3.notifyUnreadEvents(filteredEvents.length); }); } }, { key: "notifyUnreadEvents", value: function notifyUnreadEvents(amountUnreadEvents) { var _props3 = this.props, channel = _props3.channel, active = _props3.active; if (!active) { channel.emit(_constants.EVENTS.UNREAD_EVENTS, amountUnreadEvents); } } // This is some cleanup tasks when the panel is unmounting. }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.stopListeningOnStory) { this.stopListeningOnStory(); } this.unmounted = true; var channel = this.props.channel; channel.removeListener(_constants.EVENTS.EVENTS, this.onAddEvents); channel.removeListener(_constants.EVENTS.INITIALIZATION, this.changeOptions); } }, { key: "render", value: function render() { return this.props.active ? this.props.render(this.state) : null; } }]); return WithRendersEvents; }(_react2.default.Component);