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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ To use this library you need to ensure you are using the correct version of Reac
| `maximumTrackImage` | Assigns a maximum track image. Only static images are supported. The leftmost pixel of the image will be stretched to fill the track. | Image<br/>.propTypes<br/>.source | iOS |
| `minimumTrackImage` | Assigns a minimum track image. Only static images are supported. The rightmost pixel of the image will be stretched to fill the track. | Image<br/>.propTypes<br/>.source | iOS |
| `thumbImage` | Sets an image for the thumb. Only static images are supported. Needs to be a URI of a local or network image; base64-encoded SVG is not supported. | Image<br/>.propTypes<br/>.source | |
| `thumbSize` | Define the size of the thumb. Only for web<br/>Default value is 20px. | number | web |
| `trackImage` | Assigns a single image for the track. Only static images are supported. The center pixel of the image will be stretched to fill the track. | Image<br/>.propTypes<br/>.source | iOS |
| [`StepMarker`](#stepmarker) | Component to be rendered for each step on the track,<br/>with the possibility to change the styling, when thumb is at that given step | `FC<MarkerProps>` | iOS, Android, Windows |
| [`StepMarker`](#stepmarker) | Component to be rendered for each step on the track,<br/>with the possibility to change the styling, when thumb is at that given step | `FC<MarkerProps>` | |
| [`renderStepNumber`](#renderstepnumber) | Turns on the displaying of numbers of steps.<br/>Numbers of steps are displayed under the track | bool | iOS, Android, Windows |
| `ref` | Reference object. | MutableRefObject | web |
| `View` | [Inherited `View` props...](https://github.com/facebook/react-native-website/blob/master/docs/view.md#props) | | |
Expand Down
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 }]}
/>
);
},
},
];
3 changes: 2 additions & 1 deletion 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 @@ -66,7 +67,7 @@ const RCTSliderWebComponent = React.forwardRef(
inverted = false,
disabled = false,
trackHeight = 4,
thumbSize = 20,
thumbSize = constants.THUMB_SIZE,
thumbImage,
onRNCSliderSlidingStart = (_: Event) => {},
onRNCSliderSlidingComplete = (_: Event) => {},
Expand Down
10 changes: 10 additions & 0 deletions package/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ type IOSProps = Readonly<{
tapToSeek?: boolean;
}>;

type WebProps = Readonly<{
/**
* The size of the thumb on the web platform.
* Default value is 20px.
*/
thumbSize?: number;
}>;

type Props = ViewProps &
IOSProps &
WebProps &
WindowsProps &
Readonly<{
/**
Expand Down Expand Up @@ -301,6 +310,7 @@ const SliderComponent = (
thumbImage={props.thumbImage}
StepMarker={props.StepMarker}
isLTR={inverted}
thumbSize={props.thumbSize}
/>
) : null}
<RCTSliderNativeComponent
Expand Down
32 changes: 26 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, ViewStyle} 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,33 @@ export const StepsIndicator = ({
: constants.STEP_NUMBER_TEXT_FONT_BIG,
};
}, [options.length]);
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]);
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 +89,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