@@ -4,18 +4,19 @@ import {Gesture, GestureDetector} from 'react-native-gesture-handler';
44import { type LayoutChangeEvent , type ViewProps , type StyleProp , type ViewStyle , type TextStyle } from 'react-native' ;
55import { interpolate , runOnJS , useAnimatedReaction , useAnimatedStyle , useSharedValue , withSpring , withTiming } from 'react-native-reanimated' ;
66
7- import { styles } from './base- slider.styles' ;
7+ import { styles } from './slider.styles' ;
88import { useTheme } from '../../theme/useTheme.hook' ;
99import { SliderTrack } from './slider-track/SliderTrack.component' ;
1010import { SliderIndicator } from './slider-indicator/SliderIndicator.component' ;
1111import { SliderTrackPoint } from './slider-track-point/SliderTrackPoint.component' ;
1212
13- export interface BaseSliderProps extends ViewProps {
14- min : number ;
13+ export interface SliderProps extends ViewProps {
1514 max : number ;
15+ min : number ;
1616
1717 value ?: number ;
18- trackPoints : number [ ] ;
18+ step ?: number ;
19+ centered ?: boolean ;
1920
2021 thumbStyle ?: StyleProp < ViewStyle > ;
2122 valueStyle ?: StyleProp < TextStyle > ;
@@ -29,12 +30,13 @@ export interface BaseSliderProps extends ViewProps {
2930 onChangeValue ?: ( value : number ) => void ;
3031}
3132
32- export const BaseSlider : React . FC < BaseSliderProps > = ( {
33- min,
33+ export const Slider : React . FC < SliderProps > = ( {
3434 max,
35+ min,
3536
37+ step,
3638 value = 0 ,
37- trackPoints = [ ] ,
39+ centered = false ,
3840
3941 thumbStyle,
4042 valueStyle,
@@ -62,6 +64,14 @@ export const BaseSlider: React.FC<BaseSliderProps> = ({
6264 const filledTrackAnimatedStyle = useAnimatedStyle ( ( ) => ( { flex : interpolate ( thumbTranslationX . value , [ 0 , sliderWidth ] , [ 0 , 1 ] ) } ) , [ sliderWidth ] ) ;
6365 const remainingTrackAnimatedStyle = useAnimatedStyle ( ( ) => ( { flex : interpolate ( thumbTranslationX . value , [ 0 , sliderWidth ] , [ 1 , 0 ] ) } ) , [ sliderWidth ] ) ;
6466
67+ const getDiscreteTrackPoints = ( discreteStep : number ) => {
68+ const totalPoints = Math . ceil ( ( max - min ) / discreteStep ) + 1 ;
69+
70+ return Array . from ( { length : totalPoints } , ( _ , index ) => Math . min ( min + index * discreteStep , max ) ) ;
71+ } ;
72+
73+ const trackPoints = step ? getDiscreteTrackPoints ( step ) : centered ? [ min , ( min + max ) / 2 , max ] : [ max ] ;
74+
6575 const calcAndUpdateValueBasedOnThumbTranslationX = ( translationX : number , onChange : ( value : number ) => void ) => {
6676 const currrentValue = Math . round ( interpolate ( translationX , [ 0 , sliderWidth ] , [ min , max ] ) ) ;
6777
@@ -105,7 +115,8 @@ export const BaseSlider: React.FC<BaseSliderProps> = ({
105115 } ;
106116
107117 const initializeThumbPosition = ( width : number ) => {
108- const initialTthumbTranslationX = ( value * width ) / max ;
118+ const initialTthumbTranslationX = ( ( value - min ) / ( max - min ) ) * width ;
119+
109120 thumbTranslationX . value = initialTthumbTranslationX ;
110121 thumbTranslationXContext . value = initialTthumbTranslationX ;
111122 } ;
@@ -148,7 +159,9 @@ export const BaseSlider: React.FC<BaseSliderProps> = ({
148159 < SliderTrack
149160 style = { [ { backgroundColor : secondaryContainer . background } , styles . remainingTrack , remainingTrackAnimatedStyle , remainingTrackStyle ] }
150161 />
151- < View style = { [ styles . trackPoints , trackPointsStyle ] } > { trackPoints . map ( renderTrackPoint ) } </ View >
162+ < View style = { [ styles . trackPoints , { justifyContent : trackPoints . length > 1 ? 'space-between' : 'flex-end' } , trackPointsStyle ] } >
163+ { trackPoints . map ( renderTrackPoint ) }
164+ </ View >
152165 </ View >
153166 </ GestureDetector >
154167 ) ;
0 commit comments