redux-devtools/packages/devui/src/Slider/Slider.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-01-03 16:00:55 +03:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import createStyledComponent from '../utils/createStyledComponent';
import * as styles from './styles';
import { containerStyle } from './styles/common';
const SliderWrapper = createStyledComponent(styles);
const ContainerWithValue = createStyledComponent(containerStyle);
export default class Slider extends Component {
shouldComponentUpdate(nextProps) {
2019-01-10 21:51:14 +03:00
return (
nextProps.label !== this.props.label ||
2019-01-03 16:00:55 +03:00
nextProps.value !== this.props.value ||
nextProps.max !== this.props.max ||
nextProps.min !== this.props.min ||
nextProps.withValue !== this.props.withValue ||
2019-01-10 21:51:14 +03:00
nextProps.disabled !== this.props.disabled
);
2019-01-03 16:00:55 +03:00
}
onChange = (e) => {
2019-01-03 16:00:55 +03:00
this.props.onChange(parseFloat(e.target.value));
};
render() {
const { label, sublabel, withValue, theme, ...rest } = this.props;
const { value, max, min, disabled } = rest;
const absMax = max - min;
2019-01-10 21:51:14 +03:00
const percent = ((value - min) / absMax) * 100;
2019-01-03 16:00:55 +03:00
const slider = <input {...rest} onChange={this.onChange} type="range" />;
return (
<SliderWrapper
percent={percent}
disabled={disabled || absMax === 0}
withLabel={!!label}
theme={theme}
>
2019-01-10 21:51:14 +03:00
{label && (
<label>
{label} {sublabel && <span>{sublabel}</span>}
</label>
)}
{!withValue ? (
slider
) : (
2019-01-03 16:00:55 +03:00
<ContainerWithValue theme={theme}>
{slider}
<div>{value}</div>
</ContainerWithValue>
2019-01-10 21:51:14 +03:00
)}
2019-01-03 16:00:55 +03:00
</SliderWrapper>
);
}
}
Slider.propTypes = {
value: PropTypes.number,
min: PropTypes.number,
max: PropTypes.number,
label: PropTypes.string,
sublabel: PropTypes.string,
withValue: PropTypes.bool,
disabled: PropTypes.bool,
onChange: PropTypes.func,
theme: PropTypes.object,
2019-01-03 16:00:55 +03:00
};
Slider.defaultProps = { value: 0, min: 0, max: 100 };