From 04121b7bddbedc080388f3702453a9f3e7dc5c4c Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Fri, 14 May 2021 15:40:13 +0200 Subject: [PATCH] feat: collapsible sections --- src/components/BottomSheet.tsx | 77 ++++++++++++++++------------------ 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/components/BottomSheet.tsx b/src/components/BottomSheet.tsx index 072fe891..7b564686 100644 --- a/src/components/BottomSheet.tsx +++ b/src/components/BottomSheet.tsx @@ -239,20 +239,6 @@ const Item: React.FC< ); }; -const ExpandableSectionItem: React.FC<{ - section: BottomSheetExpandableSectionType; - expandSection: () => void; - expanded: boolean; -}> = ({ section, expandSection, expanded }) => ( - -); - const ActionItem: React.FC<{ action: BottomSheetAction; dismissBottomSheet: () => void; @@ -290,17 +276,10 @@ const Section: React.FC<{ section: BottomSheetSectionType & { animationValue: Animated.Value }; first: boolean; dismissBottomSheet: () => void; - expandSection: () => void; + onToggleExpand: () => void; stackIndex: number; expanded: boolean; -}> = ({ - section, - first, - dismissBottomSheet, - expandSection, - stackIndex, - expanded, -}) => { +}> = ({ section, first, dismissBottomSheet, onToggleExpand, stackIndex }) => { const [actionsContainerHeight, setActionsContainerHeight] = useState(0); const actionsContainerStyle = section.expandable @@ -325,10 +304,12 @@ const Section: React.FC<{ <> {section.expandable && ( - )} @@ -391,22 +372,36 @@ export const BottomSheet: React.FC = ({ snapPoints = contentHeight < maxLimit ? [contentHeight] : [maxLimit]; } - const expandSection = (sectionKey: string) => { - const animations: Animated.CompositeAnimation[] = []; - animatedSections.forEach(section => { - if (section.expandable) { - animations.push( + const toggleExpandSection = ({ + key: sectionKey, + animationValue, + }: { + key: string; + animationValue: Animated.Value; + }) => { + const duration = 250; + const useNativeDriver = false; + + if (expandedSectionKey) { + Animated.timing(animationValue, { + toValue: 0, + duration, + useNativeDriver, + }).start(() => setExpandedSectionKey('')); + } else { + const animations = animatedSections + .filter(section => section.expandable) + .map(section => Animated.timing(section.animationValue, { toValue: sectionKey === section.key ? 1 : 0, - duration: 250, - useNativeDriver: false, + duration, + useNativeDriver, }) ); - } - }); - Animated.parallel(animations).start(() => - setExpandedSectionKey(sectionKey) - ); + Animated.parallel(animations).start(() => + setExpandedSectionKey(sectionKey) + ); + } }; const onDismiss = () => { @@ -445,7 +440,9 @@ export const BottomSheet: React.FC = ({ section={section} first={index === 0} dismissBottomSheet={() => bottomSheetRef?.current?.dismiss()} - expandSection={() => expandSection(section.key)} + onToggleExpand={() => { + toggleExpandSection(section); + }} stackIndex={sections.length - index} expanded={section.key === expandedSectionKey} />