UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

64 lines (63 loc) 2.42 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; define(["require", "exports", 'react', '../eventGroup/EventGroup'], function (require, exports, React, EventGroup_1) { "use strict"; var Router = (function (_super) { __extends(Router, _super); function Router() { _super.call(this); this.state = { path: location.hash }; this._events = new EventGroup_1.EventGroup(this); } Router.prototype.componentDidUpdate = function (prevProps, prevState) { if (this.state.path !== prevState.path) { window.scrollTo(0, 0); } }; Router.prototype.render = function () { return _getComponent(this.state.path, this.props.children); }; Router.prototype.componentDidMount = function () { var _this = this; this._events.on(window, 'hashchange', function () { if (_this.state.path !== location.hash) { _this.setState({ path: location.hash }); } }); }; Router.prototype.componentWillUnmount = function () { this._events.dispose(); }; return Router; }(React.Component)); exports.Router = Router; function _getComponent(matchPath, children) { if (children && children.$$typeof) { children = [children]; } for (var i = 0; children && i < children.length; i++) { var currentChild = children[i]; if (_match(matchPath, currentChild)) { var component = currentChild.props.component; var childComponent = _getComponent(matchPath, currentChild.props.children); return React.createElement(component, null, childComponent); } } return null; } function _match(currentPath, child) { if (child.props) { var path = child.props.path; path = path || ''; currentPath = currentPath || ''; return ((!path) || (path.toLowerCase() === currentPath.toLowerCase())); } return false; } });