UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

281 lines (280 loc) 9.63 kB
"use client"; var _AccordionHeader; import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import React, { useContext, useState, useEffect, useRef, useMemo } from 'react'; import clsx from 'clsx'; import { makeUniqueId, findElementInChildren, extendPropsWithContext, validateDOMAttributes, dispatchCustomElementEvent } from "../../shared/component-helper.js"; import { applySpacing } from "../space/SpacingUtils.js"; import AccordionGroup from "./AccordionGroup.js"; import AccordionHeader from "./AccordionHeader.js"; import AccordionContent from "./AccordionContent.js"; import AccordionContext from "./AccordionContext.js"; import AccordionProviderContext from "./AccordionProviderContext.js"; import Context from "../../shared/Context.js"; import { AccordionStore, Store, rememberWarning } from "./AccordionStore.js"; import { accordionDefaultProps } from "./types.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; export { accordionDefaultProps } from "./types.js"; function Accordion({ variant = 'outlined', iconSize = 'medium', ...restOfProps }) { const props = { variant, iconSize, ...restOfProps }; const context = useContext(AccordionProviderContext); const group = props.group || (context === null || context === void 0 ? void 0 : context.group); const id = useRef(props.id || makeUniqueId()).current; const store = new Store({ id: props.id, group }); const [previousExpanded, setPreviousExpanded] = useState(props.expanded); const [expanded, setExpanded] = useState(getInitialExpandedState()); const hasAddedCallbackRef = useRef(false); if (props.expanded !== previousExpanded) { setExpanded(props.expanded !== undefined ? props.expanded : false); setPreviousExpanded(props.expanded); } const thisInstance = { _id: id, context, handleDisabledClick, callOnChange, callOnChangeHandler, close, setExpandedState, state: { expanded, group }, props, store }; useEffect(() => { if (group && typeof window !== 'undefined') { window['__dnbAccordion'] = window['__dnbAccordion'] || {}; window['__dnbAccordion'][group] = window['__dnbAccordion'][group] || new AccordionStore(group); window['__dnbAccordion'][group].addInstance(thisInstance); } if (context && typeof (context === null || context === void 0 ? void 0 : context.onInit) === 'function') { context.onInit(thisInstance); } return () => { if (group && typeof window !== 'undefined') { var _window; (_window = window) === null || _window === void 0 || (_window = _window['__dnbAccordion'][group]) === null || _window === void 0 || _window.removeInstance(thisInstance); } }; }, []); useEffect(() => { if (context.flushRememberedState) { store.flush(); setExpanded(props.expanded); } if (context !== null && context !== void 0 && context.expandedId && context.expandedId === props.id) { setExpanded(true); } }, [context.flushRememberedState, context.expandedId, props.expanded, props.id, store]); if (context !== null && context !== void 0 && context.collapseAllHandleRef && !hasAddedCallbackRef.current) { var _context$collapseAcco, _context; context === null || context === void 0 || (_context$collapseAcco = context.collapseAccordionCallbacks) === null || _context$collapseAcco === void 0 || _pushInstanceProperty(_context = _context$collapseAcco.current).call(_context, close); hasAddedCallbackRef.current = true; } function getInitialExpandedState() { if (props.expandedSsr || context !== null && context !== void 0 && context.expandedSsr) { return typeof window === 'undefined'; } if (props.rememberState || context.rememberState) { const storedExpanded = store.getState(); if (props.expanded && storedExpanded === false) { return false; } if (storedExpanded) { return true; } } return props.expanded !== undefined ? props.expanded : (context === null || context === void 0 ? void 0 : context.expanded) !== undefined ? context.expanded : false; } function setExpandedState(expanded) { setExpanded(expanded); } function close() { changeOpened(false); } function changeOpened(expanded) { setExpanded(expanded); if (props.rememberState || context.rememberState) { store.saveState(expanded); } } function handleDisabledClick(e) { e.preventDefault(); return false; } function callOnChangeHandler(params) { callOnChange(params); if (context !== null && context !== void 0 && context.onChange) { context === null || context === void 0 || context.onChange(params); } if (group && typeof window !== 'undefined') { var _window2; (_window2 = window) === null || _window2 === void 0 || (_window2 = _window2['__dnbAccordion'][group]) === null || _window2 === void 0 || _window2.onChange(params); } } function callOnChange(params) { const { expanded, event } = params; changeOpened(expanded); dispatchCustomElementEvent(thisInstance, 'onChange', { expanded, event }); } const globalContext = useContext(Context); const nestedContext = useContext(AccordionContext); let expandedState = expanded; const extendedProps = extendPropsWithContext(props, accordionDefaultProps, context, nestedContext, { skeleton: globalContext === null || globalContext === void 0 ? void 0 : globalContext.skeleton }, globalContext.Accordion, globalContext.translation['Accordion']); if (expandedState === undefined && globalContext.Accordion) { if (globalContext.Accordion.expanded) { expandedState = extendedProps.expanded; } } const { variant: extendedVariant, className, keepInDOM, preventRerender, preventRerenderConditional, singleContainer, rememberState, disabled, skeleton, noAnimation, expandedSsr: _expandedSsr, children, id: _id, group: _group, title, description, leftComponent, icon, iconPosition, iconSize: _iconSize, onChange, contentRef, ...restOfExtendedProps } = extendedProps; const mainParams = applySpacing(extendedProps, { id, className: clsx('dnb-accordion', className, expandedState && 'dnb-accordion--expanded', extendedVariant && `dnb-accordion__variant--${extendedVariant}`, keepInDOM && 'dnb-accordion--prerender') }); if (disabled) { mainParams.onClick = handleDisabledClick; } validateDOMAttributes(props, restOfExtendedProps); const extendedPropsForContext = extendPropsWithContext(props, accordionDefaultProps, { expanded, group }, context); const accordionContext = { ...extendedPropsForContext, id, expanded: expandedState, keepInDOM: keepInDOM, preventRerender: preventRerender, preventRerenderConditional: preventRerenderConditional, singleContainer: singleContainer, rememberState: rememberState, disabled: disabled, skeleton: skeleton, noAnimation: noAnimation, callOnChange: callOnChangeHandler }; return _jsx(AccordionContext, { value: accordionContext, children: _jsxs("div", { ...mainParams, children: [findElementInChildren(children, cur => cur.type === AccordionHeader) ? null : _AccordionHeader || (_AccordionHeader = _jsx(AccordionHeader, {})), findElementInChildren(children, cur => cur.type === AccordionContent) ? children : _jsx(AccordionContent, { children: children })] }) }); } const Group = ({ expandBehavior = 'single', ...props }) => { if (props.rememberState && !props.id) { rememberWarning('accordion group'); } const [expandedId, setExpandedId] = useState(null); const instanceIDs = useRef([]); const group = props !== null && props !== void 0 && props.id ? props.id : !props.group ? '#' + makeUniqueId() : undefined; const store = useMemo(() => new Store({ group }), [group]); useEffect(() => { const storedData = store.getData(); const currentIDs = instanceIDs === null || instanceIDs === void 0 ? void 0 : instanceIDs.current; if (!(storedData !== null && storedData !== void 0 && storedData.id)) { return; } if (currentIDs.includes(storedData === null || storedData === void 0 ? void 0 : storedData.id)) { return; } const fallbackId = currentIDs[0]; if (!fallbackId) { return; } setExpandedId(fallbackId); }, [store]); useEffect(() => { if (!expandedId) { return; } store.saveState(true, expandedId); setExpandedId(null); }, [expandedId, store]); function onInit(instance) { if (instance.props.id && !instanceIDs.current.includes(instance.props.id)) { var _context2; _pushInstanceProperty(_context2 = instanceIDs.current).call(_context2, instance.props.id); } } return _jsx(AccordionGroup, { onInit: onInit, ...props, group: group, expandBehavior: expandBehavior, expandedId: expandedId || props.expandedId }); }; Accordion.Provider = AccordionGroup; Accordion.Header = AccordionHeader; Accordion.Content = AccordionContent; Accordion.Group = Group; Group.Store = (group, id = null) => { return new Store({ group, id }); }; Accordion.Store = id => { return new Store({ id }); }; withComponentMarkers(Accordion, { _supportsSpacingProps: true }); export default Accordion; //# sourceMappingURL=Accordion.js.map