| 7 | import { cn } from '@/lib/utils'; |
| 8 | |
| 9 | const SliderWidget = ({ |
| 10 | label, |
| 11 | value = 50, |
| 12 | min = 0, |
| 13 | max = 100, |
| 14 | step = 1, |
| 15 | onChange, |
| 16 | id, |
| 17 | className, |
| 18 | }) => { |
| 19 | const [localValue, setLocalValue] = React.useState(value); |
| 20 | |
| 21 | const debouncedOnChange = useDebouncedCallback((value) => { |
| 22 | onChange?.(value); |
| 23 | }, 100); |
| 24 | |
| 25 | const handleChange = (e) => { |
| 26 | const newValue = parseFloat(e.target.value); |
| 27 | setLocalValue(newValue); |
| 28 | debouncedOnChange(newValue); |
| 29 | }; |
| 30 | |
| 31 | React.useEffect(() => { |
| 32 | setLocalValue(value); |
| 33 | }, [value]); |
| 34 | |
| 35 | return ( |
| 36 | <div id={id} className={cn('grid gap-2 w-full max-w-sm mb-4', className)}> |
| 37 | <div className="flex items-center justify-between"> |
| 38 | <Label |
| 39 | htmlFor={id} |
| 40 | className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" |
| 41 | > |
| 42 | {label} |
| 43 | </Label> |
| 44 | <span className="text-sm text-muted-foreground">{localValue}</span> |
| 45 | </div> |
| 46 | <input |
| 47 | id={id} |
| 48 | type="range" |
| 49 | min={min} |
| 50 | max={max} |
| 51 | step={step} |
| 52 | value={localValue} |
| 53 | onChange={handleChange} |
| 54 | className="w-full h-2 appearance-none bg-secondary rounded-full cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:h-4 [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-primary" |
| 55 | /> |
| 56 | </div> |
| 57 | ); |
| 58 | }; |
| 59 | |
| 60 | export default SliderWidget; |