@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
179 lines (176 loc) • 7.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.RepeaterBase = exports.Repeater = void 0;
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _ForwardRef = _interopRequireDefault(require("../ForwardRef"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["key"],
_excluded2 = ["component", "componentRef"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": 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 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); } /**
* A repeater component.
*
* @module ui/Repeater
* @exports Repeater
*/ /**
* A stateless component that stamps out copies of `childComponent`, without
* [RepeaterDecorator](ui/Repeater.RepeaterDecorator) applied.
*
* @class RepeaterBase
* @memberof ui/Repeater
* @ui
* @public
*/
var RepeaterBase = exports.RepeaterBase = (0, _kind["default"])({
name: 'Repeater',
propTypes: /** @lends ui/Repeater.RepeaterBase.prototype */{
/**
* Component type to repeat.
*
* This can be a React component or a string describing a DOM node (e.g. `'div'`).
*
* @type {String|Component}
* @required
* @public
*/
childComponent: _propTypes["default"].renderable.isRequired,
/**
* An array of data to be mapped onto the `childComponent`.
*
* This supports two data types. If an array of strings is provided, the strings will be used
* in the generated `childComponent` as the readable text. If an array of objects is provided,
* each object will be spread onto the generated `childComponent` with no interpretation.
* You'll be responsible for setting properties like `disabled`, `className`, and setting the
* text content using the `children` key.
*
* > **NOTE**: When an array of objects is provided, make sure a unique `key` is assigned to each
* data. See https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key for more information.
*
* @type {String[]|Array.<{key: (Number|String)}>}
* @required
* @public
*/
children: _propTypes2["default"].oneOfType([_propTypes2["default"].arrayOf(_propTypes2["default"].string), _propTypes2["default"].arrayOf(_propTypes2["default"].shape({
key: _propTypes2["default"].oneOfType([_propTypes2["default"].string, _propTypes2["default"].number]).isRequired
}))]).isRequired,
/**
* The property on each `childComponent` that receives the data in `children`.
*
* @type {String}
* @default 'children'
* @public
*/
childProp: _propTypes2["default"].string,
/**
* Component type to wrap around all the repeated elements.
*
* This can be a string describing a DOM node or React component (e.g. `'div'` or `Layout`).
*
* @type {String|Component}
* @default 'span'
* @public
*/
component: _propTypes["default"].renderable,
/**
* Called with a reference to the root component.
*
* When using {@link ui/Repeater.Repeater}, the `ref` prop is forwarded to this component
* as `componentRef`.
*
* @type {Object|Function}
* @public
*/
componentRef: _propTypes["default"].ref,
/**
* The property on each `childComponent` that receives the index of the item in the `Repeater`.
*
* @type {String}
* @default 'data-index'
* @public
*/
indexProp: _propTypes2["default"].string,
/**
* An object containing properties to be passed to each child.
*
* @type {Object}
* @public
*/
itemProps: _propTypes2["default"].object
},
defaultProps: {
childProp: 'children',
component: 'span',
indexProp: 'data-index'
},
computed: {
children: function children(_ref) {
var Component = _ref.childComponent,
_children = _ref.children,
childProp = _ref.childProp,
indexProp = _ref.indexProp,
itemProps = _ref.itemProps;
return _children.map(function (data, index) {
var props = {};
if (typeof data === 'object') {
props = _objectSpread(_objectSpread({}, itemProps), data);
} else if (childProp) {
props = _objectSpread(_objectSpread({
key: index
}, itemProps), {}, _defineProperty({}, childProp, data));
}
if (indexProp) props[indexProp] = index;
var _props = _objectSpread({}, props),
key = _props.key,
rest = _objectWithoutProperties(_props, _excluded);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _objectSpread({}, rest), key);
});
}
},
render: function render(_ref2) {
var Component = _ref2.component,
componentRef = _ref2.componentRef,
rest = _objectWithoutProperties(_ref2, _excluded2);
delete rest.childComponent;
delete rest.childProp;
delete rest.indexProp;
delete rest.itemProps;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _objectSpread({
ref: componentRef,
role: "list"
}, rest));
}
});
/**
* Applies Repeater behaviors.
*
* @hoc
* @memberof ui/Repeater
* @mixes ui/ForwardRef.ForwardRef
* @public
*/
var RepeaterDecorator = (0, _ForwardRef["default"])({
prop: 'componentRef'
});
/**
* A stateless component that stamps out copies of `childComponent`.
*
* @class Repeater
* @memberof ui/Repeater
* @extends ui/Repeater.RepeaterBase
* @mixes ui/Repeater.RepeaterDecorator
* @omit componentRef
* @ui
* @public
*/
var Repeater = exports.Repeater = RepeaterDecorator(RepeaterBase);
var _default = exports["default"] = Repeater;