@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
197 lines (192 loc) • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.Linkable = exports.LinkBase = exports.Link = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _handle = require("@enact/core/handle");
var _hoc = _interopRequireDefault(require("@enact/core/hoc"));
var _useHandlers = _interopRequireDefault(require("@enact/core/useHandlers"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _useLink = _interopRequireDefault(require("./useLink"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["path"];
/**
* A base component that is used to make a link.
*
* @class LinkBase
* @ui
* @memberof ui/Routable
* @public
*/
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var LinkBase = exports.LinkBase = (0, _kind["default"])({
name: 'Link',
propTypes: /** @lends ui/Routable.Link.prototype */{
/**
* The `path` to navigate that matches the path of the {@link ui/Routable.Routable} container.
*
* @type {String}
* @required
* @public
*/
path: _propTypes["default"].string.isRequired,
/**
* Applies a disabled style and the control becomes non-interactive.
*
* @type {Boolean}
* @public
*/
disabled: _propTypes["default"].bool
},
defaultProps: {
disabled: false
},
handlers: {
onClick: (0, _handle.handle)((0, _handle.forProp)('disabled', false), (0, _handle.forward)('onClick'), _handle.preventDefault)
},
render: function render(_ref) {
var path = _ref.path,
rest = _objectWithoutProperties(_ref, _excluded);
/* eslint-disable jsx-a11y/anchor-has-content */
return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", _objectSpread({
href: path
}, rest));
/* eslint-enable jsx-a11y/anchor-has-content */
}
});
/**
* A higher-order component adds support to a component to handle `navigate` from {@link ui/Routable.Routable|Routable}.
* It has configuration placed in the first argument to define which event will be used to navigate.
* `onClick` event is used by default. Thus, if you don't configure it, the component should forward `onClick` event
* to make {@link ui/Routable.Routable|Routable} know when navigation is triggered.
*
* Example:
* ```
* const CustomItemBase = kind({
* name: 'CustomItem',
*
* handlers: {
* onClick: handle(
* forward('onClick')
* )
* },
*
* render: ({...rest}) => {
* return (
* <div {...rest} />
* );
* }
* });
*
* const CustomItem = Linkable({navigate: 'onClick'}, CustomItemBase);
* // same as const CustomItem = Linkable(CustomItemBase);
*
* const Main = () => (
* <div>
* <CustomItem path="./about">About</CustomItem>
* <CustomItem path="./faq">FAQ</CustomItem>
* </div>
* );
* ```
*
* @class Linkable
* @memberof ui/Routable
* @hoc
* @public
*/
var Linkable = exports.Linkable = (0, _hoc["default"])({
navigate: 'onClick'
}, function (config, Wrapped) {
var navigate = config.navigate;
var navHandlers = _defineProperty({}, navigate, (0, _handle.handle)((0, _handle.forward)(navigate), function (ev, props, hook) {
hook.navigate(props);
}));
// eslint-disable-next-line no-shadow
function Linkable(props) {
var link = (0, _useLink["default"])();
var handlers = (0, _useHandlers["default"])(navHandlers, props, link);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, props), handlers));
}
// TODO: Added to maintain `ref` compatibility with 3.x. Remove in 4.0
return /*#__PURE__*/function (_Component) {
_inherits(LinkableAdapter, _Component);
var _super = _createSuper(LinkableAdapter);
function LinkableAdapter() {
_classCallCheck(this, LinkableAdapter);
return _super.apply(this, arguments);
}
_createClass(LinkableAdapter, [{
key: "render",
value: function render() {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Linkable, _objectSpread({}, this.props));
}
}]);
return LinkableAdapter;
}(_react.Component);
});
/**
* A component that is used to make a link to navigate among {@link ui/Routable.Route|Route} components.
*
* In the following example, `Sample` would render `Main` with two Links for `About` and `FAQ`.
* If `About` is clicked, the content of `About` would be displayed under `Main`.
*
* Example:
* ```
* const Views = Routable({navigate: 'onNavigate'}, ({children}) => <div>{children}</div>);
*
* const Main = () => (
* <div>
* <Link path="./about">About</Link>
* <Link path="./faq">FAQ</Link>
* </div>
* );
*
* const About = () => (<div>Greetings! We are Enact team.</div>);
*
* const Faq = () => (<div>List of FAQ</div>);
*
* const Sample = (props) => {
* // use 'main' for the default path
* const [path, nav] = useState('main');
* // if onNavigate is called with a new path, update the state
* const handleNavigate = useCallback((ev) => nav(ev.path), [nav]);
*
* return (
* <Views {...props} path={path} onNavigate={handleNavigate}>
* <Route path="main" component={Main}>
* <Route path="about" component={About} />
* <Route path="faq" component={Faq} />
* </Route>
* </Views>
* );
* };
* ```
*
* @class Link
* @ui
* @extends ui/Routable.LinkBase
* @mixes ui/Routable.Linkable
* @memberof ui/Routable
* @public
*/
var Link = exports.Link = Linkable(LinkBase);
var _default = exports["default"] = Link;