UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

88 lines (84 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.Slottable = void 0; Object.defineProperty(exports, "useSlots", { enumerable: true, get: function get() { return _useSlots["default"]; } }); var _hoc = _interopRequireDefault(require("@enact/core/hoc")); var _useSlots = _interopRequireDefault(require("./useSlots")); var _jsxRuntime = require("react/jsx-runtime"); 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 _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); } /** * Provides a higher-order component that render child components into pre-designated slots. * * @module ui/Slottable * @exports Slottable */ /** * Default config for `Slottable`. * * @memberof ui/Slottable.Slottable * @hocconfig * @public */ var defaultConfig = { /** * Array of slot names which will be extracted from `children` and distributed to props. * * @type {String[]} * @memberof ui/Slottable.Slottable.defaultConfig */ slots: null }; /** * A higher-order component that allows wrapped components to separate children into pre-designated 'slots'. * * To use `Slottable`, you must configure it by passing in a config object with the `slots` member set to an * array of slot names. Any children whose `slot` or `defaultSlot` property matches a named slot or whose * type matches a named slot will be placed into a property of the same name on the wrapped component. * * @class Slottable * @memberof ui/Slottable * @hoc * @public */ var Slottable = exports.Slottable = (0, _hoc["default"])(defaultConfig, function (config, Wrapped) { var slots = config.slots; // eslint-disable-next-line no-shadow return function Slottable(props) { // extract the slots into a new object but populating the default value to be undefined so // the key exists in order to allow the current "harmful" behavior below. Must be undefined // in order to trigger defaultProps on downstream components. var slotProps = { children: props.children }; slots.forEach(function (k) { return slotProps[k] = undefined; }); // eslint-disable-line no-undefined // Slottable allows there to be other values in the destination slot and merges them. // However, consumers can't avoid key warnings when merging the two lists so we should // "consider this harmful" and not continue to support this with the hook and instead // encourage using the slot as the default with the prop as a fallback as implemented by the // hook. var distributed = (0, _useSlots["default"])(slotProps); slots.forEach(function (slot) { var dist = distributed[slot]; var prop = props[slot]; if (prop != null && dist != null) { distributed[slot] = [].concat(prop, dist); } else if (prop != null) { distributed[slot] = prop; } }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, props), distributed)); }; }); var _default = exports["default"] = Slottable;