1 | "use client";
|
2 |
|
3 | import * as React from 'react';
|
4 | import { useContext } from 'react';
|
5 | import classNames from 'classnames';
|
6 | import AccordionContext, { isAccordionItemSelected } from './AccordionContext';
|
7 | import AccordionItemContext from './AccordionItemContext';
|
8 | import { useBootstrapPrefix } from './ThemeProvider';
|
9 | import { jsx as _jsx } from "react/jsx-runtime";
|
10 | export function useAccordionButton(eventKey, onClick) {
|
11 | const {
|
12 | activeEventKey,
|
13 | onSelect,
|
14 | alwaysOpen
|
15 | } = useContext(AccordionContext);
|
16 | return e => {
|
17 | |
18 |
|
19 |
|
20 |
|
21 | let eventKeyPassed = eventKey === activeEventKey ? null : eventKey;
|
22 | if (alwaysOpen) {
|
23 | if (Array.isArray(activeEventKey)) {
|
24 | if (activeEventKey.includes(eventKey)) {
|
25 | eventKeyPassed = activeEventKey.filter(k => k !== eventKey);
|
26 | } else {
|
27 | eventKeyPassed = [...activeEventKey, eventKey];
|
28 | }
|
29 | } else {
|
30 |
|
31 | eventKeyPassed = [eventKey];
|
32 | }
|
33 | }
|
34 | onSelect == null ? void 0 : onSelect(eventKeyPassed, e);
|
35 | onClick == null ? void 0 : onClick(e);
|
36 | };
|
37 | }
|
38 | const AccordionButton = React.forwardRef(({
|
39 |
|
40 | as: Component = 'button',
|
41 | bsPrefix,
|
42 | className,
|
43 | onClick,
|
44 | ...props
|
45 | }, ref) => {
|
46 | bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-button');
|
47 | const {
|
48 | eventKey
|
49 | } = useContext(AccordionItemContext);
|
50 | const accordionOnClick = useAccordionButton(eventKey, onClick);
|
51 | const {
|
52 | activeEventKey
|
53 | } = useContext(AccordionContext);
|
54 | if (Component === 'button') {
|
55 | props.type = 'button';
|
56 | }
|
57 | return _jsx(Component, {
|
58 | ref: ref,
|
59 | onClick: accordionOnClick,
|
60 | ...props,
|
61 | "aria-expanded": Array.isArray(activeEventKey) ? activeEventKey.includes(eventKey) : eventKey === activeEventKey,
|
62 | className: classNames(className, bsPrefix, !isAccordionItemSelected(activeEventKey, eventKey) && 'collapsed')
|
63 | });
|
64 | });
|
65 | AccordionButton.displayName = 'AccordionButton';
|
66 | export default AccordionButton; |
\ | No newline at end of file |