-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from uptick/feature/relative-datetime
Add RelativeDateTime component
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import Moment from 'moment' | ||
import DatePart from './DatePart' | ||
import Empty from './Empty' | ||
import {SHORTDATETIME_FORMAT} from '../utils' | ||
|
||
/** | ||
* Displays a datetime relative to the current time eg. "4 days ago" or "in 3 hours" | ||
* alongside the datetime passed in | ||
*/ | ||
class RelativeDateTime extends React.Component { | ||
static propTypes = { | ||
value: PropTypes.string, | ||
} | ||
|
||
render() { | ||
const date = Moment(this.props.value) | ||
const isValid = date.isValid() | ||
if (isValid) { | ||
return ( | ||
<div> | ||
<div> | ||
{Moment(this.props.value).fromNow()} | ||
</div> | ||
<div className="text-muted"> | ||
<DatePart value={this.props.value} outputFormat={SHORTDATETIME_FORMAT} /> | ||
</div> | ||
</div> | ||
) | ||
} | ||
return (<Empty />) | ||
} | ||
} | ||
export default RelativeDateTime |
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