mirror of
https://github.com/jgthms/bulma.git
synced 2024-11-28 12:24:23 +00:00
Improve Slider
This commit is contained in:
parent
4a165737df
commit
681592e689
@ -12,15 +12,27 @@ const RANGES = {
|
|||||||
any: [0, 100, 1],
|
any: [0, 100, 1],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const xToValue = (x, width, min, max) => {
|
||||||
|
const decimal = x / width;
|
||||||
|
const newValue = decimal * (max - min);
|
||||||
|
return Math.round(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
const valueToX = (value, width, min, max) => {
|
||||||
|
const decimal = value / (max - min);
|
||||||
|
const newValue = decimal * width;
|
||||||
|
return Math.round(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
function Slider({ id, kind, start, unit }) {
|
function Slider({ id, kind, start, unit }) {
|
||||||
|
const [min, max] = RANGES[kind];
|
||||||
|
|
||||||
const [value, setValue] = useState(start);
|
const [value, setValue] = useState(start);
|
||||||
const [isMoving, setMoving] = useState(false);
|
const [isMoving, setMoving] = useState(false);
|
||||||
const [x, setX] = useState(0);
|
const [x, setX] = useState(valueToX(start, 240, min, max));
|
||||||
const sliderRef = useRef(null);
|
const sliderRef = useRef(null);
|
||||||
const handleRef = useRef(null);
|
const handleRef = useRef(null);
|
||||||
|
|
||||||
const [min, max, step] = RANGES[kind];
|
|
||||||
|
|
||||||
const handleMouseDown = (event) => {
|
const handleMouseDown = (event) => {
|
||||||
setMoving(true);
|
setMoving(true);
|
||||||
const slider = sliderRef.current;
|
const slider = sliderRef.current;
|
||||||
@ -50,6 +62,13 @@ function Slider({ id, kind, start, unit }) {
|
|||||||
}
|
}
|
||||||
}, [id, start, unit, value]);
|
}, [id, start, unit, value]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const slider = sliderRef.current;
|
||||||
|
const sliderRect = slider.getBoundingClientRect();
|
||||||
|
const final = xToValue(x, sliderRect.width, min, max);
|
||||||
|
setValue(final);
|
||||||
|
}, [min, max, unit, x]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const docMouseMove = (event) => {
|
const docMouseMove = (event) => {
|
||||||
if (!isMoving || !sliderRef.current || !handleRef.current) {
|
if (!isMoving || !sliderRef.current || !handleRef.current) {
|
||||||
|
Loading…
Reference in New Issue
Block a user