|
|
@@ -1,16 +1,25 @@
|
|
|
import { View, Text } from '@tarojs/components'
|
|
|
import './Slider.scss'
|
|
|
import { ColorType } from '@/context/themes/color'
|
|
|
-import { useRef, useState } from 'react';
|
|
|
+import { useEffect, useRef, useState } from 'react';
|
|
|
import { rpxToPx } from '@/utils/tools';
|
|
|
+import Taro from '@tarojs/taro';
|
|
|
+import { useSelector } from 'react-redux';
|
|
|
|
|
|
var currentValue = 0;
|
|
|
export default function (props: { onChanged?: Function, value?: number, edit?: boolean }) {
|
|
|
-
|
|
|
- const [brightness, setBrightness] = useState(0);
|
|
|
+ const sliderWidth = 578;
|
|
|
+ const [brightness, setBrightness] = useState(props.value ? props.value*10 : 0);
|
|
|
const [isSliding, setIsSliding] = useState(false);
|
|
|
const [value, setValue] = useState(props.value ? props.value : 0)
|
|
|
const [startX, setStartX] = useState(0);
|
|
|
+ const common = useSelector((state: any) => state.common);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ // if (props.value) {
|
|
|
+ // setBrightness(props.value * 10)
|
|
|
+ // }
|
|
|
+ }, [props.value])
|
|
|
|
|
|
const handleTouchStart = (event) => {
|
|
|
setIsSliding(true);
|
|
|
@@ -19,13 +28,12 @@ export default function (props: { onChanged?: Function, value?: number, edit?: b
|
|
|
|
|
|
const handleTouchMove = (event) => {
|
|
|
if (isSliding) {
|
|
|
-
|
|
|
const { touches } = event;
|
|
|
const touchX = touches[0].clientX;
|
|
|
const deltaX = touchX - startX;
|
|
|
// console.log(deltaY,startY,touchY)
|
|
|
// const containerHeight = rpxToPx(182); // 容器高度,根据实际情况调整
|
|
|
- let brightnessValue = brightness + deltaX / rpxToPx(600 - 120) * 100;// / containerHeight * 100;
|
|
|
+ let brightnessValue = brightness + deltaX / rpxToPx(sliderWidth - 120) * 100;// / containerHeight * 100;
|
|
|
|
|
|
// 边界限制
|
|
|
brightnessValue = Math.max(0, Math.min(brightnessValue, 100));
|
|
|
@@ -44,9 +52,15 @@ export default function (props: { onChanged?: Function, value?: number, edit?: b
|
|
|
props.onChanged(value);
|
|
|
}
|
|
|
if (props.edit) {
|
|
|
- console.log('mind')
|
|
|
- if (startX != 0) {
|
|
|
+ if (startX != 0 && value != 0) {
|
|
|
// 松手后,滑块动画移动到最左边
|
|
|
+ Taro.vibrateShort({
|
|
|
+ type: 'heavy'
|
|
|
+ })
|
|
|
+ Taro.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ title: '请先上传!'
|
|
|
+ })
|
|
|
const animationId = requestAnimationFrame(moveToStart);
|
|
|
return () => cancelAnimationFrame(animationId);
|
|
|
}
|
|
|
@@ -54,11 +68,11 @@ export default function (props: { onChanged?: Function, value?: number, edit?: b
|
|
|
};
|
|
|
|
|
|
const moveToStart = () => {
|
|
|
- const newPosition = Math.max(currentValue - 3, 0);
|
|
|
+ const newPosition = Math.max(currentValue - 8, 0);
|
|
|
currentValue = newPosition
|
|
|
setBrightness(newPosition)
|
|
|
+ setValue(calculateNumber(newPosition))
|
|
|
if (newPosition > 0) {
|
|
|
- console.log(newPosition)
|
|
|
requestAnimationFrame(moveToStart);
|
|
|
} else {
|
|
|
setStartX(0);
|
|
|
@@ -100,19 +114,33 @@ export default function (props: { onChanged?: Function, value?: number, edit?: b
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ function getTitle() {
|
|
|
+ var obj = common.food_scales.filter((item: any) => {
|
|
|
+ return item.point == value
|
|
|
+ })
|
|
|
+ return obj[0].name
|
|
|
+ }
|
|
|
|
|
|
- return <View style={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
+ function getDesc() {
|
|
|
+ var obj = common.food_scales.filter((item: any) => {
|
|
|
+ return item.point == value
|
|
|
+ })
|
|
|
+ return obj[0].description ?? '暂无描述'
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return <View style={{ display: 'flex', flexDirection: 'column', position: 'relative', width: rpxToPx(578) }}>
|
|
|
<View className='slider-container'
|
|
|
catchMove
|
|
|
onTouchStart={handleTouchStart}
|
|
|
onTouchMove={handleTouchMove}
|
|
|
onTouchEnd={handleTouchEnd}>
|
|
|
- <View className='slider-item-bg' style={{ width: brightness * 0.01 * rpxToPx(600 - 120) + rpxToPx(120) }}>
|
|
|
+ <View className='slider-item-bg' style={{ width: brightness * 0.01 * rpxToPx(sliderWidth - 120) + rpxToPx(120) }}>
|
|
|
|
|
|
|
|
|
</View>
|
|
|
<View className='slider-item' style={{
|
|
|
- width: brightness * 0.01 * rpxToPx(600 - 120) + rpxToPx(120),
|
|
|
+ width: brightness * 0.01 * rpxToPx(sliderWidth - 120) + rpxToPx(120),
|
|
|
backgroundColor: brightness <= 50 ? ColorType.fast : ColorType.food,
|
|
|
opacity: calculateOpacity(brightness),
|
|
|
}}>
|
|
|
@@ -123,12 +151,15 @@ export default function (props: { onChanged?: Function, value?: number, edit?: b
|
|
|
</View>
|
|
|
|
|
|
</View>
|
|
|
- {/* <View style={{ display: 'flex', flexDirection: 'row' }}>
|
|
|
- {
|
|
|
- colors.map(item => {
|
|
|
- return <View style={{ width: 15, height: 40, backgroundColor: item }} />
|
|
|
- })
|
|
|
- }
|
|
|
- </View> */}
|
|
|
+ {
|
|
|
+ isSliding && <View className='tooltip_bg'>
|
|
|
+ <View style={{ flex: 1 }} />
|
|
|
+ <View className='tooltip_content'>
|
|
|
+ <Text className='tooltip_title'>{getTitle()}</Text>
|
|
|
+ <Text className='tooltip_desc'>{getDesc()}</Text>
|
|
|
+ </View>
|
|
|
+ <View className='tooltip_arrow' style={{ marginLeft: brightness * 0.01 * rpxToPx(sliderWidth - 120) + rpxToPx((120 - 50) / 2) }} />
|
|
|
+ </View>
|
|
|
+ }
|
|
|
</View>
|
|
|
}
|