@navinc/base-react-components
Version:
Nav's Pattern Library
50 lines • 2.54 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { jsx as _jsx } from "react/jsx-runtime";
import propTypes from 'prop-types';
import { Route } from 'react-router-dom';
import LoadingView from './loading-view.js';
export const GuardedRoute = (_a) => {
var {
// A boolean condition that must be met for the route to render.
condition,
// Boolean that determines if the condition logic is still loading. Used when condition must fetch async data first.
isConditionLoading,
// Component to render when condition is still loading (optional).
LoadingComponent = LoadingView,
// Component to render when condition fails. This is required, since it will break React Router without one.
FailureComponent,
// The route component to render on condition success.
component: Component } = _a,
// The remaining props that we'll pass to React Router's Route component
routeProps = __rest(_a, ["condition", "isConditionLoading", "LoadingComponent", "FailureComponent", "component"]);
if (!FailureComponent)
throw new Error('You must provide a FailureComponent to your guarded route.');
return (_jsx(Route, Object.assign({}, routeProps, { render: (props) => {
if (LoadingComponent && isConditionLoading)
return _jsx(LoadingComponent, Object.assign({}, props));
return !isConditionLoading && condition === true ? _jsx(Component, Object.assign({}, props)) : _jsx(FailureComponent, Object.assign({}, props));
} })));
};
GuardedRoute.propTypes = {
condition: propTypes.bool,
isConditionLoading: propTypes.bool,
LoadingComponent: propTypes.elementType,
FailureComponent: propTypes.elementType.isRequired,
component: propTypes.elementType,
};
/**
* Create a GuardedRoute component with defaults.
* @param {*} defaults Supports `condition`, `isConditionLoading`, `LoadingComponent`, and `FailureComponent`.
*/
export const createGuardedRoute = (defaults) => (props) => _jsx(GuardedRoute, Object.assign({}, defaults, props));
//# sourceMappingURL=guarded-route.js.map