|
| 1 | +import React from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import {GestureDetector} from 'react-native-gesture-handler'; |
| 4 | +import {type LayoutChangeEvent, type ViewProps, type StyleProp, type ViewStyle, type TextStyle} from 'react-native'; |
| 5 | + |
| 6 | +import {styles} from './range-slider.styles'; |
| 7 | +import {useRangeSlider} from './useRangeSlider.hook'; |
| 8 | +import {useSliderColors} from '../use-slider-colors.hook'; |
| 9 | +import {SliderTrack} from '../ui/slider-track/SliderTrack.component'; |
| 10 | +import {useRangeSliderTrackPoints} from './useRangeSliderTrackPoints.hook'; |
| 11 | +import {SliderIndicator} from '../ui/slider-indicator/SliderIndicator.component'; |
| 12 | +import {SliderTrackPoint} from '../ui/slider-track-point/SliderTrackPoint.component'; |
| 13 | + |
| 14 | +export interface RangeSliderProps extends ViewProps { |
| 15 | + max: number; |
| 16 | + min: number; |
| 17 | + |
| 18 | + range?: number[]; |
| 19 | + step?: number; |
| 20 | + damping?: number; |
| 21 | + centered?: boolean; |
| 22 | + disabled?: boolean; |
| 23 | + |
| 24 | + valueHeight?: number; |
| 25 | + thumbWidthActive?: number; |
| 26 | + thumbWidthInactive?: number; |
| 27 | + |
| 28 | + thumbStyle?: StyleProp<ViewStyle>; |
| 29 | + valueStyle?: StyleProp<TextStyle>; |
| 30 | + indicatorStyle?: StyleProp<ViewStyle>; |
| 31 | + trackPointStyle?: StyleProp<ViewStyle>; |
| 32 | + trackPointsStyle?: StyleProp<ViewStyle>; |
| 33 | + filledTrackStyle?: StyleProp<ViewStyle>; |
| 34 | + remainingTrackStyle?: StyleProp<ViewStyle>; |
| 35 | + |
| 36 | + onChangeRange?: (range: number[]) => void; |
| 37 | +} |
| 38 | + |
| 39 | +export const RangeSlider: React.FC<RangeSliderProps> = ({ |
| 40 | + max, |
| 41 | + min, |
| 42 | + |
| 43 | + step, |
| 44 | + range = [0, 0], |
| 45 | + damping = 20, |
| 46 | + centered = false, |
| 47 | + disabled = false, |
| 48 | + |
| 49 | + thumbStyle, |
| 50 | + valueStyle, |
| 51 | + indicatorStyle, |
| 52 | + trackPointStyle, |
| 53 | + trackPointsStyle, |
| 54 | + filledTrackStyle, |
| 55 | + remainingTrackStyle, |
| 56 | + |
| 57 | + valueHeight, |
| 58 | + thumbWidthActive, |
| 59 | + thumbWidthInactive, |
| 60 | + |
| 61 | + onChangeRange, |
| 62 | + onLayout, |
| 63 | + style, |
| 64 | + ...props |
| 65 | +}) => { |
| 66 | + const {filledTrackColor, remainingTrackColor} = useSliderColors(disabled, centered); |
| 67 | + |
| 68 | + const trackPoints = useRangeSliderTrackPoints({max, min, step, centered}); |
| 69 | + const { |
| 70 | + gesture, |
| 71 | + selectedRange, |
| 72 | + thumbMinSliding, |
| 73 | + thumbMaxSliding, |
| 74 | + animMinValueProps, |
| 75 | + animMaxValueProps, |
| 76 | + filledTrackAnimatedStyle, |
| 77 | + remainingTrackAfterAnimatedStyle, |
| 78 | + remainingTrackBeforeAnimatedStyle, |
| 79 | + slideToTrackPoint, |
| 80 | + setUpSliderLayout, |
| 81 | + } = useRangeSlider({max, min, centered, step, damping}, range, onChangeRange); |
| 82 | + |
| 83 | + const handleLayoutChange = (e: LayoutChangeEvent) => { |
| 84 | + setUpSliderLayout(e); |
| 85 | + |
| 86 | + if (onLayout) { |
| 87 | + onLayout(e); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const renderTrackPoint = (pointValue: number) => ( |
| 92 | + <SliderTrackPoint |
| 93 | + key={pointValue} |
| 94 | + value={pointValue} |
| 95 | + disabled={disabled} |
| 96 | + selectedValue={selectedRange} |
| 97 | + onPress={slideToTrackPoint} |
| 98 | + style={trackPointStyle} |
| 99 | + /> |
| 100 | + ); |
| 101 | + |
| 102 | + return ( |
| 103 | + <GestureDetector gesture={gesture.enabled(!disabled)}> |
| 104 | + <View style={[styles.container, style]} onLayout={handleLayoutChange} {...props}> |
| 105 | + <SliderTrack |
| 106 | + style={[{backgroundColor: remainingTrackColor}, styles.remainingBeforeTrack, remainingTrackBeforeAnimatedStyle, filledTrackStyle]} |
| 107 | + /> |
| 108 | + <SliderIndicator |
| 109 | + animValueProps={animMinValueProps} |
| 110 | + sliding={thumbMinSliding} |
| 111 | + disabled={disabled} |
| 112 | + style={[styles.thumb, indicatorStyle]} |
| 113 | + thumbStyle={thumbStyle} |
| 114 | + valueStyle={valueStyle} |
| 115 | + valueHeight={valueHeight} |
| 116 | + thumbWidthActive={thumbWidthActive} |
| 117 | + thumbWidthInactive={thumbWidthInactive} |
| 118 | + /> |
| 119 | + <SliderTrack style={[{backgroundColor: filledTrackColor}, styles.filledTrack, filledTrackAnimatedStyle, filledTrackStyle]} /> |
| 120 | + <SliderIndicator |
| 121 | + animValueProps={animMaxValueProps} |
| 122 | + sliding={thumbMaxSliding} |
| 123 | + disabled={disabled} |
| 124 | + style={[styles.thumb, indicatorStyle]} |
| 125 | + thumbStyle={thumbStyle} |
| 126 | + valueStyle={valueStyle} |
| 127 | + valueHeight={valueHeight} |
| 128 | + thumbWidthActive={thumbWidthActive} |
| 129 | + thumbWidthInactive={thumbWidthInactive} |
| 130 | + /> |
| 131 | + <SliderTrack |
| 132 | + style={[{backgroundColor: remainingTrackColor}, styles.remainingAfterTrack, remainingTrackAfterAnimatedStyle, remainingTrackStyle]} |
| 133 | + /> |
| 134 | + <View style={[styles.trackPoints, trackPointsStyle]}>{trackPoints.map(renderTrackPoint)}</View> |
| 135 | + </View> |
| 136 | + </GestureDetector> |
| 137 | + ); |
| 138 | +}; |
0 commit comments