@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
209 lines (206 loc) • 10.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.RouterBase = exports.Router = void 0;
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _warning = _interopRequireDefault(require("warning"));
var _ForwardRef = _interopRequireDefault(require("../ForwardRef"));
var _util = require("./util");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["path", "children", "component"],
_excluded2 = ["component", "componentRef"];
var _Class;
/**
* A Router component for use with {@link ui/ViewManager.ViewManager|ViewManager}
*
* @class Router
* @memberof ui/Routable
* @mixes ui/ForwardRef.ForwardRef
* @ui
* @private
*/
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var RouterBase = exports.RouterBase = (_Class = /*#__PURE__*/function (_ReactComponent) {
function RouterBase(props) {
_classCallCheck(this, RouterBase);
return _callSuper(this, RouterBase, [props]);
}
/**
* Generates a set of routes from `children` and appends them to `routes`.
*
* @param {React.element[]} children Array of children
* @param {Object} routes Route configuration object
*
* @returns {Object} Route configuration object
*/
_inherits(RouterBase, _ReactComponent);
return _createClass(RouterBase, [{
key: "createRoutes",
value: function createRoutes(children, routes) {
var _this = this;
_react.Children.forEach(children, function (child) {
var _child$props = child.props,
path = _child$props.path,
grandchildren = _child$props.children,
component = _child$props.component,
rest = _objectWithoutProperties(_child$props, _excluded);
if (path && component) {
routes[path] = {
$component: component,
$props: rest
};
if (grandchildren) {
_this.createRoutes(grandchildren, routes[path]);
}
}
});
return routes;
}
/**
* Creates an array of React.elements for the current path.
*
* @returns {React.element[]} Children to render
*/
}, {
key: "createChildren",
value: function createChildren() {
var segments = (0, _util.toSegments)(this.props.path);
var _this$props = this.props,
routes = _this$props.routes,
children = _this$props.children;
var computedRoutes = routes || this.createRoutes(children, {});
var valid = true;
var route = computedRoutes;
var childrenElements = segments.map(function (segment, index) {
var subPath = segments.slice(0, index + 1).join('/');
route = route && route[segment];
if (route && route.$component) {
return /*#__PURE__*/(0, _react.createElement)(route.$component, _objectSpread(_objectSpread({}, route.$props), {}, {
key: 'view$/' + subPath,
spotlightId: "panel-".concat(subPath.replace(/\//g, '-'))
}));
}
valid = false;
return null;
});
process.env.NODE_ENV !== "production" ? (0, _warning["default"])(valid, "".concat(this.props.path, " does not match the configured routes: ").concat((0, _util.stringifyRoutes)(computedRoutes))) : void 0;
return valid ? childrenElements : [];
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
Component = _this$props2.component,
componentRef = _this$props2.componentRef,
rest = _objectWithoutProperties(_this$props2, _excluded2);
var children = this.createChildren();
delete rest.path;
delete rest.routes;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _objectSpread(_objectSpread({
ref: componentRef
}, rest), {}, {
children: children
}));
}
}]);
}(_react.Component), _Class.displayName = 'Router', _Class.propTypes = /** @lends ui/Routable.Router.prototype */{
/**
* List of views to render.
*
* Will be rendered as a flat array of views suitable for use in
* Panels and not a hierarchy of views as the path implies.
*
* May either be a URI-style path (`'/app/home/settings'`) or an array
* of strings (`['app', 'home', 'settings']`).
*
* @type {String|String[]}
* @required
* @public
*/
path: _util.RoutablePropTypes.path.isRequired,
/**
* The component wrapping the rendered path.
*
* @type {String|Component}
* @default 'div'
* @public
*/
component: _propTypes["default"].renderable,
/**
* Called with a reference to {@link ui/Routable.RouterBase.component|component}.
*
* @type {Object|Function}
* @private
*/
componentRef: _propTypes["default"].ref,
/**
* Routes defined as an object rather than via JSX.
*
* If specified, `routes` will take
* precedence over a JSX definition.
*
* ```JavaScript
* const routes = {
* 'first': {
* '$props': {
* 'title': 'About Routable Panels Pattern'
* },
* '$component': AboutPanel,
* 'second': {
* '$props': {
* 'next': 'fourth',
* 'title': 'Second'
* },
* '$component': MainPanel
* },
* 'third': {
* '$props': {
* 'next': 'first',
* 'title': 'Third'
* },
* '$component': MainPanel,
* 'fourth': {
* '$props': {
* 'next': 'third',
* 'title': 'Fourth'
* },
* '$component': MainPanel
* }
* }
* }
* };
*
* <Panels path="/app/home/settings" routes={routes} />
* ```
*
* @type {Object}
* @public
*/
routes: _propTypes2["default"].object
}, _Class.defaultProps = {
component: 'div'
}, _Class);
var Router = exports.Router = (0, _ForwardRef["default"])({
prop: 'componentRef'
}, RouterBase);
var _default = exports["default"] = Router;
;