Skip to content

Commit

Permalink
feat(slider): enable change of slider value though a prop (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaraTschamon authored Sep 11, 2024
1 parent 37850e1 commit d126c1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/components/Slider/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ WithCustomMinAndMax.args = {
export const WithLowerAndUpperBound = Template.bind({});
WithLowerAndUpperBound.args = {
variant: 'boundary',
value: 45,
min: 10,
max: 90,
lowerBoundText: 'Lower bound',
Expand Down
10 changes: 10 additions & 0 deletions src/components/Slider/Slider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,15 @@ describe('The Slider component', () => {

expect(screen.getByRole('tooltip')).toBeInTheDocument();
});

it('updates the slider value when the external value prop changes', () => {
const { rerender } = render(<Slider {...defaultProps} value={30} onChange={onChangeSpy} />);

expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow', '30');

rerender(<Slider {...defaultProps} value={70} onChange={onChangeSpy} />);

expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow', '70');
});
});
});
25 changes: 18 additions & 7 deletions src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StyleProps, useMediaQuery } from '@chakra-ui/react';
import React, { useMemo, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import {
Slider,
SliderTrack,
Expand All @@ -15,6 +15,7 @@ import CustomSliderThumb from './CustomSliderThumb';
export interface SliderProps extends StyleProps {
variant?: 'default' | 'boundary';
defaultValue?: number;
value?: number;
ariaLabel: string;
min?: number;
max?: number;
Expand All @@ -27,6 +28,7 @@ export interface SliderProps extends StyleProps {
export const BoemlySlider: React.FC<SliderProps> = ({
variant = 'default',
defaultValue,
value,
ariaLabel,
min = 0,
max = 100,
Expand All @@ -39,16 +41,25 @@ export const BoemlySlider: React.FC<SliderProps> = ({
const [isMobile] = useMediaQuery(BREAKPOINT_MD_QUERY);

const initialValue = useMemo(
() => defaultValue ?? min + (max - min) / 2,
[defaultValue, min, max]
() => value ?? defaultValue ?? min + (max - min) / 2,
[defaultValue, min, max, value]
);

const [sliderValue, setSliderValue] = useState(initialValue);
const [inputValue, setInputValue] = useState(initialValue.toString());

const sliderOnChange = (value: number) => {
setSliderValue(value);
setInputValue(value.toString());
onChange(value);
// Update internal sliderValue when the external value prop changes
useEffect(() => {
if (value !== undefined) {
setSliderValue(value);
setInputValue(value.toString());
}
}, [value]);

const sliderOnChange = (newValue: number) => {
setSliderValue(newValue);
setInputValue(newValue.toString());
onChange(newValue);
};

const inputOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down

0 comments on commit d126c1a

Please sign in to comment.