Skip to content

Commit

Permalink
bump version to 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaocongDong committed Sep 5, 2019
1 parent 99ecc97 commit a4c9569
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 172 deletions.
225 changes: 54 additions & 171 deletions es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,6 @@ import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}

function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}

return obj;
}

function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
Expand All @@ -57,53 +20,8 @@ function _extends() {
return _extends.apply(this, arguments);
}

function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}

subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}

function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}

function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};

return _setPrototypeOf(o, p);
}

function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}

return self;
}

function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}

return _assertThisInitialized(self);
}

var useEffect = React.useEffect,
useRef = React.useRef;
/**
* This function generates the HOC function that will call
* the handleRoutedChanged function of the DecoratedComponent instance
Expand All @@ -120,110 +38,75 @@ var onRouteChangedHOC = function onRouteChangedHOC(DecoratedComponent) {
var componentName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component';
var isReactComponent = DecoratedComponent.prototype && DecoratedComponent.prototype.isReactComponent;

var RouteChangedComponent =
/*#__PURE__*/
function (_React$Component) {
_inherits(RouteChangedComponent, _React$Component);

function RouteChangedComponent() {
var _getPrototypeOf2;
var RouteChangedComponent = function RouteChangedComponent(props) {
var location = props.location;
var prevLocationRef = useRef(location);
var instanceRef = useRef(null);

var _this;
var __getHandleRouteChangedFunc = function __getHandleRouteChangedFunc() {
var handleRouteChanged;

_classCallCheck(this, RouteChangedComponent);

for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
if (!isReactComponent && typeof config.handleRouteChanged === 'function') {
handleRouteChanged = config.handleRouteChanged;
}

_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(RouteChangedComponent)).call.apply(_getPrototypeOf2, [this].concat(args)));

_defineProperty(_assertThisInitialized(_this), "__getHandleRouteChangedFunc", function () {
var handleRouteChanged;

if (!isReactComponent && typeof config.handleRouteChanged === 'function') {
handleRouteChanged = config.handleRouteChanged;
}

if (isReactComponent && typeof _this.instanceRef.handleRouteChanged === 'function') {
handleRouteChanged = _this.instanceRef.handleRouteChanged;
}

return handleRouteChanged;
});

_defineProperty(_assertThisInitialized(_this), "__routeChangedHandler", function (prevLocation, nextLocation) {
var isSamePath = prevLocation.pathname === nextLocation.pathname;
var isSameSearch = prevLocation.search === nextLocation.search;
var isSameHash = prevLocation.hash === nextLocation.hash;

var handleRouteChanged = _this.__getHandleRouteChangedFunc();

if (!isSamePath) {
return handleRouteChanged(prevLocation, nextLocation);
}
if (isReactComponent && typeof instanceRef.current.handleRouteChanged === 'function') {
handleRouteChanged = instanceRef.current.handleRouteChanged;
}

if (!config.onlyPathname && (!isSameHash || !isSameSearch)) {
return handleRouteChanged(prevLocation, nextLocation);
}
});
return handleRouteChanged;
};

return _this;
}
var __routeChangedHandler = function __routeChangedHandler(prevLocation, nextLocation) {
var isSamePath = prevLocation.pathname === nextLocation.pathname;
var isSameSearch = prevLocation.search === nextLocation.search;
var isSameHash = prevLocation.hash === nextLocation.hash;

_createClass(RouteChangedComponent, [{
key: "componentDidMount",
value: function componentDidMount() {
var handleRouteChanged = this.__getHandleRouteChangedFunc();
var handleRouteChanged = __getHandleRouteChangedFunc();

if (typeof handleRouteChanged !== 'function') {
throw new Error('WrappedComponent lacks a handleRouteChanged(prevLocation, currLocation) for processing route changed event.');
}

if (config.mounted) {
// Since we have no idea what the previous route is when the component got mounted,
// We will pass an null as the prevLocation param
handleRouteChanged(null, this.props.location);
}
if (!isSamePath) {
return handleRouteChanged(prevLocation, nextLocation);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var prevLocation = this.props.location;
var nextLocation = nextProps.location;

if (prevLocation === nextLocation) {
return;
}

this.__routeChangedHandler(prevLocation, nextLocation);
if (!config.onlyPathname && (!isSameHash || !isSameSearch)) {
return handleRouteChanged(prevLocation, nextLocation);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
};

var props = _extends({}, this.props);
useEffect(function () {
var handleRouteChanged = __getHandleRouteChangedFunc();

if (isReactComponent) {
props.ref = function (ref) {
_this2.instanceRef = ref;
};
}

return React.createElement(DecoratedComponent, props);
if (typeof handleRouteChanged !== 'function') {
throw new Error('WrappedComponent lacks a handleRouteChanged(prevLocation, currLocation) for processing route changed event.');
}
}]);

return RouteChangedComponent;
}(React.Component);

_defineProperty(RouteChangedComponent, "displayName", "OnRouteChanged(".concat(componentName, ")"));
if (config.mounted) {
// Since we have no idea what the previous route is when the component got mounted,
// We will pass an null as the prevLocation param
handleRouteChanged(null, location);
}
}, []);
useEffect(function () {
prevLocationRef.current = location;
});
var preLocation = prevLocationRef.current;
useEffect(function () {
__routeChangedHandler(preLocation, location);
}, [props.location]);

if (isReactComponent) {
return React.createElement(DecoratedComponent, _extends({
ref: instanceRef
}, props));
} else {
return React.createElement(DecoratedComponent, props);
}
};

_defineProperty(RouteChangedComponent, "propTypes", {
RouteChangedComponent.propTypes = {
location: PropTypes.object.isRequired
});

};
RouteChangedComponent.displayName = "OnRouteChanged(".concat(componentName, ")");
return withRouter(RouteChangedComponent);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-onroutechanged",
"version": "1.0.1",
"version": "1.0.2",
"description": "An onRouteChanged wrapper for React components",
"main": "es/index.js",
"types": "types/index",
Expand Down

0 comments on commit a4c9569

Please sign in to comment.