|
| 1 | +import React, {useState} from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import Animated, {useAnimatedReaction, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated'; |
| 4 | +import type {SharedValue} from 'react-native-reanimated'; |
| 5 | +import Accordion from '@components/Accordion'; |
| 6 | +import Badge from '@components/Badge'; |
| 7 | +import Icon from '@components/Icon'; |
| 8 | +import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback'; |
| 9 | +import Text from '@components/Text'; |
| 10 | +import useAccordionAnimation from '@hooks/useAccordionAnimation'; |
| 11 | +import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; |
| 12 | +import useTheme from '@hooks/useTheme'; |
| 13 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 14 | +import variables from '@styles/variables'; |
| 15 | +import CONST from '@src/CONST'; |
| 16 | +import type ChildrenProps from '@src/types/utils/ChildrenProps'; |
| 17 | + |
| 18 | +type SearchTypeMenuAccordionProps = ChildrenProps & { |
| 19 | + title: string; |
| 20 | + defaultExpanded?: boolean; |
| 21 | + badgeText?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +type AnimatedBadgeProps = { |
| 25 | + text: string; |
| 26 | + isExpanded: SharedValue<boolean>; |
| 27 | +}; |
| 28 | + |
| 29 | +function getBadgeOpacity(isExpanded: boolean) { |
| 30 | + return Number(!isExpanded); |
| 31 | +} |
| 32 | + |
| 33 | +function getBadgeOffsetY(isExpanded: boolean): `${number}%` | number { |
| 34 | + return isExpanded ? '50%' : 0; |
| 35 | +} |
| 36 | + |
| 37 | +function getArrowRotation(isExpanded: boolean) { |
| 38 | + return isExpanded ? 0 : 180; |
| 39 | +} |
| 40 | + |
| 41 | +function AnimatedBadge({text, isExpanded}: AnimatedBadgeProps) { |
| 42 | + const styles = useThemeStyles(); |
| 43 | + |
| 44 | + const badgeOpacity = useSharedValue(getBadgeOpacity(isExpanded.get())); |
| 45 | + const badgeOffsetY = useSharedValue(getBadgeOffsetY(isExpanded.get())); |
| 46 | + |
| 47 | + useAnimatedReaction( |
| 48 | + () => isExpanded.get(), |
| 49 | + (isExpandedValue) => { |
| 50 | + badgeOpacity.set(withTiming(getBadgeOpacity(isExpandedValue), {duration: CONST.ANIMATED_TRANSITION})); |
| 51 | + badgeOffsetY.set(withTiming(getBadgeOffsetY(isExpandedValue), {duration: CONST.ANIMATED_TRANSITION})); |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + const badgeAnimatedStyle = useAnimatedStyle(() => ({ |
| 56 | + opacity: badgeOpacity.get(), |
| 57 | + transform: [{translateY: badgeOffsetY.get()}], |
| 58 | + })); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Animated.View style={[badgeAnimatedStyle]}> |
| 62 | + <Badge |
| 63 | + text={text} |
| 64 | + badgeStyles={styles.searchSectionBadge} |
| 65 | + success |
| 66 | + isCondensed |
| 67 | + /> |
| 68 | + </Animated.View> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +function SearchTypeMenuAccordion({title, defaultExpanded = true, badgeText, children}: SearchTypeMenuAccordionProps) { |
| 73 | + const icons = useMemoizedLazyExpensifyIcons(['UpArrow']); |
| 74 | + const theme = useTheme(); |
| 75 | + const styles = useThemeStyles(); |
| 76 | + const [isExpanded, setIsExpanded] = useState(defaultExpanded); |
| 77 | + const {isAccordionExpanded, shouldAnimateAccordionSection} = useAccordionAnimation(isExpanded); |
| 78 | + |
| 79 | + const toggleSection = () => { |
| 80 | + setIsExpanded((prevIsExpanded) => !prevIsExpanded); |
| 81 | + }; |
| 82 | + |
| 83 | + const arrowRotation = useSharedValue(getArrowRotation(defaultExpanded)); |
| 84 | + |
| 85 | + useAnimatedReaction( |
| 86 | + () => isAccordionExpanded.get(), |
| 87 | + (isExpandedValue) => { |
| 88 | + const rotateDegree = getArrowRotation(isExpandedValue); |
| 89 | + arrowRotation.set(withTiming(rotateDegree, {duration: CONST.ANIMATED_TRANSITION})); |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + const arrowAnimatedStyle = useAnimatedStyle(() => ({transform: [{rotate: `${arrowRotation.get()}deg`}]})); |
| 94 | + |
| 95 | + return ( |
| 96 | + <View> |
| 97 | + <PressableWithoutFeedback |
| 98 | + onPress={toggleSection} |
| 99 | + style={[styles.flexRow, styles.p2, styles.gap2, styles.alignItemsCenter, styles.br2]} |
| 100 | + role={CONST.ROLE.BUTTON} |
| 101 | + accessibilityLabel={title} |
| 102 | + sentryLabel={CONST.SENTRY_LABEL.ACCORDION_SECTION.TOGGLE} |
| 103 | + hoverStyle={styles.hoveredComponentBG} |
| 104 | + > |
| 105 | + <Text |
| 106 | + style={[styles.flex1, styles.mutedNormalTextLabel]} |
| 107 | + accessibilityRole={CONST.ROLE.HEADER} |
| 108 | + > |
| 109 | + {title} |
| 110 | + </Text> |
| 111 | + {!!badgeText && ( |
| 112 | + <AnimatedBadge |
| 113 | + text={badgeText} |
| 114 | + isExpanded={isAccordionExpanded} |
| 115 | + /> |
| 116 | + )} |
| 117 | + <Animated.View style={[arrowAnimatedStyle]}> |
| 118 | + <Icon |
| 119 | + fill={theme.icon} |
| 120 | + src={icons.UpArrow} |
| 121 | + width={variables.iconSizeSmall} |
| 122 | + height={variables.iconSizeSmall} |
| 123 | + /> |
| 124 | + </Animated.View> |
| 125 | + </PressableWithoutFeedback> |
| 126 | + <Accordion |
| 127 | + isExpanded={isAccordionExpanded} |
| 128 | + isToggleTriggered={shouldAnimateAccordionSection} |
| 129 | + > |
| 130 | + {children} |
| 131 | + </Accordion> |
| 132 | + </View> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +export default SearchTypeMenuAccordion; |
0 commit comments