ndla-ui
Version:
UI component library for NDLA.
105 lines (87 loc) • 3.88 kB
JavaScript
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; }; }();
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 React from 'react';
import PropTypes from 'prop-types';
var EmbeddedTwitter = function (_React$Component) {
_inherits(EmbeddedTwitter, _React$Component);
_createClass(EmbeddedTwitter, null, [{
key: 'removeChildren',
value: function removeChildren(node) {
if (node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
}
}]);
function EmbeddedTwitter(props) {
_classCallCheck(this, EmbeddedTwitter);
var _this = _possibleConstructorReturn(this, (EmbeddedTwitter.__proto__ || Object.getPrototypeOf(EmbeddedTwitter)).call(this, props));
_this.loadWidget = _this.loadWidget.bind(_this);
return _this;
}
_createClass(EmbeddedTwitter, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (window.twttr) {
this.loadWidget();
}
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps) {
return this.props.screenName !== nextProps.screenName;
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
if (window.twttr) {
this.loadWidget();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
EmbeddedTwitter.removeChildren(this.widgetWrapper);
}
}, {
key: 'loadWidget',
value: function loadWidget() {
var _this2 = this;
window.twttr.ready(function (tw) {
// Delete existing
EmbeddedTwitter.removeChildren(_this2.widgetWrapper);
var _props = _this2.props,
screenName = _props.screenName,
tweetLimit = _props.tweetLimit;
// Create widget
tw.widgets.createTimeline({ sourceType: 'profile', screenName: screenName }, _this2.widgetWrapper, {
tweetLimit: tweetLimit,
chrome: 'noheader nofooter noborders',
borderColor: '#e8e3e3'
});
});
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
return React.createElement('div', {
ref: function ref(c) {
_this3.widgetWrapper = c;
}
});
}
}]);
return EmbeddedTwitter;
}(React.Component);
EmbeddedTwitter.propTypes = {
screenName: PropTypes.string.isRequired,
tweetLimit: PropTypes.number
};
EmbeddedTwitter.defaultProps = {
tweetLimit: 10
};
export default EmbeddedTwitter;