forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 1.45 KB
/
index.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
import React from 'react';
import { classes } from 'common/util';
import styles from './ProgressBar.module.scss';
class ProgressBar extends React.Component {
constructor(props) {
super(props);
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
}
handleMouseDown(e) {
this.target = e.target;
this.handleMouseMove(e);
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
}
handleMouseMove(e) {
const { left } = this.target.getBoundingClientRect();
const { offsetWidth } = this.target;
const { onChangeProgress } = this.props;
const progress = (e.clientX - left) / offsetWidth;
if (onChangeProgress) onChangeProgress(progress);
}
handleMouseUp(e) {
document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp);
}
render() {
const { className, total, current } = this.props;
return (
<div className={classes(styles.progress_bar, className)} onMouseDown={this.handleMouseDown}>
<div className={styles.active} style={{ width: `${current / total * 100}%` }} />
<div className={styles.label}>
<span className={styles.current}>{current}</span> / {total}
</div>
</div>
);
}
}
export default ProgressBar;