Skip to content

Commit

Permalink
🐛Slider: fix type, allow array val for non-range (#3076)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvernes authored Oct 9, 2023
1 parent b42752d commit 5dcf2ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
13 changes: 5 additions & 8 deletions packages/eds-core-react/src/components/Slider/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export const RangeSlider: StoryFn<SliderProps> = () => {
const [value, updateValue] = useState([30, 70])
const changeHandler = (
event: ChangeEvent<HTMLInputElement>,
value: number[] | number,
value: number[],
) => {
updateValue(value as number[])
updateValue(value)
}

return (
Expand All @@ -98,16 +98,13 @@ export const RangeSliderWithCommittedStep: StoryFn<SliderProps> = () => {
<Slider
aria-label="Range slider with a lot of steps"
value={value}
onChange={(
event: ChangeEvent<HTMLInputElement>,
value: number[] | number,
) => {
updateValue(value as number[])
onChange={(event, value) => {
updateValue(value)
}}
min={0}
max={500}
onChangeCommitted={(event, value) => {
updateValueCommited(value as number[])
updateValueCommited(value)
}}
/>
<Typography variant="caption">
Expand Down
20 changes: 10 additions & 10 deletions packages/eds-core-react/src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,11 @@ export type SliderProps = {
/** Components value, range of numbers */
value: number[] | number
/** Function to be called when value change */
onChange?: (
event: ChangeEvent<HTMLInputElement>,
newValue: number[] | number,
) => void
onChange?: (event: ChangeEvent<HTMLInputElement>, newValue: number[]) => void
/** Function to be called when value is committed by mouseup event */
onChangeCommitted?: (
event: MouseEvent | KeyboardEvent,
newValue: number[] | number,
newValue: number[],
) => void
/** Function for formatting the displayed value. E.g. formatting dates, or adding a unit suffix */
outputFunction?: (value: number) => string
Expand Down Expand Up @@ -234,8 +231,10 @@ export const Slider = forwardRef<HTMLDivElement, SliderProps>(function Slider(
},
ref,
) {
const isRangeSlider = Array.isArray(value)
const parsedValue: number[] = isRangeSlider ? value : [value]
const isNumber = !Array.isArray(value)
const isRangeSlider = !isNumber && value.length === 2

const parsedValue: number[] = isNumber ? [value] : value
const [initalValue, setInitalValue] = useState<number[]>(parsedValue)
const [sliderValue, setSliderValue] = useState<number[]>(parsedValue)
const [mousePressed, setMousePressed] = useState<boolean>(false)
Expand All @@ -247,9 +246,10 @@ export const Slider = forwardRef<HTMLDivElement, SliderProps>(function Slider(
setSliderValue(value)
}
} else {
if (value !== initalValue[0]) {
setInitalValue([value])
setSliderValue([value])
const numberValue = Number(value)
if (numberValue !== initalValue[0]) {
setInitalValue([numberValue])
setSliderValue([numberValue])
}
}
}, [value, initalValue, isRangeSlider])
Expand Down

0 comments on commit 5dcf2ad

Please sign in to comment.