SlidngScale.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { ScrollView, View, Text, Image } from "@tarojs/components";
  2. import './SlidngScale.scss'
  3. import { useEffect, useRef, useState } from "react";
  4. import Taro from "@tarojs/taro";
  5. export default function Component(props: {
  6. step: number, min: number,
  7. max: number, default_value: number, changed: Function, unit: string
  8. themeColor: string
  9. }) {
  10. const scrollViewRef = useRef<any>();
  11. const minNum: number = props.min;
  12. const maxNum: number = props.max;
  13. const stepNum: number = props.step * 10;
  14. const jingdu: number = props.step < 1 ? 10 : 1;
  15. const list: any[] = [];
  16. const slidngWidth = 10
  17. const [current, setCurrent] = useState(props.default_value)
  18. const [left, setLeft] = useState((props.default_value - props.min) * slidngWidth / props.step + 1);
  19. for (var i: number = props.min; i <= props.max; i += props.step) {
  20. var value: number = parseFloat(i.toFixed(1));
  21. var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0;
  22. var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  23. list.push({
  24. value: props.step < 1 ? value : Math.round(value),
  25. showBig: isBig && i >= minNum && i <= maxNum,
  26. showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum
  27. })
  28. }
  29. useEffect(() => {
  30. // var aa = current-props.min;
  31. // var bb = aa/props.step*slidngWidth;
  32. // setLeft(bb/ratio)
  33. }, [])
  34. // const dpr = Taro.pxTransform//Taro.getSystemInfoSync().pixelRatio;
  35. const handleScroll = (e) => {
  36. const { scrollLeft } = e.detail;
  37. var count = scrollLeft / slidngWidth;
  38. var strValue = (props.min + Math.round(count) * props.step).toFixed(1);
  39. if ((strValue as any) < props.min) {
  40. strValue = props.min as any;
  41. }
  42. if ((strValue as any) > props.max) {
  43. strValue = props.max as any;
  44. }
  45. if (parseFloat(strValue) != current) {
  46. var data = strValue as any;
  47. if (props.step < 1) {
  48. data = parseFloat(strValue).toFixed(1);
  49. }
  50. else {
  51. data = Math.round(data);
  52. }
  53. setCurrent(data);
  54. props.changed(data);
  55. }
  56. };
  57. return <View className="slidng">
  58. <View className="number_bg">
  59. <Text className="number">{current}</Text>
  60. <Text className="unit">{props.unit}</Text>
  61. </View>
  62. <View className="scroll_bg">
  63. <View className="top_line" style={{ backgroundColor: props.themeColor }} />
  64. <ScrollView ref={scrollViewRef} scrollX scrollLeft={left} className="scroll" onScroll={handleScroll}>
  65. <View className="scrollContent">
  66. <View className="scrollPadding" />
  67. <View className="content">
  68. {
  69. list.map((item, index) => {
  70. return <View className={item.showBig ? 'slidng_item_big' : item.showMiddle ? 'slidng_item_middle' : 'slidng_item'} style={{ width: 2, marginRight: 8, backgroundColor: props.themeColor }} key={index}>
  71. {
  72. item.showBig ? <Text className="slidng_text">{item.value}</Text> : null
  73. }
  74. </View>
  75. })
  76. }
  77. </View>
  78. {/* <View className="demo2"/> */}
  79. <View className="scrollPadding" />
  80. </View>
  81. </ScrollView>
  82. <View className="shadow_top"/>
  83. <View className="shadow_left"/>
  84. <View className="shadow_right"/>
  85. <Image className="center_line" src={require('@assets/images/scale_center.png')} />
  86. {/* <View className="center_line" /> */}
  87. </View>
  88. </View>
  89. }