@bigfishtv/cockpit
Version:
87 lines (68 loc) • 3.67 kB
JavaScript
var _class, _temp2;
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import * as scrollUtils from '../../utils/scrollUtils';
/**
* Wrapper for a section that determines whether or not it should update based on if it's visible
* This makes rendering less expensive on huge complex pages with many sections
*/
var Section = (_temp2 = _class = function (_Component) {
_inherits(Section, _Component);
function Section() {
var _temp, _this, _ret;
_classCallCheck(this, Section);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.el = null, _this.state = { inView: true }, _this.handleScroll = function (scroll, windowHeight) {
var position = _this.el.getBoundingClientRect();
var inView = position.bottom > 0 && position.top < windowHeight;
if (inView !== _this.state.inView) {
_this.setState({ inView: inView });
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
Section.prototype.componentDidMount = function componentDidMount() {
scrollUtils.addListener(this.handleScroll);
};
Section.prototype.componentWillUnmount = function componentWillUnmount() {
scrollUtils.removeListener(this.handleScroll);
};
Section.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
// if (!this.state.inView && nextState.inView) console.log(this.props.sectionProperty + ' now in view')
// if (this.state.inView && !nextState.inView) console.log(this.props.sectionProperty + ' out of view')
return nextState.inView;
};
Section.prototype.render = function render() {
var _this2 = this;
var _props = this.props,
Component = _props.Component,
props = _objectWithoutProperties(_props, ['Component']);
return React.createElement(
'div',
{ ref: function ref(el) {
return _this2.el = el;
} },
React.createElement(Component, props)
);
};
return Section;
}(Component), _class.propTypes = {
/** Section component */
Component: PropTypes.func.isRequired,
/** Component icon */
icon: PropTypes.string,
/** Section property, e.g. hero_sections */
sectionProperty: PropTypes.string,
collapsed: PropTypes.bool,
isNew: PropTypes.bool,
onToggleCollapsed: PropTypes.func,
onDuplicate: PropTypes.func,
onRemove: PropTypes.func,
onReorder: PropTypes.func
}, _temp2);
export { Section as default };