Убрал рефы.

Починил поведение при неизменяющемся значении
This commit is contained in:
Фролов 2021-05-28 14:30:55 +05:00
parent 3757112dc1
commit eb92d5a0d7
2 changed files with 95 additions and 21332 deletions

21393
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +1,42 @@
import { useState, useEffect, useRef } from 'react'; import { useState, useEffect } from 'react';
import {CaretUpOutlined, CaretDownOutlined} from '@ant-design/icons' import {CaretUpOutlined, CaretDownOutlined} from '@ant-design/icons'
export const ValueDisplay = ({prefix, value, suffix, isArrowVisible}) =>{ export const ValueDisplay = ({prefix, value, suffix, isArrowVisible}) =>{
const [oldVal, setOldVal] = useState(NaN) const [oldVal, setOldVal] = useState(NaN)
const [val, setVal] = useState('---') const [val, setVal] = useState('---')
const arrowRef = useRef(null); const [arrowState, setArrowState] = useState(0)
useEffect(()=>{ useEffect(()=>{
if(value) if(value){
if(Number.isFinite(+value)){ if(Number.isFinite(+value)){
setVal((+value).toPrecision(4)??'---')
if (isArrowVisible) if (isArrowVisible)
{ {
let newArrowState = 0
if (value > oldVal) if (value > oldVal)
arrowRef.current = (<CaretUpOutlined style={{color:"red"}} />) newArrowState = 1
else if (value < oldVal) if (value < oldVal)
arrowRef.current = (<CaretDownOutlined style={{color:"red"}} />) newArrowState = -1
setArrowState(newArrowState)
setOldVal(value) setOldVal(value)
} }
} else setVal((+value).toPrecision(4)??'---')
setVal(value) }
else
setVal(value)
}
},[value]) },[value])
return(<span className='display_value'>{prefix} {val} {suffix}{arrowRef.current}</span>) let arrow = null
switch (arrowState){
case 1:
arrow = <CaretUpOutlined style={{color:"red"}}/>
break;
case -1:
arrow = <CaretDownOutlined style={{color:"red"}}/>
}
return(<span className='display_value'>{prefix} {val} {suffix}{arrow}</span>)
} }
export const Display = (props)=>{ export const Display = (props)=>{