UNPKG

@enact/ui

Version:

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

233 lines (230 loc) 9.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupDecorator = exports.GroupBase = exports.Group = void 0; Object.defineProperty(exports, "GroupItem", { enumerable: true, get: function get() { return _GroupItem.GroupItem; } }); exports["default"] = 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 _compose = _interopRequireDefault(require("ramda/src/compose")); var _Changeable = _interopRequireDefault(require("../Changeable")); var _ForwardRef = _interopRequireDefault(require("../ForwardRef")); var _Repeater = _interopRequireDefault(require("../Repeater")); var _GroupItem = require("./GroupItem"); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["componentRef"]; /** * Provides a component that renders a group of components given a set of data. * * @module ui/Group * @exports Group * @exports GroupBase * @exports GroupDecorator * @exports GroupItem */ /** * A stateless component that supports selection of its child items via configurable * properties and events. * * @class GroupBase * @memberof ui/Group * @ui * @public */ 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); } 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; } var GroupBase = exports.GroupBase = (0, _kind["default"])({ name: 'Group', propTypes: /** @lends ui/Group.Group.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 content (i.e. passed as `children`). 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 content using `children`. * * NOTE: When providing an array of objects be sure a unique `key` is assigned to each * item. [Read about keys](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, /** * The name of the event that triggers activation. * * @type {String} * @default 'onClick' * @public */ childSelect: _propTypes2["default"].string, /** * Called with a reference to the root component. * * When using {@link ui/Group.Group}, 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 * * @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, /** * Callback method to be invoked when an item is activated. * * @type {Function} * @public */ onSelect: _propTypes2["default"].func, /** * Selection mode for the group * * * `single` - Allows for 0 or 1 item to be selected. The selected item may be deselected. * * `radio` - Allows for 0 or 1 item to be selected. The selected item may only be * deselected by selecting another item. * * `multiple` - Allows 0 to _n_ items to be selected. Each item may be selected or * deselected. * * @type {String} * @default 'single' * @public */ select: _propTypes2["default"].oneOf(['single', 'radio', 'multiple']), /** * The index(es) of the currently activated item. * * @type {Number|Array} * @public */ selected: _propTypes2["default"].oneOfType([_propTypes2["default"].number, _propTypes2["default"].array]), /** * The key that will hold the value in the event passed to `onSelect`. * * @type {String} * @default 'data' * @public */ selectedEventProp: _propTypes2["default"].string, /** * The name of the DOM property that represents the selected state. * * @type {String} * @default 'data-selected' * @public */ selectedProp: _propTypes2["default"].string }, defaultProps: { childProp: 'children', childSelect: 'onClick', indexProp: 'data-index', select: 'single', selectedEventProp: 'data', selectedProp: 'data-selected' }, computed: { 'aria-multiselectable': function ariaMultiselectable(_ref) { var select = _ref.select; return select === 'multiple'; }, itemProps: function itemProps(props) { return Object.assign({}, (0, _GroupItem.pickGroupItemProps)(props), props.itemProps); } }, render: function render(_ref2) { var componentRef = _ref2.componentRef, rest = _objectWithoutProperties(_ref2, _excluded); delete rest.onSelect; delete rest.childSelect; delete rest.select; delete rest.selected; delete rest.selectedEventProp; delete rest.selectedProp; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Repeater["default"], _objectSpread(_objectSpread({ role: "group" }, rest), {}, { childComponent: _GroupItem.GroupItem, ref: componentRef })); } }); /** * A higher-order component that adds behavior to {@link ui/Group.GroupBase|Group}. * * @hoc * @memberof ui/Group * @mixes ui/ForwardRef.ForwardRef * @mixes ui/Changeable.Changeable * @public */ var GroupDecorator = exports.GroupDecorator = (0, _compose["default"])((0, _ForwardRef["default"])({ prop: 'componentRef' }), (0, _Changeable["default"])({ change: 'onSelect', prop: 'selected' })); /** * A component that supports selection of its child items via configurable properties and * events. * * Selected item is managed by {@link ui/Changeable.Changeable}. * * @class Group * @memberof ui/Group * @extends ui/Group.GroupBase * @mixes ui/Group.GroupDecorator * @omit componentRef * @ui * @public */ var Group = exports.Group = GroupDecorator(GroupBase); var _default = exports["default"] = Group;