forked from samuellawerentz/style-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { Progress } from 'antd' | ||
import PropTypes from 'prop-types' | ||
|
||
export const TimeLeftBar = ({ | ||
duration, | ||
colorOne, | ||
colorTwo, | ||
colorThreshold, | ||
className, | ||
...props | ||
}) => { | ||
const [progressPercent, setProgressPercent] = useState(100) | ||
useEffect(() => { | ||
let startTime = new Date().getTime() | ||
|
||
const interval = setInterval(() => { | ||
const currentTime = new Date().getTime() | ||
const elapsedTime = currentTime - startTime | ||
|
||
if (elapsedTime >= duration) { | ||
clearInterval(interval) | ||
} | ||
// calculating the percentage of time left | ||
const percentage = 100 - (elapsedTime / duration) * 100 | ||
|
||
if (percentage < 0) clearInterval(interval) | ||
setProgressPercent(percentage) | ||
}, 100) // Update every 100 milliseconds | ||
|
||
return () => clearInterval(interval) | ||
}, []) | ||
|
||
let progressBarColor | ||
if (colorThreshold !== undefined) | ||
progressBarColor = progressPercent <= colorThreshold ? colorTwo : colorOne | ||
else progressBarColor = colorOne | ||
|
||
return ( | ||
<Progress | ||
className={className} | ||
percent={progressPercent} | ||
showInfo={false} | ||
strokeColor={`var(--${progressBarColor})`} | ||
reverse | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
TimeLeftBar.propTypes = { | ||
className: PropTypes.string, | ||
/** | ||
* This indicates how much time is left | ||
*/ | ||
duration: PropTypes.string, | ||
/** | ||
* This indicates initial color of the bar | ||
*/ | ||
colorOne: PropTypes.string, | ||
/** | ||
* This indicates color that changes after color one threshold | ||
*/ | ||
colorTwo: PropTypes.string, | ||
/** | ||
* This indicates when at what percentage the color one changes | ||
*/ | ||
colorThreshold: PropTypes.string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react' | ||
import { TimeLeftBar } from '.' | ||
|
||
export default { | ||
title: 'Components/TimeLeft Bar', | ||
component: TimeLeftBar, | ||
parameters: {}, | ||
} | ||
|
||
const Template = (args) => <TimeLeftBar {...args} /> | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = { | ||
duration: 20000, // total duration of bar | ||
colorOne: 'success-color', // Color of the bar is it's percentage is more than progressPercentage | ||
colorTwo: 'danger-color', // Color of the bar is it's percentage is less than progressPercentage | ||
colorThreshold: 25, // Percent at which the bar changes it's color | ||
} | ||
|
||
export const WithoutThreshold = Template.bind({}) | ||
WithoutThreshold.args = { | ||
duration: 20000, | ||
colorOne: 'primary-color', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters