summaryrefslogtreecommitdiff
path: root/frontend/app/components/progressBar.js
blob: dd3a3a12e12a5c3a367c8c18517d03b0049479d8 (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 {
  StyleSheet,
  View,
  Animated,
  Easing,
  ViewPropTypes,
} from 'react-native';
import PropTypes from 'prop-types';
import { RkTheme } from 'react-native-ui-kitten';

export class ProgressBar extends React.Component {
  static propTypes = {
    width: PropTypes.number.isRequired,
    progress: PropTypes.number,
    color: PropTypes.string,
    style: ViewPropTypes.style,
  };
  static defaultProps = {
    progress: 0,
    color: RkTheme.current.colors.accent,
    style: {},
  };

  state = {
    progress: new Animated.Value(0),
  };

  componentDidUpdate(prevProps) {
    if (this.props.progress >= 0 && this.props.progress !== prevProps.progress) {
      this.animate(this.props.progress);
    }
  }

  animate = (endValue) => {
    Animated.timing(this.state.progress, {
      easing: Easing.inOut(Easing.ease),
      duration: 500,
      toValue: endValue,
    }).start();
  };

  render() {
    const width = this.state.progress.interpolate({
      inputRange: [0, 1],
      outputRange: [0, this.props.width],
    });
    return (
      <View style={[styles.container, this.props.style, { width: this.props.width }]}>
        <Animated.View style={[styles.value, { width }, { backgroundColor: this.props.color }]} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    height: 3,
  },
  value: {
    height: 3,
  },
});