Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make only one collapse open #71

Open
FatmaMahmoud698 opened this issue Dec 23, 2021 · 1 comment
Open

How to make only one collapse open #71

FatmaMahmoud698 opened this issue Dec 23, 2021 · 1 comment

Comments

@FatmaMahmoud698
Copy link

I only want one open at a time. I've tried changing states of collapse, but nothing is working. I can open all of the items. Any thoughts?

@Rohan-Rajesh
Copy link

You have to use isExpanded as well as onToggle, the gist is you store the selected index in a state variable and only set expanded of an item if the index is equal to the selected index while looping through, here is an example:

const content: SupportData[] = [
    {title: 'Title 1', content: 'Content 1'},
    {title: 'Title 2', content: 'Content 2'},
  ];
  const [expandedIndex, setExpandedIndex] = useState<number | null>(0);

  return (
    <ScrollView contentContainerStyle={styles.container}>
      <View>
        <View style={styles.body}>
          {content.map((item, index) => (
            <Collapse
              key={index}
              style={styles.accordionItem}
              isExpanded={index === expandedIndex}
              onToggle={() => setExpandedIndex(index)}>
              <CollapseHeader style={styles.accordionHeader}>
                <Text style={styles.accordionTitle}>{item.title}</Text>
                <Image source={DownBlue} style={styles.downArrow} />
              </CollapseHeader>
              <CollapseBody>
                <Text style={styles.accordionContent}>{item.content}</Text>
              </CollapseBody>
            </Collapse>
          ))}
        </View>
      </View>
    </ScrollView>
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants