import React, { useState, useRef, useEffect, useContext } from 'react'; import { Animated, TouchableOpacity, StyleSheet } from 'react-native'; import { ThemeContext } from '../Theme'; import { Title } from './Text'; export interface IProps { success: number; warning: number; failed: number; } // This component is a bar divided in three parts // A part to represent success request, a part for warning and a part // for requests failed. // It is possible to press on the indicator to expand it and displayed // a value in percent for each part. export const Indicator: React.FC = (props: IProps) => { const theme = useContext(ThemeContext); const height = useRef(new Animated.Value(6)).current; const [isOpened, setIsOpened] = useState(false); useEffect(() => { Animated.timing(height, { toValue: isOpened ? 24 : 6, duration: 500, useNativeDriver: false, }).start(); }, [isOpened]); return ( setIsOpened(!isOpened)}> {isOpened && props.success > 10 && ( {`${Math.round(props.success)}%`} )} {isOpened && props.warning > 10 && ( {`${Math.round(props.warning)}%`} )} {isOpened && props.failed > 10 && ( {`${Math.round(props.failed)}%`} )} ); }; const styles = StyleSheet.create({ text: { fontSize: 16, fontWeight: 'bold', }, }); export default Indicator;