[NEW] Scroll down floating button (#735)

This commit is contained in:
pranavpandey1998official 2019-03-26 02:24:40 +05:30 committed by Diego Mello
parent e9183b523b
commit 5771757fa8
1 changed files with 64 additions and 21 deletions

View File

@ -1,7 +1,7 @@
import { ListView as OldList } from 'realm/react-native'; import { ListView as OldList } from 'realm/react-native';
import React from 'react'; import React from 'react';
import { import {
ScrollView, ListView as OldList2, ImageBackground, ActivityIndicator TouchableOpacity, ScrollView, ListView as OldList2, ImageBackground, ActivityIndicator
} from 'react-native'; } from 'react-native';
import moment from 'moment'; import moment from 'moment';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -14,6 +14,8 @@ import scrollPersistTaps from '../../utils/scrollPersistTaps';
import debounce from '../../utils/debounce'; import debounce from '../../utils/debounce';
import RocketChat from '../../lib/rocketchat'; import RocketChat from '../../lib/rocketchat';
import log from '../../utils/log'; import log from '../../utils/log';
import { CustomIcon } from '../../lib/Icons';
import { isIOS, isNotch } from '../../utils/deviceInfo';
const DEFAULT_SCROLL_CALLBACK_THROTTLE = 100; const DEFAULT_SCROLL_CALLBACK_THROTTLE = 100;
@ -47,7 +49,8 @@ export class List extends React.Component {
this.state = { this.state = {
loading: true, loading: true,
loadingMore: false, loadingMore: false,
end: false end: false,
showScollToBottomButton: false
}; };
this.dataSource = ds.cloneWithRows(this.data); this.dataSource = ds.cloneWithRows(this.data);
} }
@ -58,8 +61,8 @@ export class List extends React.Component {
} }
shouldComponentUpdate(nextProps, nextState) { shouldComponentUpdate(nextProps, nextState) {
const { loadingMore, loading, end } = this.state; const { loadingMore, loading, end, showScollToBottomButton } = this.state;
return end !== nextState.end || loadingMore !== nextState.loadingMore || loading !== nextState.loading; return end !== nextState.end || loadingMore !== nextState.loadingMore || loading !== nextState.loading || showScollToBottomButton !== nextState.showScollToBottomButton;
} }
componentWillUnmount() { componentWillUnmount() {
@ -91,6 +94,18 @@ export class List extends React.Component {
} }
} }
scrollToBottom = () => {
this.listView.scrollTo({ x: 0, y: 0, animated: true });
}
handleScroll= (event) => {
if (event.nativeEvent.contentOffset.y > 0) {
this.setState({ showScollToBottomButton: true });
} else {
this.setState({ showScollToBottomButton: false });
}
}
renderFooter = () => { renderFooter = () => {
const { loadingMore, loading } = this.state; const { loadingMore, loading } = this.state;
if (loadingMore || loading) { if (loadingMore || loading) {
@ -99,12 +114,33 @@ export class List extends React.Component {
return null; return null;
} }
getScrollButtonStyle = () => {
let right = 30;
if (isIOS) {
right = isNotch ? 45 : 30;
}
return ({
position: 'absolute',
width: 42,
height: 42,
alignItems: 'center',
justifyContent: 'center',
right,
bottom: 70,
backgroundColor: '#EAF2FE',
borderRadius: 20
})
}
render() { render() {
const { renderRow } = this.props; const { renderRow } = this.props;
const { showScollToBottomButton } = this.state;
const scrollButtonStyle = this.getScrollButtonStyle();
return ( return (
<React.Fragment>
<ListView <ListView
enableEmptySections enableEmptySections
ref={ref => this.listView = ref}
style={styles.list} style={styles.list}
data={this.data} data={this.data}
keyExtractor={item => item._id} keyExtractor={item => item._id}
@ -115,16 +151,23 @@ export class List extends React.Component {
renderRow={(item, previousItem) => renderRow(item, previousItem)} renderRow={(item, previousItem) => renderRow(item, previousItem)}
initialListSize={1} initialListSize={1}
pageSize={20} pageSize={20}
onScroll={this.handleScroll}
testID='room-view-messages' testID='room-view-messages'
{...scrollPersistTaps} {...scrollPersistTaps}
/> />
{showScollToBottomButton ? (
<TouchableOpacity activeOpacity={0.5} style={scrollButtonStyle} onPress={this.scrollToBottom}>
<CustomIcon name='arrow-down' color='white' size={30} />
</TouchableOpacity>
) : null}
</React.Fragment>
); );
} }
} }
@connect(state => ({ @connect(state => ({
lastOpen: state.room.lastOpen lastOpen: state.room.lastOpen
})) }), null, null, { forwardRef: true })
export class ListView extends OldList2 { export class ListView extends OldList2 {
constructor(props) { constructor(props) {
super(props); super(props);