diff --git a/src/components/Slider/Slider.tsx b/src/components/Slider/Slider.tsx index 204fbe9dca..15311bdd46 100644 --- a/src/components/Slider/Slider.tsx +++ b/src/components/Slider/Slider.tsx @@ -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'; @@ -69,7 +69,7 @@ export const Slider = React.forwardRef(function Slider( }; }, [handleUpdate, handleUpdateComplete]); - const innerState = getInnerState({ + const innerState = prepareSliderInnerState({ availableValues, defaultValue, marksCount, diff --git a/src/components/Slider/index.ts b/src/components/Slider/index.ts index 05ecd5c22a..4551608dc1 100644 --- a/src/components/Slider/index.ts +++ b/src/components/Slider/index.ts @@ -1,2 +1,3 @@ export * from './Slider'; export type {SliderSize, SliderValue, SliderProps, BaseSliderRefType} from './types'; +export {prepareSliderInnerState} from './utils'; diff --git a/src/components/Slider/utils.ts b/src/components/Slider/utils.ts index 6dd0d4c42b..b21ca33bfe 100644 --- a/src/components/Slider/utils.ts +++ b/src/components/Slider/utils.ts @@ -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, @@ -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;