UNPKG

@prodo/route

Version:

Route plugin for [Prodo](https://prodo.dev). See [documentation](https://docs.prodo.dev/plugins/routing) for more info.

123 lines 4.63 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@prodo/core"); var React = require("react"); var plugin_1 = require("./plugin"); var utils_1 = require("./utils"); exports.Route = core_1.connect(function (_a) { var route = _a.route, watch = _a.watch; return function (_a) { var path = _a.path, exact = _a.exact, children = _a.children, component = _a.component; var match = path == null ? {} : utils_1.matchRoute(watch(route.path), path, exact); if (match != null) { if (children && React.Children.count(children) > 0) { return React.createElement(React.Fragment, null, children); } else if (component != null) { return React.createElement(component, match); } } return null; }; }, "Route"); exports.Switch = core_1.connect(function (_a) { var route = _a.route, watch = _a.watch; return function (_a) { var children = _a.children; var element = null; React.Children.forEach(children, function (child) { if (element == null && React.isValidElement(child)) { var path = child.props.path; if (path != null) { if (utils_1.matchRoute(watch(route.path), path, child.props.exact)) { element = child; } } else { element = child; } } }); return element && React.cloneElement(element, {}); }; }, "Switch"); exports.Redirect = core_1.connect(function (_a) { var dispatch = _a.dispatch; return function (_a) { var _b = _a.push, push = _b === void 0 ? false : _b, to = _a.to; if (typeof to === "string") { to = { path: to }; } if (to.params == null) { to.params = {}; } var action = push ? push : plugin_1.replace; React.useEffect(function () { dispatch(action)(to); }, [to.path, to.params]); return null; }; }, "Redirect"); var Anchor = function (_a) { var onClick = _a.onClick, navigate = _a.navigate, rest = __rest(_a, ["onClick", "navigate"]); return (React.createElement("a", __assign({}, rest, { onClick: function (e) { try { if (onClick) { onClick(e); } } catch (ex) { e.preventDefault(); throw ex; } if (!e.defaultPrevented && e.button === 0 && (!rest.target || rest.target === "_self") && !e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey) { e.preventDefault(); navigate(); } } }))); }; exports.Link = core_1.connect(function (_a) { var dispatch = _a.dispatch; return function (_a) { var _b = _a.component, component = _b === void 0 ? Anchor : _b, _c = _a.replace, replace = _c === void 0 ? false : _c, to = _a.to, rest = __rest(_a, ["component", "replace", "to"]); if (typeof to === "string") { to = { path: to, params: {} }; } if (to.params == null) { to.params = {}; } return React.createElement(component, __assign(__assign({}, rest), { href: "" + to.path + (Object.keys(to.params).length > 0 ? utils_1.createParamString(to.params) : ""), navigate: function () { var action = replace ? replace : plugin_1.push; dispatch(action)(to); } })); }; }, "Link"); //# sourceMappingURL=react.js.map