import React, { useState, useCallback } from 'react' import { View, Text } from '@tarojs/components' import Taro from '@tarojs/taro' import './NTRPSlider.scss' export interface NTRPRange { min: number max: number } // 获取NTRP显示文本的工具函数 export const getNTRPRangeText = (range: NTRPRange): string => { if (range.min === 2.0 && range.max === 4.0) { return '不限' } return `${range.min} - ${range.max}` } interface NTRPSliderProps { value: NTRPRange onChange: (range: NTRPRange) => void title?: string showTitle?: boolean } const NTRPSlider: React.FC = ({ value = { min: 1.0, max: 5.0 }, onChange, title = 'NTRP水平要求', showTitle = false }) => { const [activeThumb, setActiveThumb] = useState<'min' | 'max' | null>(null) // 计算滑动条位置百分比 const getSliderPercentage = useCallback((level: number) => { return ((level - 2.0) / 2.0) * 100 }, []) // 获取当前NTRP显示文本 const currentRangeText = getNTRPRangeText(value) const handleSliderTouchStart = useCallback((thumb: 'min' | 'max') => { setActiveThumb(thumb) }, []) const handleSliderTouchMove = useCallback((e: any) => { if (!activeThumb) return e.preventDefault() const query = Taro.createSelectorQuery() query.select('.ntrp-slider-track').boundingClientRect((rect: any) => { if (rect && !Array.isArray(rect)) { const touch = e.touches[0] const relativeX = touch.clientX - rect.left const percentage = Math.max(0, Math.min(1, relativeX / rect.width)) const level = Number((2.0 + percentage * 2.0).toFixed(1)) if (activeThumb === 'min') { const newMin = Math.min(level, value.max - 0.1) onChange({ min: newMin, max: value.max }) } else { const newMax = Math.max(level, value.min + 0.1) onChange({ min: value.min, max: newMax }) } } }).exec() }, [activeThumb, value, onChange]) const handleSliderTouchEnd = useCallback(() => { setActiveThumb(null) }, []) return ( {showTitle && ( {title} {currentRangeText} )} 2.0及以下 4.0及以上 {/* 背景轨道 */} {/* 选中区间 */} {/* 最小值滑块 */} handleSliderTouchStart('min')} > {value.min} {/* 最大值滑块 */} handleSliderTouchStart('max')} > {value.max} ) } export default NTRPSlider