UNPKG

saagie-ui

Version:

Saagie UI from Saagie Design System

79 lines (68 loc) 1.79 kB
import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; const propTypes = { /** * The component used for the root node. * Either a string to use a DOM element or a component. */ tag: PropTypes.elementType, defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), className: PropTypes.string, timeRange: PropTypes.number, start: PropTypes.number.isRequired, end: PropTypes.number.isRequired, isFailed: PropTypes.bool, }; const defaultProps = { tag: 'div', defaultClassName: 'sui-prj-project-instances-quickview__item', className: '', timeRange: 0, isFailed: false, }; export const ProjectInstancesQuickviewItem = ({ tag: Tag, defaultClassName, className, timeRange, start, end, isFailed, ...attributes }) => { const classes = classnames( defaultClassName, className, isFailed ? 'as--failed' : '', ); const [percentageLeftWidth, setPercentageLeftWidth] = useState(0); useEffect(() => { const duration = end - start; const percentageLeft = (start / timeRange) * 100; const percentageWidth = (duration / timeRange) * 100; if (duration < 0) { setPercentageLeftWidth({ percentageLeft, percentageWidth: 0, }); return; } setPercentageLeftWidth({ percentageLeft, percentageWidth, }); }, [start, end]); return ( <Tag {...attributes} className={classes} style={{ left: `${percentageLeftWidth.percentageLeft}%`, width: `${percentageLeftWidth.percentageWidth}%`, }} /> ); }; ProjectInstancesQuickviewItem.propTypes = propTypes; ProjectInstancesQuickviewItem.defaultProps = defaultProps;