How to open/close Accordion programmatically #824
-
Hi Author and the community, I'm asking for a help about how to open/close the accordion programmatically? I've been searching in the docs and the issue there is no answer related to this topic. Any ideas and workaround would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 19 replies
-
All of our components can be "controlled" if you would like to open/close them programmatically. const [value, setValue] = React.useState('one');
return () => (
<div>
{/* Something outside of Accordion that controls the open content */}
<button onClick={() => setValue('two')}>Open two</button>
<Accordion.Root type="single" value={value} onValueChange={setValue}>
<Accordion.Item value="one">
<Accordion.Header>
<Accordion.Trigger>One</Accordion.Trigger>
</Accordion.Header>
<Accordion.Content>
Content for one
<Accordion.Content>
</Accordion.Item>
<Accordion.Item value="two">
<Accordion.Header>
<Accordion.Trigger>Two</Accordion.Trigger>
</Accordion.Header>
<Accordion.Content>
Content for two
<Accordion.Content>
</Accordion.Item>
</Accordion.Root>
</div> |
Beta Was this translation helpful? Give feedback.
-
Is there a way to know the open or closed state of an accordion? |
Beta Was this translation helpful? Give feedback.
-
Could anyone explain me what the purpose is of |
Beta Was this translation helpful? Give feedback.
-
Hey all, import { Root as RDXAccordion } from "@radix-ui/react-accordion";
import clsx from "clsx";
import { Children, isValidElement, useEffect, useState } from "react";
import styles from "./Accordion.module.scss";
import { AccordionProps } from "./types.ts";
export function Accordion({
children,
className,
type,
dataTestId,
openAll,
...props
}: AccordionProps) {
const [openAccordionItems, setOpenAccordionItems] = useState<string[]>();
const setOpenValues = (values: string[]) => {
setOpenAccordionItems(values);
};
useEffect(() => {
const convertAllAccordionItemValuesToStringArray = () => {
const values: string[] = [];
const getAllAccordionItemValues = (children: React.ReactNode) => {
Children.forEach(children, (child) => {
if (isValidElement(child)) {
console.log("Test", child);
if ((child.type as any).displayName === "AccordionItem")
return values.push((child as React.ReactElement).props.value);
return getAllAccordionItemValues(
(child as React.ReactElement).props.children
);
}
});
};
getAllAccordionItemValues(children);
return values;
};
if (openAll) {
setOpenAccordionItems(convertAllAccordionItemValuesToStringArray());
} else {
setOpenAccordionItems([]);
}
}, [children, openAll]);
return (
<RDXAccordion
type={type || "multiple"}
className={clsx(className, styles.accordion)}
data-testid={dataTestId || "accordion"}
value={openAccordionItems} //there has to be a array of AccordionItem Values to open all accordions
onValueChange={setOpenValues}
{...props}
>
{children}
</RDXAccordion>
);
}` I hope someone have an Idea 🔥 ❤️ |
Beta Was this translation helpful? Give feedback.
-
@jjenzz Hello jenna I want my accrdion item to disable when its data state is open how can i achieve this thank you ? |
Beta Was this translation helpful? Give feedback.
All of our components can be "controlled" if you would like to open/close them programmatically.