|
1 | 1 | import clsx from 'clsx'; |
2 | | -import React, { useState } from 'react'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
3 | 3 | import type { AccordionProps } from './types'; |
4 | 4 | import { AccordionContext } from './AccordionContext'; |
5 | 5 |
|
6 | 6 | const MDBAccordion: React.FC<AccordionProps> = React.forwardRef<HTMLAllCollection, AccordionProps>( |
7 | | - ({ alwaysOpen, className, flush, initialActive, tag: Tag, children, ...props }, ref) => { |
| 7 | + ({ alwaysOpen, className, flush, initialActive, tag: Tag, children, onChange, ...props }, ref) => { |
8 | 8 | const classes = clsx('accordion', flush && 'accordion-flush', className); |
9 | 9 |
|
10 | 10 | const [activeItem, setActiveItem] = useState(initialActive); |
11 | 11 |
|
| 12 | + useEffect(() => { |
| 13 | + if (!activeItem) return; |
| 14 | + |
| 15 | + onChange && onChange(activeItem); |
| 16 | + }, [onChange, activeItem]); |
| 17 | + |
12 | 18 | return ( |
13 | | - <AccordionContext.Provider value={{ activeItem, setActiveItem, alwaysOpen, initialActive }}> |
14 | | - <Tag className={classes} ref={ref} {...props}> |
| 19 | + <Tag className={classes} ref={ref} {...props}> |
| 20 | + <AccordionContext.Provider value={{ activeItem, setActiveItem, alwaysOpen, initialActive }}> |
15 | 21 | {children} |
16 | | - </Tag> |
17 | | - </AccordionContext.Provider> |
| 22 | + </AccordionContext.Provider> |
| 23 | + </Tag> |
18 | 24 | ); |
19 | 25 | } |
20 | 26 | ); |
|
0 commit comments