@talend/react-containers
Version:
Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.
72 lines (69 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _reactCmf = require("@talend/react-cmf");
var _lodash = require("lodash");
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
* ShortcutManager matches shortcuts to the current route to redirect to a new
* one
*
* @extends {React}
*/
class ShortcutManager extends _react.Component {
constructor(props) {
super(props);
this.redirect = this.redirect.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleRegexMatching = this.handleRegexMatching.bind(this);
}
componentDidMount() {
document.addEventListener('keydown', this.handleKeyPress);
}
// eslint-disable-next-line class-methods-use-this
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyPress);
}
redirect(event, path) {
this.props.dispatchActionCreator('redirect', event, {
action: {
path
}
});
}
handleRegexMatching(event, redirectMap, pathname) {
const match = (0, _lodash.keys)(redirectMap).find(key => RegExp(key, 'm').test(pathname));
if (match) {
if (redirectMap[match].startsWith('/')) {
this.redirect(event, redirectMap[match]);
} else {
this.props.dispatchActionCreator(redirectMap[match], event);
}
}
}
handleKeyPress(event) {
// toLowerCase allows to handle retro-compatibility
const redirectMap = this.props.redirectMap[event.key] || this.props.redirectMap[event.key.toLowerCase()];
if (redirectMap) {
if (redirectMap[this.props.pathname]) {
this.redirect(event, redirectMap[this.props.pathname]);
} else {
this.handleRegexMatching(event, redirectMap, this.props.pathname);
}
}
}
// eslint-disable-next-line class-methods-use-this
render() {
return null;
}
}
_defineProperty(ShortcutManager, "displayName", 'Container(ShortcutManager)');
_defineProperty(ShortcutManager, "propTypes", {
..._reactCmf.cmfConnect.propTypes
});
var _default = exports.default = ShortcutManager;
//# sourceMappingURL=ShortcutManager.container.js.map