import React, { useState, useEffect } from 'react'; import { StyleProp, StyleSheet, View, ViewStyle, LayoutChangeEvent, } from 'react-native'; import type { Theme } from '../../types'; import { withTheme } from '../../core/theming'; import { blockSizes } from '../../styles/styles'; import { Panel } from '../..'; type Props = React.ComponentPropsWithRef & { background?: 'canvas' | 'material'; percent: number; style?: StyleProp; theme: Theme; variant?: 'default' | 'tile' | 'indeterminate'; }; const tileWidth = 17; const Progress = ({ background = 'material', percent = 0, style = {}, theme, variant = 'default', ...rest }: Props) => { const [tilesNumber, setTilesNumber] = useState(0); const [progressWidth, setProgressWidth] = useState(0); function onProgressWidthChange(e: LayoutChangeEvent) { const { width } = e.nativeEvent.layout; setProgressWidth(width); } useEffect(() => { setTilesNumber(Math.round((percent / 100) * (progressWidth / tileWidth))); }, [percent]); return ( {variant === 'tile' ? ( {Array(tilesNumber) .fill(null) .map((_, index) => ( ))} ) : ( )} ); }; const styles = StyleSheet.create({ wrapper: { position: 'relative', padding: 4, }, progressWrapper: { width: '100%', height: '100%', }, progress: { height: '100%', }, tilesWrapper: { flexDirection: 'row', flex: 1, borderWidth: 1, }, tile: { width: tileWidth, borderWidth: 1, }, }); export default withTheme(Progress);