-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slider.tsx
48 lines (39 loc) · 1.11 KB
/
Slider.tsx
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, { useCallback } from "react";
interface Props
extends Omit<React.HTMLProps<HTMLInputElement>, "max" | "min" | "onChange" | "step" | "type" | "value"> {
onChange: (value: number) => void;
type: SliderType;
value: number;
}
export enum SliderType {
Linear = "Linear",
Log = "Log",
}
function lin2log(value: number): number {
return (Math.pow(10, value) - 1) / 9;
}
function log2lin(value: number): number {
return Math.log10(1 + value * 9);
}
function Slider({ onChange, type, value, ...props }: Props) {
const min = 0;
const max = 1;
let step = 0.001;
let valueShown = value;
if (type === SliderType.Log) {
step = 9 / ((max - min) / step);
valueShown = lin2log(valueShown);
}
const change = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
let newValue = +e.target.value;
if (type === SliderType.Log) {
newValue = log2lin(newValue);
}
onChange(newValue);
},
[onChange, type]
);
return <input {...props} type="range" max={max} min={min} step={step} onChange={change} value={valueShown} />;
}
export default Slider;