All files / kernel-ui/src/components/shared Accordion.tsx

100% Statements 9/9
100% Branches 10/10
100% Functions 2/2
100% Lines 9/9

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                                            5x             64x     64x   64x 17x   17x 15x   2x       64x                                                
import { Box, Icon, IconName } from '@metamask/design-system-react';
import { useState } from 'react';
 
export type AccordionProps = {
  title: React.ReactNode;
  children: React.ReactNode;
  isExpanded?: boolean;
  onToggle?: (isExpanded: boolean) => void;
  testId?: string;
};
 
/**
 * A reusable accordion component that can expand and collapse content.
 *
 * @param props - The accordion props.
 * @param props.title - The title to display in the accordion header.
 * @param props.children - The content to display when expanded.
 * @param props.isExpanded - Whether the accordion is expanded (controlled).
 * @param props.onToggle - Callback when the accordion is toggled.
 * @param props.testId - Test ID for the accordion container.
 * @returns An accordion component.
 */
export const Accordion: React.FC<AccordionProps> = ({
  title,
  children,
  isExpanded: controlledExpanded,
  onToggle,
  testId,
}) => {
  const [internalExpanded, setInternalExpanded] = useState(false);
 
  // Use controlled state if provided, otherwise use internal state
  const isExpanded = controlledExpanded ?? internalExpanded;
 
  const handleToggle = (): void => {
    const newExpanded = !isExpanded;
 
    if (onToggle) {
      onToggle(newExpanded);
    } else {
      setInternalExpanded(newExpanded);
    }
  };
 
  return (
    <Box
      className="mb-4 border border-border-default hover:border-primary-default rounded-lg overflow-hidden"
      data-testid={testId}
    >
      <Box
        className="flex justify-between items-center p-3 cursor-pointer transition-colors select-none"
        onClick={handleToggle}
        data-testid="accordion-header"
      >
        <Box className="flex items-center" data-testid="accordion-title">
          {title}
        </Box>
        <Box
          className="text-lg w-5 h-5 flex items-center justify-center text-text-muted"
          data-testid={`accordion-icon-${isExpanded ? 'minus' : 'add'}`}
        >
          <Icon name={isExpanded ? IconName.Minus : IconName.Add} />
        </Box>
      </Box>
      {isExpanded && <Box data-testid="accordion-content">{children}</Box>}
    </Box>
  );
};