Skip to content

Incubator Slider - fix step interpolation #2988

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

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Changes from all 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
29 changes: 20 additions & 9 deletions src/incubator/Slider/SliderPresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function countDecimals(value: number) {
if (Math.floor(value.valueOf()) === value.valueOf()) {
return 0;
}
return value.toString().split('.')[1].length || 0;
return value.toString().split('.')[1].length || 0;
}

export function getValueForOffset(offset: number, span: number, minimum = 0, maximum = 1, step = 0) {
Expand All @@ -39,26 +39,37 @@ function inRange(value: number, min: number, max: number) {

export function validateValues(props: SliderProps) {
const {useRange, value, minimumValue = 0, maximumValue = 1, initialMinimumValue, initialMaximumValue} = props;
if (minimumValue > maximumValue ||
useRange && initialMinimumValue && initialMaximumValue && initialMinimumValue > initialMaximumValue) {
if (
minimumValue > maximumValue ||
(useRange && initialMinimumValue && initialMaximumValue && initialMinimumValue > initialMaximumValue)
) {
console.error('Your passed values are invalid. Please check if minimum values are not higher than maximum values');
}
if (value !== undefined && minimumValue && maximumValue && !inRange(value, minimumValue, maximumValue)) {
console.error(`Your passed value (${value}) is invalid.
Please check that it is in range of the minimum (${minimumValue}) and maximum (${maximumValue}) values`);
}
if (useRange && initialMinimumValue && initialMaximumValue) {
if (!inRange(initialMinimumValue, minimumValue, maximumValue) ||
!inRange(initialMaximumValue, minimumValue, maximumValue)) {
console.error('Your passed values are invalid. Please check that they are in range of the minimum and maximum values');
if (
!inRange(initialMinimumValue, minimumValue, maximumValue) ||
!inRange(initialMaximumValue, minimumValue, maximumValue)
) {
console.error(
'Your passed values are invalid. Please check that they are in range of the minimum and maximum values'
);
}
}
}

export function getStepInterpolated(trackWidth: number, minimumValue: number, maximumValue: number, stepXValue: SharedValue<number>) {
export function getStepInterpolated(
trackWidth: number,
minimumValue: number,
maximumValue: number,
stepXValue: SharedValue<number>
) {
'worklet';
const outputRange = [0, trackWidth];
const inputRange = minimumValue < 0 ?
[Math.abs(maximumValue), Math.abs(minimumValue)] : [minimumValue, maximumValue];
const inputRange =
minimumValue < 0 ? [0, Math.abs(minimumValue) + maximumValue] : [0, maximumValue - minimumValue];
return interpolate(stepXValue.value, inputRange, outputRange);
}