summaryrefslogtreecommitdiff
path: root/frontend/app/components/walkthrough.js
blob: 7fdb247e123c18e6b8058845c56b68512472bdcd (plain)
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
import React from 'react';
import {
  View,
  FlatList,
  Dimensions,
  StyleSheet,
} from 'react-native';
import PropTypes from 'prop-types';

export class Walkthrough extends React.Component {
  static propTypes = {
    children: PropTypes.arrayOf(PropTypes.element).isRequired,
    onChanged: PropTypes.func,
  };
  static defaultProps = {
    onChanged: (() => null),
  };

  constructor(props) {
    super(props);
    this.itemWidth = Dimensions.get('window').width;
  }

  extractItemKey = (item) => `${this.props.children.indexOf(item)}`;

  onScrollEnd = (e) => {
    const { contentOffset } = e.nativeEvent;
    const viewSize = e.nativeEvent.layoutMeasurement;
    const pageNum = Math.floor(contentOffset.x / viewSize.width);
    this.props.onChanged(pageNum);
  };

  renderItem = ({ item }) => (
    <View style={[styles.item, { width: this.itemWidth }]}>
      {item}
    </View>
  );

  render = () => (
    <FlatList
      style={styles.list}
      data={this.props.children}
      onMomentumScrollEnd={this.onScrollEnd}
      keyExtractor={this.extractItemKey}
      pagingEnabled
      horizontal
      renderSeparator={() => null}
      showsHorizontalScrollIndicator={false}
      showsVerticalScrollIndicator={false}
      directionalLockEnabled
      renderItem={this.renderItem}
    />
  );
}

const styles = StyleSheet.create({
  list: {
    flex: 1,
  },
  item: {
    flex: 1,
  },
});