add scroll to top if same filter is selected

This commit is contained in:
feruz 2020-01-27 07:02:40 +02:00
parent e7ee917132
commit 7f3ad510c7

View File

@ -1,5 +1,5 @@
/* eslint-disable react/jsx-wrap-multilines */ /* eslint-disable react/jsx-wrap-multilines */
import React, { useState, useEffect, useCallback } from 'react'; import React, { useState, useEffect, useCallback, useRef } from 'react';
import { FlatList, View, ActivityIndicator, RefreshControl } from 'react-native'; import { FlatList, View, ActivityIndicator, RefreshControl } from 'react-native';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { withNavigation } from 'react-navigation'; import { withNavigation } from 'react-navigation';
@ -53,6 +53,7 @@ const PostsView = ({
filterOptionsValue && filterOptionsValue[selectedFilterIndex], filterOptionsValue && filterOptionsValue[selectedFilterIndex],
); );
const intl = useIntl(); const intl = useIntl();
const postsList = useRef(null);
useEffect(() => { useEffect(() => {
if (isConnected) { if (isConnected) {
@ -114,14 +115,19 @@ const PostsView = ({
]); ]);
const _handleOnDropdownSelect = async index => { const _handleOnDropdownSelect = async index => {
if (filterOptions && filterOptions.length > 0) { if (index === selectedFilterIndex) {
await setSelectedFilterValue(filterOptionsValue[index]); _scrollTop();
} else {
if (filterOptions && filterOptions.length > 0) {
await setSelectedFilterValue(filterOptionsValue[index]);
}
setSelectedFilterIndex(index);
setPosts([]);
setStartPermlink('');
setStartAuthor('');
setIsNoPost(false);
} }
setSelectedFilterIndex(index);
setPosts([]);
setStartPermlink('');
setStartAuthor('');
setIsNoPost(false);
}; };
const _getPromotePosts = useCallback(async () => { const _getPromotePosts = useCallback(async () => {
@ -315,6 +321,10 @@ const PostsView = ({
} }
}; };
const _scrollTop = () => {
postsList.current.scrollToOffset({ x: 0, y: 0, animated: true });
};
return ( return (
<ThemeContainer> <ThemeContainer>
{({ isDarkTheme }) => ( {({ isDarkTheme }) => (
@ -335,6 +345,7 @@ const PostsView = ({
)} )}
<FlatList <FlatList
ref={postsList}
data={posts} data={posts}
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
renderItem={({ item, index }) => { renderItem={({ item, index }) => {