Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions example-web/src/Examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,19 @@ export const examples: Props[] = [
);
},
},
// Check the fix for the issue #743
{
title: 'With step numbers',
render() {
return (
<SliderExample
minimumValue={1}
maximumValue={5}
step={1}
renderStepNumber={true}
style={[styles.slider, { height: 70 }]}
/>
);
},
},
];
4 changes: 2 additions & 2 deletions package/src/RNCSliderNativeComponent.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'react-native';
//@ts-ignore
import type {ImageSource} from 'react-native/Libraries/Image/ImageSource';
import {constants} from './utils/constants';

type Event = Readonly<{
nativeEvent: {
Expand Down Expand Up @@ -40,7 +41,6 @@ export interface Props {
inverted: boolean;
disabled: boolean;
trackHeight: number;
thumbSize: number;
thumbImage?: ImageSource;
onRNCSliderSlidingStart: (event: Event) => void;
onRNCSliderSlidingComplete: (event: Event) => void;
Expand All @@ -66,7 +66,6 @@ const RCTSliderWebComponent = React.forwardRef(
inverted = false,
disabled = false,
trackHeight = 4,
thumbSize = 20,
thumbImage,
onRNCSliderSlidingStart = (_: Event) => {},
onRNCSliderSlidingComplete = (_: Event) => {},
Expand Down Expand Up @@ -234,6 +233,7 @@ const RCTSliderWebComponent = React.forwardRef(
flexGrow: maxPercent,
};

const thumbSize = constants.THUMB_SIZE;
const thumbViewStyle = [
{
width: thumbSize,
Expand Down
34 changes: 28 additions & 6 deletions package/src/components/StepsIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {FC, Fragment, useCallback, useMemo} from 'react';
import {View} from 'react-native';
import {Platform, View} from 'react-native';
import {StepNumber} from './StepNumber';
import {MarkerProps, SliderTrackMark} from './TrackMark';
//@ts-ignore
Expand All @@ -15,6 +15,7 @@ export const StepsIndicator = ({
renderStepNumber,
thumbImage,
isLTR,
thumbSize = constants.THUMB_SIZE,
}: {
options: number[];
sliderWidth: number;
Expand All @@ -23,6 +24,7 @@ export const StepsIndicator = ({
renderStepNumber?: boolean;
thumbImage?: ImageSource;
isLTR?: boolean;
thumbSize?: number;
}) => {
const stepNumberFontStyle = useMemo(() => {
return {
Expand All @@ -32,13 +34,35 @@ export const StepsIndicator = ({
: constants.STEP_NUMBER_TEXT_FONT_BIG,
};
}, [options.length]);

const platformDependentStyles = useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const platformDependentStyles = useMemo(() => {
const platformDependentStyles = useMemo(() => {

const isWeb = Platform.OS === 'web';
return {
stepIndicatorContainerStyle: isWeb
? styles.stepsIndicator
: {
...styles.stepsIndicator,
marginHorizontal: sliderWidth * constants.MARGIN_HORIZONTAL_PADDING,
},
stepIndicatorElementStyle: isWeb
? {
...styles.stepIndicatorElement,
width: thumbSize,
justifyContent: 'space-between' as const,
}
: styles.stepIndicatorElement,
};
}, [sliderWidth, thumbSize]);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}, [sliderWidth, thumbSize]);
}, [sliderWidth, thumbSize]);


const values = isLTR ? options.reverse() : options;

const renderStepIndicator = useCallback(
(i: number, index: number) => {
return (
<Fragment key={index}>
<View style={styles.stepIndicatorElement} key={`${index}-View`}>
<View
style={platformDependentStyles.stepIndicatorElementStyle}
key={`${index}-View`}>
<SliderTrackMark
key={`${index}-SliderTrackMark`}
isTrue={currentValue === i}
Expand Down Expand Up @@ -67,16 +91,14 @@ export const StepsIndicator = ({
thumbImage,
renderStepNumber,
stepNumberFontStyle,
platformDependentStyles.stepIndicatorElementStyle,
],
);

return (
<View
pointerEvents="none"
style={[
styles.stepsIndicator,
{marginHorizontal: sliderWidth * constants.MARGIN_HORIZONTAL_PADDING},
]}>
style={platformDependentStyles.stepIndicatorContainerStyle}>
{values.map((i, index) => renderStepIndicator(i, index))}
</View>
);
Expand Down
2 changes: 2 additions & 0 deletions package/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {Platform} from 'react-native';
export const constants = {
SLIDER_DEFAULT_INITIAL_VALUE: 0,
MARGIN_HORIZONTAL_PADDING: 0.05,
// Default thumb size for web platform (used in step indicator positioning)
THUMB_SIZE: 20,
STEP_NUMBER_TEXT_FONT_SMALL: 8,
STEP_NUMBER_TEXT_FONT_BIG: 12,
LIMIT_MIN_VALUE: Number.MIN_SAFE_INTEGER,
Expand Down