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

fix(Slider): export function for preparing initial value #1637

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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';
38 changes: 20 additions & 18 deletions src/components/Slider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function createMarks(points: number[]): RcSliderProps['marks'] {
return marks;
}

export function getInnerState({
export function prepareSliderInnerState({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide some jsdoc here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, forgot to do it, thanks
143584f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why user might needed this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why user might needed this function?

I need it to use in RangeSliderPicker component to get inner state. And I don't want to duplicate the code.
Also it could be helpful for other users in the development of components based on the slider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amje, I don't know is it really "fix" but not "chore"?
Is it important enough to get into the changelog?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's important to note this change in the changelog because you have changed the implementation

max = 100,
min = 0,
availableValues,
Expand Down Expand Up @@ -134,24 +134,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
Loading