@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
79 lines (60 loc) • 3.4 kB
JavaScript
;
exports.__esModule = true;
exports.default = 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 _class, _temp;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
/**
* Display a backup image if there is some error with the desired image source.
* If neither provided source is successfully displayed, then display nothing.
* You can pass in any props that work for a standard <img> tag and they'll be added to the inserted image.
*/
var DefaultImage = (_temp = _class = function (_React$Component) {
_inherits(DefaultImage, _React$Component);
function DefaultImage(props) {
_classCallCheck(this, DefaultImage);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.state = {
src: props.src
};
_this.onError = _this.onError.bind(_this);
return _this;
}
DefaultImage.prototype.componentWillReceiveProps = function componentWillReceiveProps(newProps) {
if (newProps.src !== this.props.src) {
this.setState({
src: newProps.src
});
}
};
DefaultImage.prototype.onError = function onError() {
var src = null;
if (this.state.src !== this.props.defaultSrc) {
src = this.props.defaultSrc;
}
this.setState({
src: src
});
};
DefaultImage.prototype.render = function render() {
var _props = this.props,
src = _props.src,
defaultSrc = _props.defaultSrc,
props = _objectWithoutProperties(_props, ['src', 'defaultSrc']);
if (this.state.src !== null) {
return _react2.default.createElement('img', _extends({ src: this.state.src, onError: this.onError }, props)); // eslint-disable-line jsx-a11y/alt-text
}
return null;
};
return DefaultImage;
}(_react2.default.Component), _class.defaultProps = {
defaultSrc: null
}, _class.displayName = 'DefaultImage', _temp);
exports.default = DefaultImage;
module.exports = exports['default'];