Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#134] fix : Component To PureComponent #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 55 additions & 59 deletions client/src/components/Slider.jsx
Original file line number Diff line number Diff line change
@@ -1,97 +1,93 @@
/* global document */
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import offsetLeft from '../utils/DomUtils';
import PropTypes from "prop-types"
import React, { Component } from "react"
import offsetLeft from "../utils/DomUtils"

const defaultProps = {
className: '',
};

const propTypes = {
className: PropTypes.string,
max: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
value: PropTypes.number.isRequired,
};

const prevent = (e) => {
e.preventDefault();
e.stopPropagation();
};
const prevent = e => {
e.preventDefault()
e.stopPropagation()
}

class Slider extends Component {
static propTypes = {
className: PropTypes.string,
max: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
value: PropTypes.number.isRequired
}
static defaultProps = {
className: ""
}
constructor() {
super();
this.onClick = this.onClick.bind(this);
this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
this.domNode = null;
super()
this.onClick = this.onClick.bind(this)
this.onMouseDown = this.onMouseDown.bind(this)
this.onMouseMove = this.onMouseMove.bind(this)
this.onMouseUp = this.onMouseUp.bind(this)
this.domNode = null
}

componentWillUnmount() {
document.removeEventListener('mousemove', this.onMouseMove);
document.removeEventListener('mouseup', this.onMouseUp);
document.removeEventListener("mousemove", this.onMouseMove)
document.removeEventListener("mouseup", this.onMouseUp)
}

onClick(e) {
const { max, onChange } = this.props;
const percent = (e.clientX - offsetLeft(e.currentTarget)) / e.currentTarget.offsetWidth;
onChange(percent * max);
const { max, onChange } = this.props
const percent =
(e.clientX - offsetLeft(e.currentTarget)) / e.currentTarget.offsetWidth
onChange(percent * max)
}

onMouseDown() {
document.addEventListener('mousemove', this.onMouseMove);
document.addEventListener('mouseup', this.onMouseUp);
document.addEventListener("mousemove", this.onMouseMove)
document.addEventListener("mouseup", this.onMouseUp)
}

onMouseMove(e) {
const { domNode, props } = this;
const { max, onChange } = props;
const { domNode, props } = this
const { max, onChange } = props

const diff = e.clientX - offsetLeft(domNode);
const percent = Math.min(Math.max(diff / domNode.offsetWidth, 0), 1);
onChange(percent * max);
const diff = e.clientX - offsetLeft(domNode)
const percent = Math.min(Math.max(diff / domNode.offsetWidth, 0), 1)
onChange(percent * max)
}

onMouseUp() {
document.removeEventListener('mousemove', this.onMouseMove);
document.removeEventListener('mouseup', this.onMouseUp);
document.removeEventListener("mousemove", this.onMouseMove)
document.removeEventListener("mouseup", this.onMouseUp)
}

render() {
const { className, max, value } = this.props;
const width = `${(value / max) * 100}%`;
const { className, max, value } = this.props
const width = `${(value / max) * 100}%`

return (
<div
className={`slider ${className}`}
onClick={this.onClick}
ref={(node) => { this.domNode = node; }}
ref={node => {
this.domNode = node
}}
role="button"
tabIndex="0"
>
<div className="slider__bar">
{max > 0
? (
<div className="slider__bar__fill" style={{ width }}>
<div
className="slider__handle"
onClick={prevent}
onMouseDown={this.onMouseDown}
role="button"
tabIndex="0"
/>
</div>
) : null
}
{max > 0 ? (
<div className="slider__bar__fill" style={{ width }}>
<div
className="slider__handle"
onClick={prevent}
onMouseDown={this.onMouseDown}
role="button"
tabIndex="0"
/>
</div>
) : null}
</div>
</div>
);
)
}
}

Slider.defaultProps = defaultProps;
Slider.propTypes = propTypes;

export default Slider;
export default Slider