Skip to content

Commit

Permalink
Add value prop to slider
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMHasperhoven committed Nov 27, 2024
1 parent 4ca4671 commit 181375b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"build": "vite build",
"build-storybook": "storybook build",
"dev:pack": "VITE_WATCH=true vite build --watch",
"dev:pack": "vite build --watch",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"lint:strict": "tsc --noEmit && eslint src --max-warnings 0",
Expand Down
14 changes: 11 additions & 3 deletions packages/ui/src/Slider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { Text } from '../Text';
import * as RadixSlider from '@radix-ui/react-slider';
import { ThemeColor, getThemeColorClass } from '../utils/color';
Expand All @@ -8,6 +8,7 @@ interface SliderProps {
min?: number;
max?: number;
step?: number;
value?: number;
defaultValue?: number;
onChange?: (value: number) => void;
leftLabel?: string;
Expand All @@ -26,6 +27,7 @@ export const Slider: React.FC<SliderProps> = ({
min = 0,
max = 100,
step = 1,
value: valueProp = 0,
defaultValue = 0,
onChange,
leftLabel,
Expand All @@ -37,13 +39,19 @@ export const Slider: React.FC<SliderProps> = ({
valueDetails,
disabled = false,
}) => {
const [value, setValue] = useState(defaultValue);
const [value, setValue] = useState(valueProp || defaultValue);
console.log('TCL: value', value);
const handleValueChange = (newValue: number[]) => {
const updatedValue = newValue[0] ?? defaultValue;
setValue(updatedValue);
onChange?.(updatedValue);
};

useEffect(() => {
console.log('TCL: valueProp', valueProp);
setValue(valueProp);
}, [valueProp]);

const totalSteps = (max - min) / step;

return (
Expand All @@ -63,7 +71,7 @@ export const Slider: React.FC<SliderProps> = ({
min={min}
max={max}
step={step}
defaultValue={[defaultValue]}
value={[value]}
onValueChange={handleValueChange}
disabled={disabled}
>
Expand Down

0 comments on commit 181375b

Please sign in to comment.