asb_cloud_front/src/components/Display.jsx

55 lines
1.4 KiB
React
Raw Normal View History

import { useState, useEffect } from 'react';
import {CaretUpOutlined, CaretDownOutlined} from '@ant-design/icons'
export const ValueDisplay = ({prefix, value, suffix, isArrowVisible}) =>{
const [oldVal, setOldVal] = useState(NaN)
const [val, setVal] = useState('---')
const [arrowState, setArrowState] = useState(0)
useEffect(()=>{
if(value){
if(Number.isFinite(+value)){
if (isArrowVisible)
{
let newArrowState = 0
if (value > oldVal)
newArrowState = 1
if (value < oldVal)
newArrowState = -1
setArrowState(newArrowState)
setOldVal(value)
}
setVal((+value).toPrecision(4)??'---')
}
else
setVal(value)
}
2021-05-28 14:37:54 +05:00
},[value, isArrowVisible, oldVal])
2021-04-16 15:50:01 +05:00
let arrow = null
switch (arrowState){
case 1:
arrow = <CaretUpOutlined style={{color:"red"}}/>
2021-05-28 14:37:54 +05:00
break
case -1:
arrow = <CaretDownOutlined style={{color:"red"}}/>
2021-05-28 14:37:54 +05:00
break
default:
break
}
return(<span className='display_value'>{prefix} {val} {suffix}{arrow}</span>)
2021-04-16 15:50:01 +05:00
}
export const Display = (props)=>{
const {label} = props
return <div className={props.className}>
<div className='display_label'>{label}</div>
<div style={{display:"flex", flexGrow:1}}>
<ValueDisplay {...props}/>
</div>
</div>
}