@renderlesskit/react
Version:
Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit
101 lines (86 loc) • 5.04 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAccordionId = getAccordionId;
exports.isAccordionSelected = isAccordionSelected;
exports.isPanelVisible = isPanelVisible;
exports.useAccordionPanelId = useAccordionPanelId;
var React = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* When <AccordionPanel> is used without accordionId:
*
* - First render: getAccordionId will return undefined because options.panels
* doesn't contain the current panel yet (registerPanel wasn't called yet).
* Thus registerPanel will be called without groupId (accordionId).
*
* - Second render: options.panels already contains the current panel (because
* registerPanel was called in the previous render). This means that we'll be
* able to get the related accordionId with the accordion panel index. Basically,
* we filter out all the accordions and panels that have already matched. In this
* phase, registerPanel will be called again with the proper groupId (accordionId).
*
* - In the third render, panel.groupId will be already defined, so we just
* return it. registerPanel is not called.
*/
function getAccordionId(options) {
var _accordionsWithoutPan;
var panels = options.panels,
id = options.id,
items = options.items;
var panel = panels === null || panels === void 0 ? void 0 : panels.find(function (p) {
return p.id === id;
});
var accordionId = options.accordionId || (panel === null || panel === void 0 ? void 0 : panel.groupId);
if (accordionId || !panel || !panels || !items) {
return accordionId;
}
var panelIndex = getPanelIndex(panels, panel);
var accordionsWithoutPanel = getAccordionsWithoutPanel(items, panels);
return ((_accordionsWithoutPan = accordionsWithoutPanel[panelIndex]) === null || _accordionsWithoutPan === void 0 ? void 0 : _accordionsWithoutPan.id) || undefined;
}
function getPanelIndex(panels, panel) {
var panelsWithoutAccordionId = panels.filter(function (p) {
return !p.groupId;
});
return panelsWithoutAccordionId.indexOf(panel);
}
function getAccordionsWithoutPanel(accordions, panels) {
var panelsAccordionIds = panels.map(function (panel) {
return panel.groupId;
}).filter(Boolean);
return accordions.filter(function (item) {
return panelsAccordionIds.indexOf(item.id || undefined) === -1;
});
}
function isPanelVisible(options) {
var allowMultiple = options.allowMultiple,
selectedId = options.selectedId,
selectedIds = options.selectedIds;
var accordionId = getAccordionId(options);
if (allowMultiple) return accordionId ? selectedIds === null || selectedIds === void 0 ? void 0 : selectedIds.includes(accordionId) : false;
return accordionId ? selectedId === accordionId : false;
}
function isAccordionSelected(options) {
var id = options.id,
allowMultiple = options.allowMultiple,
selectedId = options.selectedId,
selectedIds = options.selectedIds;
if (!id) return;
if (allowMultiple) return selectedIds === null || selectedIds === void 0 ? void 0 : selectedIds.includes(id);
return selectedId === id;
}
function useAccordionPanelId(options) {
var panels = options.panels,
id = options.id;
return React.useMemo(function () {
var _panels$find;
return (panels === null || panels === void 0 ? void 0 : (_panels$find = panels.find(function (panel) {
return panel.groupId === id;
})) === null || _panels$find === void 0 ? void 0 : _panels$find.id) || undefined;
}, [panels, id]);
}
//# sourceMappingURL=helpers.js.map