File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
@navikt/core/react/src/accordion Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,29 @@ import { AccordionContext } from "./AccordionContext";
55import AccordionHeader , { AccordionHeaderProps } from "./AccordionHeader" ;
66import AccordionItem , { AccordionItemProps } from "./AccordionItem" ;
77
8+ let hasWarnedAboutItems = false ;
9+
10+ function checkAccordionItems ( ) {
11+ // eslint-disable-next-line react-hooks/rules-of-hooks
12+ React . useEffect ( ( ) => {
13+ document . querySelectorAll ( ".aksel-accordion" ) . forEach ( ( accordion ) => {
14+ if ( hasWarnedAboutItems || accordion . children . length !== 1 ) {
15+ return ;
16+ }
17+ if ( accordion . nextElementSibling ?. classList . contains ( "aksel-accordion" ) ) {
18+ console . warn (
19+ "[Aksel] Do not put multiple accordions directly after each other. Use one <Accordion> with multiple <Accordion.Item> instead." ,
20+ ) ;
21+ } else {
22+ console . warn (
23+ "[Aksel] Accordions should have more than one item. Consider using ExpansionPanel instead." ,
24+ ) ;
25+ }
26+ hasWarnedAboutItems = true ;
27+ } ) ;
28+ } , [ ] ) ;
29+ }
30+
831interface AccordionComponent
932 extends React . ForwardRefExoticComponent <
1033 AccordionProps & React . RefAttributes < HTMLDivElement >
@@ -89,6 +112,10 @@ export const Accordion = forwardRef<HTMLDivElement, AccordionProps>(
89112 ) => {
90113 const { cn } = useRenameCSS ( ) ;
91114
115+ if ( process . env . NODE_ENV !== "production" ) {
116+ checkAccordionItems ( ) ;
117+ }
118+
92119 return (
93120 < AccordionContext . Provider
94121 value = { {
You can’t perform that action at this time.
0 commit comments