All files / packages/sds-subsystems/.storybook/addons/theme-builder/src/components Accordion.jsx

0% Statements 0/40
0% Branches 0/24
0% Functions 0/3
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                                                                                                                                               
import React, { useState, useCallback, useRef, isValidElement, cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import AccordionPanel from './AccordionPanel';
 
/**
 * Lightning Design System Accordion Component
 * A React 19 compatible implementation following SLDS patterns
 */
const Accordion = ({
  id,
  children,
  className = '',
  allowMultipleOpen = false,
  assistiveText = {},
  ...otherProps
}) => {
  const [openPanels, setOpenPanels] = useState(new Set());
  const [focusIndex, setFocusIndex] = useState(0);
  const headerButtonsRefs = useRef([]);
 
  const handlePanelToggle = useCallback(
    (panelId, isExpanded) => {
      setOpenPanels((prevOpenPanels) => {
        const newOpenPanels = new Set(prevOpenPanels);
 
        if (isExpanded) {
          if (!allowMultipleOpen) {
            // Close all other panels if multiple open is not allowed
            newOpenPanels.clear();
          }
          newOpenPanels.add(panelId);
        } else {
          newOpenPanels.delete(panelId);
        }
 
        return newOpenPanels;
      });
    },
    [allowMultipleOpen],
  );
 
  const accordionClasses = `slds-accordion ${className}`.trim();
 
  // Default assistive text
  const defaultAssistiveText = {
    accordion: 'Accordion',
  };
 
  const mergedAssistiveText = { ...defaultAssistiveText, ...assistiveText };
 
  const focusHeaderAt = useCallback(
    (index) => {
      const clamped = Math.max(0, Math.min(index, Children.count(children) - 1));
      const node = headerButtonsRefs.current[clamped];
      if (node && typeof node.focus === 'function') {
        node.focus();
      }
      setFocusIndex(clamped);
    },
    [children],
  );
 
  const onHeaderKeyDown = useCallback(
    (index, event) => {
      const key = event.key;
      if (key === 'ArrowDown' || key === 'ArrowRight') {
        event.preventDefault();
        focusHeaderAt(index + 1);
        return;
      }
      if (key === 'ArrowUp' || key === 'ArrowLeft') {
        event.preventDefault();
        focusHeaderAt(index - 1);
        return;
      }
      if (key === 'Home') {
        event.preventDefault();
        focusHeaderAt(0);
        return;
      }
      if (key === 'End') {
        event.preventDefault();
        focusHeaderAt(Children.count(children) - 1);
        return;
      }
    },
    [children, focusHeaderAt],
  );
 
  return (
    <ul
      id={id}
      className={accordionClasses}
      role="presentation"
      aria-label={mergedAssistiveText.accordion}
      {...otherProps}
    >
      {Children.map(children, (child, index) => {
        if (isValidElement(child) && child.type === AccordionPanel) {
          const panelId = child.props.id || `panel-${index}`;
          const isExpanded = openPanels.has(panelId);
 
          return cloneElement(child, {
            ...child.props,
            id: panelId,
            expanded: child.props.expanded !== undefined ? child.props.expanded : isExpanded,
            onTogglePanel:
              child.props.onTogglePanel ||
              ((event, data) => {
                const newExpanded = !isExpanded;
                handlePanelToggle(panelId, newExpanded);
              }),
            headerTabIndex: focusIndex === index ? 0 : -1,
            onHeaderKeyDown: (e) => onHeaderKeyDown(index, e),
            setHeaderButtonRef: (node) => {
              headerButtonsRefs.current[index] = node;
            },
          });
        }
        return child;
      })}
    </ul>
  );
};
 
// PropTypes for Accordion
Accordion.propTypes = {
  id: PropTypes.string.isRequired,
  children: PropTypes.node,
  className: PropTypes.string,
  allowMultipleOpen: PropTypes.bool,
  assistiveText: PropTypes.shape({
    accordion: PropTypes.string,
  }),
};
 
Accordion.defaultProps = {
  className: '',
  allowMultipleOpen: false,
  assistiveText: {},
};
 
export default Accordion;