-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLoopScrollTabView.js
68 lines (60 loc) · 1.5 KB
/
LoopScrollTabView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import ScrollableTabView, { DefaultTabBar } from 'react-native-scrollable-tab-view';
import last from 'lodash/last';
import first from 'lodash/first';
import LoopTabBar from './LoopTabBar';
export default class LoopScrollTabView extends React.Component {
constructor(props) {
super(props);
this.pages = this.props.children.length + 2;
}
state = {
currentPage: 1,
};
onChangeTab = ({ i, ref, from }) => {
if (i === this.pages - 1) {
i = 1;
} else if (i === 0) {
i = this.pages - 2;
}
this.setState({
currentPage: i,
});
if (this.props.onChangeTab) {
this.props.onChangeTab({ i, ref, from });
}
};
render() {
const {
children,
} = this.props;
if (!children.length) {
return null;
}
const firstOne = first(children);
const lastOne = last(children);
const firstOneCopy = React.cloneElement(firstOne, {
key: 'first',
});
const lastOneCopy = React.cloneElement(lastOne, {
key: 'last',
});
const newChildren = [firstOneCopy, ...children, lastOneCopy];
return (
<ScrollableTabView
initialPage={1}
onChangeTab={this.onChangeTab}
page={this.state.currentPage}
prerenderingSiblingsNumber={1}
renderTabBar={() => (
<LoopTabBar>
{this.props.renderTabBar()}
</LoopTabBar>)
}
scrollWithoutAnimation
>
{newChildren}
</ScrollableTabView>
);
}
}