Dwinzo_dev/app/src/components/ui/inputs/InputRange.tsx

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-03-25 06:17:41 +00:00
import React, { useEffect, useState } from "react";
import * as CONSTANTS from "../../../types/world/worldConstants";
2025-03-25 06:17:41 +00:00
interface InputToggleProps {
label: string; // Represents the toggle state (on/off)
min?: number;
max?: number;
onClick?: () => void; // Function to handle toggle clicks
onChange?: (value: number) => void; // Function to handle toggle clicks
disabled?: boolean;
value?: number;
2025-04-01 03:28:56 +00:00
onPointerUp?: (value: number) => void;
2025-03-25 06:17:41 +00:00
}
const InputRange: React.FC<InputToggleProps> = ({
label,
onClick,
onChange,
min,
max,
2025-03-25 06:17:41 +00:00
disabled,
2025-04-01 03:28:56 +00:00
value,
onPointerUp,
2025-03-25 06:17:41 +00:00
}) => {
2025-04-01 03:28:56 +00:00
const [rangeValue, setRangeValue] = useState<number>(value ? value : 5);
2025-03-25 06:17:41 +00:00
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
const newValue = parseInt(e.target.value); // Parse the value to an integer
2025-03-25 06:17:41 +00:00
setRangeValue(newValue); // Update the local state
if (onChange) {
onChange(newValue); // Call the onChange function if it exists
}
}
useEffect(() => {
2025-04-01 03:28:56 +00:00
value && setRangeValue(value);
2025-03-25 06:17:41 +00:00
}, [value]);
2025-04-01 03:28:56 +00:00
function handlePointerUp(e: React.PointerEvent<HTMLInputElement>) {
const newValue = parseInt(e.currentTarget.value, 10); // Parse value correctly
if (onPointerUp) {
onPointerUp(newValue); // Call the callback function if it exists
}
}
function handlekey(e: React.KeyboardEvent<HTMLInputElement>) {
const newValue = parseInt(e.currentTarget.value, 10); // Parse value correctly
if (onPointerUp) {
onPointerUp(newValue); // Call the callback function if it exists
}
}
2025-03-25 06:17:41 +00:00
return (
<div className="input-range-container">
<label
htmlFor={`range-input ${value}`}
className="label"
onClick={onClick}
>
{label}
</label>
<div className="input-container">
<input
id={`range-input ${value}`}
type="range"
min={min}
max={max}
onChange={handleChange}
disabled={disabled}
value={rangeValue}
2025-04-01 03:28:56 +00:00
onPointerUp={handlePointerUp}
2025-03-25 06:17:41 +00:00
/>
<input
type="number"
min={min}
className="input-value"
max={max}
value={rangeValue}
onChange={handleChange}
disabled={disabled}
2025-04-01 03:28:56 +00:00
onKeyUp={(e) => {
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
console.log("e.key: ", e.key);
handlekey(e);
}
}}
2025-03-25 06:17:41 +00:00
/>
</div>
</div>
);
};
export default InputRange;