Skip to content

Commit

Permalink
fix(Slider): export function for preparing initial value (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arucard89 authored Jun 11, 2024
1 parent 7d26272 commit 434949e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {block} from '../utils/cn';
import {BaseSlider} from './BaseSlider/BaseSlider';
import {SliderTooltip} from './SliderTooltip/SliderTooltip';
import type {RcSliderValueType, SliderProps, SliderValue, StateModifiers} from './types';
import {getInnerState} from './utils';
import {prepareSliderInnerState} from './utils';

import './Slider.scss';

Expand Down Expand Up @@ -69,7 +69,7 @@ export const Slider = React.forwardRef(function Slider(
};
}, [handleUpdate, handleUpdateComplete]);

const innerState = getInnerState({
const innerState = prepareSliderInnerState({
availableValues,
defaultValue,
marksCount,
Expand Down
1 change: 1 addition & 0 deletions src/components/Slider/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Slider';
export type {SliderSize, SliderValue, SliderProps, BaseSliderRefType} from './types';
export {prepareSliderInnerState} from './utils';
42 changes: 24 additions & 18 deletions src/components/Slider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ function createMarks(points: number[]): RcSliderProps['marks'] {
return marks;
}

export function getInnerState({
/**
* Calculates the basic properties of the Slider component depending on the passed parameters
* @returns {SliderInnerState} Properties to pass to the Slider
*/
export function prepareSliderInnerState({
max = 100,
min = 0,
availableValues,
Expand Down Expand Up @@ -134,24 +138,26 @@ export function getInnerState({
state.marks = createMarks(calculateInfoPoints({count: marksCount, max, min}));
}

if (value && Array.isArray(value)) {
state.range = true;
state.value = prepareArrayValue({min: state.min, max: state.max, value});
} else if (defaultValue && Array.isArray(defaultValue)) {
state.range = true;
state.defaultValue = prepareArrayValue({
min: state.min,
max: state.max,
value: defaultValue,
});
} else if (value) {
state.value = prepareSingleValue({min: state.min, max: state.max, value});
if (value === undefined) {
const isArray = Array.isArray(defaultValue);
state.range = isArray;
state.defaultValue = isArray
? prepareArrayValue({
min: state.min,
max: state.max,
value: defaultValue,
})
: prepareSingleValue({
min: state.min,
max: state.max,
value: defaultValue,
});
} else {
state.defaultValue = prepareSingleValue({
min: state.min,
max: state.max,
value: defaultValue,
});
const isArray = Array.isArray(value);
state.range = isArray;
state.value = isArray
? prepareArrayValue({min: state.min, max: state.max, value})
: prepareSingleValue({min: state.min, max: state.max, value});
}

return state;
Expand Down

0 comments on commit 434949e

Please sign in to comment.