Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Slider component #190

Merged
merged 8 commits into from
Jun 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: eliminate race condition in Slider
  • Loading branch information
jhoward-eightfold committed Jun 22, 2022
commit c418d6cdd805d8950bf86513f8fa59d7069926c5
21 changes: 12 additions & 9 deletions src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useLayoutEffect, useRef, useState } from 'react';
import React, { FC, useEffect, useLayoutEffect, useRef, useState } from 'react';

import { mergeClasses, visuallyHidden } from '../../shared/utilities';
import { SliderMarker, SliderProps } from './Slider.types';
Expand Down Expand Up @@ -72,14 +72,6 @@ export const Slider: FC<SliderProps> = ({
return idTokens.join('-');
};

const handleChange = (newVal: number, index: number) => {
const newValues = [...values];
newValues.splice(index, 1, newVal);
newValues.sort(asc);
setValues(newValues);
onChange?.(isRange ? [...newValues] : newValues[0]);
};

const isMarkerActive = (markerValue: number): boolean => {
const markerPct = valueToPercent(markerValue, min, max);
return isRange
Expand All @@ -96,6 +88,17 @@ export const Slider: FC<SliderProps> = ({
);
};

const handleChange = (newVal: number, index: number) => {
const newValues = [...values];
newValues.splice(index, 1, newVal);
newValues.sort(asc);
setValues(newValues);
};

useEffect(() => {
onChange?.(isRange ? [...values] : values[0]);
}, [values]);

// Set width of the range to decrease from the left side
useLayoutEffect(() => {
const inputWidth = railRef.current?.offsetWidth || 0;
Expand Down