UNPKG

@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

81 lines (73 loc) 3 kB
import * as React from "react"; /** * 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. */ export function getAccordionId(options) { var _accordionsWithoutPan; var { panels, id, items } = options; var panel = panels === null || panels === void 0 ? void 0 : panels.find(p => 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(p => !p.groupId); return panelsWithoutAccordionId.indexOf(panel); } function getAccordionsWithoutPanel(accordions, panels) { var panelsAccordionIds = panels.map(panel => panel.groupId).filter(Boolean); return accordions.filter(item => panelsAccordionIds.indexOf(item.id || undefined) === -1); } export function isPanelVisible(options) { var { allowMultiple, selectedId, selectedIds } = options; var accordionId = getAccordionId(options); if (allowMultiple) return accordionId ? selectedIds === null || selectedIds === void 0 ? void 0 : selectedIds.includes(accordionId) : false; return accordionId ? selectedId === accordionId : false; } export function isAccordionSelected(options) { var { id, allowMultiple, selectedId, selectedIds } = options; if (!id) return; if (allowMultiple) return selectedIds === null || selectedIds === void 0 ? void 0 : selectedIds.includes(id); return selectedId === id; } export function useAccordionPanelId(options) { var { panels, id } = options; return React.useMemo(() => { var _panels$find; return (panels === null || panels === void 0 ? void 0 : (_panels$find = panels.find(panel => panel.groupId === id)) === null || _panels$find === void 0 ? void 0 : _panels$find.id) || undefined; }, [panels, id]); } //# sourceMappingURL=helpers.js.map