import React from 'react'; import {View, Text, Animated} from 'react-native'; import {SliderProps} from './types'; import {DEFAULT_THUMB_SIZE, DEFAULT_TRACK_HEIGHT} from './utils'; import useSlider from './hooks/useSlider'; import styles from './styles'; const Slider: React.FC = ({ min, max, step = 1, value, onChange, style, showValue = true, valuePrefix = '', valueSuffix = '', trackColor = '#E5E4E2', thumbColor = '#22223B', trackHeight = DEFAULT_TRACK_HEIGHT, trackWidth, thumbSize = DEFAULT_THUMB_SIZE, allowDecimal = false, disableTrackTouch = false, Thumb, points = [], showFloatingLabel = false, FloatingLabel, trackStyles, trackProgressStyles, // orientation = 'horizontal', // verticalHeight = 200, ...restProps }) => { const { displayValue, trackPanResponder, currentValue, isDragging, trackContainerRef, panResponder, sliderContainerRef, customThumbRef, sliderRef, progressAnim, thumbAnim, // isVertical, getPositionFromValue, } = useSlider({ value, min, max, step, allowDecimal, onChange, thumbSize, // orientation, }); const renderThumb = () => { if (Thumb) { return ( { const {width, height} = event.nativeEvent.layout; customThumbRef.current.width = width; customThumbRef.current.height = height; }} style={[ { position: 'absolute', left: thumbAnim, zIndex: 2, transform: [{scale: isDragging ? 1.2 : 1}], }, ]} {...panResponder.panHandlers}> {Thumb} ); } return ( ); }; const renderPoints = () => { return points.map((point, index) => { const pointPosition = getPositionFromValue(point.value); const pointSize = point.size || 10; const isActive = value !== undefined ? point.value <= value : point.value <= (currentValue || 1); if (point.customPoint) { return ( {point.customPoint} ); } return ( ); }); }; const renderFloatingLabel = () => { if (!showFloatingLabel) return null; const sliderValue = value !== undefined ? value : currentValue; const displayValue = typeof sliderValue === 'number' ? sliderValue : min; if (FloatingLabel) { return ( ); } return ( {valuePrefix} {displayValue} {valueSuffix} ); }; return ( { const {width, height} = event.nativeEvent.layout; // sliderContainerRef.current.width = isVertical ? height : width; sliderContainerRef.current.width = width; }}> {showValue && ( {valuePrefix} {displayValue} {valueSuffix} )} { const {width, height} = event.nativeEvent.layout; // sliderRef.current.width = isVertical ? height : width; sliderRef.current.width = width; sliderRef.current.height = height; }}> {points?.length > 0 && renderPoints()} {renderFloatingLabel()} {renderThumb()} ); }; export default Slider;