redux-devtools/packages/redux-devtools-ui/src/Slider/Slider.tsx
Nathan Bierema bbb1a40395
Convert React packages to ESM (#1650)
* react-base16-styling

* Use inline react-base16-styling themes

* Fix

* Format

* Fix

* Fixes

* Transform more

* react-json-tree

* Update lock

* Remove unnecessary

* react-dock

* Move to dep

* Lock

* Fix

* Fix

* Create tame-eagles-relax.md
2024-04-07 21:04:45 +00:00

71 lines
2.0 KiB
TypeScript

import React, { Component } from 'react';
import type { Base16Theme } from 'react-base16-styling';
import createStyledComponent from '../utils/createStyledComponent';
import * as styles from './styles';
import { containerStyle } from './styles/common';
const SliderWrapper = createStyledComponent(styles);
const ContainerWithValue = createStyledComponent(containerStyle);
export interface SliderProps {
value: number;
min: number;
max: number;
label?: string;
sublabel?: string;
withValue?: boolean;
disabled?: boolean;
onChange: (value: number) => void;
theme?: Base16Theme;
}
export default class Slider extends Component<SliderProps> {
shouldComponentUpdate(nextProps: SliderProps) {
return (
nextProps.label !== this.props.label ||
nextProps.value !== this.props.value ||
nextProps.max !== this.props.max ||
nextProps.min !== this.props.min ||
nextProps.withValue !== this.props.withValue ||
nextProps.disabled !== this.props.disabled
);
}
onChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
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;
const percent = ((value - min) / absMax) * 100;
const slider = <input {...rest} onChange={this.onChange} type="range" />;
return (
<SliderWrapper
percent={percent}
disabled={disabled || absMax === 0}
withLabel={!!label}
theme={theme}
>
{label && (
<label>
{label} {sublabel && <span>{sublabel}</span>}
</label>
)}
{!withValue ? (
slider
) : (
<ContainerWithValue theme={theme}>
{slider}
<div>{value}</div>
</ContainerWithValue>
)}
</SliderWrapper>
);
}
static defaultProps = { value: 0, min: 0, max: 100 };
}