Skip to content

Commit

Permalink
Merge pull request #48898 from bernhardoj/fix/47640-keep-status-scrol…
Browse files Browse the repository at this point in the history
…l-position

Scroll into view the search status button
  • Loading branch information
luacmartins authored Sep 11, 2024
2 parents 70e88a6 + b6878e3 commit ecb629d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/components/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useIsFocused} from '@react-navigation/native';
import type {ForwardedRef} from 'react';
import React, {useCallback, useMemo, useState} from 'react';
import type {GestureResponderEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {GestureResponderEvent, LayoutChangeEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import {ActivityIndicator, View} from 'react-native';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
Expand Down Expand Up @@ -53,6 +53,9 @@ type ButtonProps = Partial<ChildrenProps> & {
/** Indicates whether the button should be disabled */
isDisabled?: boolean;

/** Invoked on mount and layout changes */
onLayout?: (event: LayoutChangeEvent) => void;

/** A function that is called when the button is clicked on */
onPress?: (event?: GestureResponderEvent | KeyboardEvent) => void;

Expand Down Expand Up @@ -192,6 +195,7 @@ function Button(
isLoading = false,
isDisabled = false,

onLayout = () => {},
onPress = () => {},
onLongPress = () => {},
onPressIn = () => {},
Expand Down Expand Up @@ -325,6 +329,7 @@ function Button(
)}
<PressableWithFeedback
ref={ref}
onLayout={onLayout}
onPress={(event) => {
if (event?.type === 'click') {
const currentTarget = event?.currentTarget as HTMLElement;
Expand Down
14 changes: 13 additions & 1 deletion src/components/Search/SearchStatusBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import React, {useRef} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {ScrollView as RNScrollView} from 'react-native';
import Button from '@components/Button';
import * as Expensicons from '@components/Icon/Expensicons';
import ScrollView from '@components/ScrollView';
Expand Down Expand Up @@ -150,9 +152,12 @@ function SearchStatusBar({type, status, resetOffset}: SearchStatusBarProps) {
const theme = useTheme();
const {translate} = useLocalize();
const options = getOptions(type);
const scrollRef = useRef<RNScrollView>(null);
const isScrolledRef = useRef(false);

return (
<ScrollView
ref={scrollRef}
style={[styles.flexRow, styles.mb5, styles.overflowScroll, styles.flexGrow0]}
horizontal
showsHorizontalScrollIndicator={false}
Expand All @@ -169,6 +174,13 @@ function SearchStatusBar({type, status, resetOffset}: SearchStatusBarProps) {
return (
<Button
key={item.key}
onLayout={(e) => {
if (!isActive || isScrolledRef.current || !('left' in e.nativeEvent.layout)) {
return;
}
isScrolledRef.current = true;
scrollRef.current?.scrollTo({x: (e.nativeEvent.layout.left as number) - styles.pl5.paddingLeft});
}}
text={translate(item.text)}
onPress={onPress}
icon={item.icon}
Expand Down
9 changes: 7 additions & 2 deletions src/pages/Search/SearchTypeMenuNarrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import type {SearchQueryJSON} from '@components/Search/types';
import Text from '@components/Text';
import useSingleExecution from '@hooks/useSingleExecution';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as SearchActions from '@libs/actions/Search';
import Navigation from '@libs/Navigation/Navigation';
import variables from '@styles/variables';
import * as Expensicons from '@src/components/Icon/Expensicons';
import * as SearchUtils from '@src/libs/SearchUtils';
import ROUTES from '@src/ROUTES';
Expand All @@ -27,6 +29,7 @@ type SearchTypeMenuNarrowProps = {
function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title}: SearchTypeMenuNarrowProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {singleExecution} = useSingleExecution();
const {windowHeight} = useWindowDimensions();

Expand Down Expand Up @@ -86,21 +89,23 @@ function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title}
onPress={openMenu}
>
{({hovered}) => (
<Animated.View style={[styles.tabSelectorButton, styles.tabBackground(hovered, true, theme.border), styles.w100]}>
<Animated.View style={[styles.tabSelectorButton, styles.tabBackground(hovered, true, theme.border), styles.w100, StyleUtils.getHeight(variables.componentSizeNormal)]}>
<View style={[styles.flexRow, styles.gap2, styles.alignItemsCenter, titleViewStyles]}>
<Icon
src={menuIcon}
fill={theme.icon}
small
/>
<Text
numberOfLines={1}
style={[styles.textStrong, styles.flexShrink1]}
style={[styles.textStrong, styles.flexShrink1, styles.fontSizeLabel]}
>
{menuTitle}
</Text>
<Icon
src={Expensicons.DownArrow}
fill={theme.icon}
small
/>
</View>
</Animated.View>
Expand Down

0 comments on commit ecb629d

Please sign in to comment.