import { ScrollView, View, Text, Image } from "@tarojs/components"; import VirtualList from '@tarojs/components/virtual-list'; import './SlidngScale.scss' import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import { rpxToPx } from "@/utils/tools"; import { throttle } from 'lodash'; // import { SvgXml } from "react-native-svg"; var timerA = null var lastValue = 0 let SvgXml; if (process.env.TARO_ENV == 'rn') { SvgXml = require("react-native-svg").SvgXml } export default function Component(props: { step: number, min: number, max: number, default_value: number, scale: number, changed: Function, unit: string themeColor: string, updateStatus: Function, special: any }) { const scrollViewRef = useRef(); const minNum: number = props.min; const maxNum: number = props.max; const min = props.min const max = props.max const stepNum: number = props.step * 10; const jingdu: number = props.step < 0.1 ? 100 : props.step < 1 ? 10 : 1; const list: any[] = []; const slidngWidth = 10 const [isFT_IN, setIsFT_IN] = useState(props.special == 'FT_IN') const [current, setCurrent] = useState(props.default_value) const [left, setLeft] = useState((props.default_value - min) * slidngWidth * (isFT_IN ? 1.2 : 1) / props.step - 0.5); const [isDraging, setIsDraging] = useState(false) const [isEnd, setIsEnd] = useState(true) const [enableText, setEnableText] = useState(true) const [isChanging, setIsChanging] = useState(false) useEffect(() => { setCurrent(props.default_value) setIsFT_IN(props.special == 'FT_IN') setLeft((props.default_value - min) * slidngWidth * (props.special == 'FT_IN' ? 1.2 : 1) / props.step - 0.5) setIsChanging(true) setTimeout(() => { setIsChanging(false) }, process.env.TARO_ENV == 'weapp' ? 700 : 100) }, [props.unit, props.default_value, props.special]) if (isFT_IN) { for (var i: number = minNum; i <= maxNum * 1.2; i += props.step) { i = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale)); var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale)); var isBig: boolean = i % 12 == 0; var isMiddle: boolean = i % 6 == 0 && !isBig list.push({ value: props.step < 1 ? value : Math.round(value), showBig: isBig, showMiddle: isMiddle }) } } else { for (var i: number = minNum; i <= maxNum; i += props.step) { i = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale)) var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale)); var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0; var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0; list.push({ value: props.step < 1 ? value : Math.round(value), showBig: isBig && i >= minNum && i <= maxNum, showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum }) } } useEffect(() => { if (process.env.TARO_ENV == 'weapp') { if (isEnd && !isDraging) { props.updateStatus(true) setEnableText(true) } else { props.updateStatus(false) setEnableText(false) } } }, [isEnd, isDraging]) function scrollContent(e) { if (global.dimissSel) { global.dimissSel() } if (isChanging) { return } if (process.env.TARO_ENV == 'rn') { update(e) setIsDraging(false) setIsEnd(true) return } lastValue = e.detail.scrollLeft; if (timerA) clearTimeout(timerA); timerA = setTimeout(() => { update(e) setIsDraging(false) }, 250) as any } const handleScroll = throttle((e) => { if (global.dimissSel) { global.dimissSel() } update(e) }, 500); function touchEnd() { setIsEnd(true) var temp = lastValue setTimeout(() => { if (temp == lastValue) { setIsDraging(false) } }, 250) } function touchStart() { if (process.env.TARO_ENV == 'weapp') { props.updateStatus(false) setEnableText(false) } } function dragStart(e) { lastValue = e.detail.scrollLeft; setIsEnd(false) setIsDraging(true) } function dragEnd(e) { setIsEnd(true) } function update(e) { const { scrollLeft } = e.detail; var count = scrollLeft / slidngWidth; if (isFT_IN) { count = count / 1.2 } var strValue = (minNum + Math.round(count) * props.step).toFixed(props.scale == 0 ? 1 : props.scale); if ((strValue as any) < minNum) { strValue = minNum as any; } if ((strValue as any) > maxNum) { strValue = maxNum as any; } if (parseFloat(strValue) != current) { var data = strValue as any; if (props.step < 1) { data = parseFloat(strValue).toFixed(props.scale == 0 ? 1 : props.scale); if (data.indexOf('.') > 0) { const regexp = /(?:\.0*|(\.\d+?)0+)$/ data = data.replace(regexp, '$1') } } else { data = Math.round(data); } if (process.env.TARO_ENV == 'rn') { const ReactNativeHapticFeedback = require("react-native-haptic-feedback") // Optional configuration const options = { enableVibrateFallback: true, ignoreAndroidSystemSettings: false, }; // Trigger haptic feedback ReactNativeHapticFeedback.trigger("impactLight", options); } setCurrent(data); props.changed(data); } } const scrollEnd = (e) => { } var svgLine = ``; var svgNumber = ``; if (isFT_IN) { for (var i = 0; i < 12; i++) { var obj = list[i]; svgLine += `` } } else { for (var i = 0; i < 10; i++) { var obj = list[i]; svgLine += `` } } for (var i = 0; i < list.length; i++) { var obj = list[i]; if (obj.showBig) { //字体设置参考 https://segmentfault.com/a/1190000006110417 svgNumber += `${isFT_IN ? parseInt('' + obj.value / 12) : obj.value}` } } svgLine += `` svgNumber += `` var basestr = encodeURIComponent(svgLine) var imgLines = `url("data:image/svg+xml,${basestr}");` var basestr2 = encodeURIComponent(svgNumber) var imgNum = `url("data:image/svg+xml,${basestr2}");` var svgLine2 = `` svgLine2 += `` if (isFT_IN) { for (var i = 0; i < 12; i++) { var obj = list[i]; svgLine2 += `` } } else { for (var i = 0; i < 10; i++) { var obj = list[i]; svgLine2 += `` } } svgLine2 += ` ` return { isFT_IN && { parseInt(current / 12 + '') > 0 && (process.env.TARO_ENV == 'weapp' ? {parseInt(current / 12 + '')} : {parseInt(current / 12 + '')}) } {parseInt(current / 12 + '') > 0 && ft} { current % 12 > 0 && (process.env.TARO_ENV == 'weapp' ? {current % 12} : {current % 12}) } { current % 12 > 0 && in } } { !isFT_IN && { process.env.TARO_ENV == 'weapp' ? {current} : {current} } {props.unit} } { process.env.TARO_ENV == 'weapp' && } { process.env.TARO_ENV == 'rn' && } {process.env.TARO_ENV == 'weapp' && } { process.env.TARO_ENV == 'rn' && } {/* */} }