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 145 146 147 148 149 150 151 152 | 1x 2x 4x 1x 2x 2x 2x 1x 1x 1x 1x 2x 1x 2x 2x | import React from "react";
import ExpandLessIcon from "@mui/icons-material/ExpandLess";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import IconButton from "@mui/material/IconButton";
import Box from "@mui/material/Box";
interface AccordionHeaderProps {
active: boolean;
headerLeftItems: React.ReactNode[];
headerRightItems: React.ReactNode[];
label: React.ReactNode;
onClick: (event: React.MouseEvent<HTMLDivElement>) => void;
style?: React.CSSProperties;
forceActive?: boolean;
}
interface AccordionItemProps {
active: boolean;
body: React.ReactNode;
label: React.ReactNode;
onHeadClick: (event: React.MouseEvent<HTMLDivElement>) => void;
headerRightItems?: React.ReactNode[];
headerLeftItems?: React.ReactNode[];
headStyle?: React.CSSProperties;
bodyStyle?: React.CSSProperties;
style?: React.CSSProperties;
wrapperProps?: React.HTMLProps<HTMLDivElement>;
forceActive?: boolean;
}
const AccordionHeader = React.memo(({ active, headerLeftItems, headerRightItems, label, onClick, style, forceActive }: AccordionHeaderProps) => {
return (
<Box style={style} onClick={onClick}>
<span style={{ display: "inline-block", margin: "-10px 0px -10px -5px" }}>
{headerLeftItems.map((item, index) => {
return (
<span key={index} style={{ display: "inline-block", margin: "0 5px" }}>
{item}
</span>
);
})}
</span>
<span style={{ position: "absolute", top: "8px", right: "5px" }}>
{headerRightItems.map((item, index) => {
return (
<span key={index} style={{ display: "inline-block", margin: "0 5px" }}>
{item}
</span>
);
})}
{forceActive ? undefined : (
<IconButton size='small' aria-label='Expand'>
{active ? <ExpandLessIcon /> : <ExpandMoreIcon />}{" "}
</IconButton>
)}
</span>
{label}
</Box>
);
});
const AccordionItem = ({
active,
body,
label,
onHeadClick,
headerRightItems = [],
headerLeftItems = [],
headStyle,
bodyStyle,
style,
wrapperProps,
forceActive,
}: AccordionItemProps) => {
const _headStyle: React.CSSProperties = {
border: "solid 1px #d8d8d8",
padding: "16px",
display: "block",
cursor: "pointer",
marginTop: 8,
position: "relative",
...headStyle,
};
const _bodyStyle: React.CSSProperties = {
display: active ? "block" : "none",
padding: "16px 0",
border: "solid 1px #d8d8d8",
borderTopWidth: 0,
...bodyStyle,
};
return (
<div style={style} className='accordion-item' {...wrapperProps}>
<AccordionHeader
style={_headStyle}
onClick={onHeadClick}
headerLeftItems={headerLeftItems}
headerRightItems={headerRightItems}
forceActive={forceActive}
active={active}
label={label}
/>
<Box style={_bodyStyle}>{active ? body : null}</Box>
</div>
);
};
interface AccordionProps {
index?: number;
onChange?: (index: number) => void;
style?: React.CSSProperties;
forceActive?: boolean;
children: React.ReactElement<AccordionItemProps>[];
}
const Accordion = ({ index, onChange, style, forceActive, children }: AccordionProps) => {
const [internalIndex, setInternalIndex] = React.useState(-1);
const openedIndex = index !== undefined ? index : internalIndex;
const handleChange = React.useCallback(
(i: number) => {
return () => {
if (index !== undefined) {
if (onChange) {
onChange(i);
}
} else {
const newIndex = i !== internalIndex ? i : -1;
setInternalIndex(newIndex);
}
};
},
[index, onChange, internalIndex]
);
return (
<div className='accordion' style={style}>
{children.map((item, idx) => {
const active = forceActive || idx === openedIndex;
return React.cloneElement(item, {
active,
onHeadClick: handleChange(idx),
});
})}
</div>
);
};
export { Accordion, AccordionItem };
|