-
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.
Merge pull request #135 from ECN/enhancement-lastupdated-styling
Enhancement lastUpdated cell styling
- Loading branch information
Showing
9 changed files
with
235 additions
and
20 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
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,37 @@ | ||
import React from 'react' | ||
import PropTypes from "prop-types"; | ||
import { makeStyles, TableCell, useTheme } from '@material-ui/core' | ||
|
||
export default function ItemTableCell({ children, TableCellProps }) { | ||
const theme = useTheme(); | ||
const useStyles = makeStyles({ | ||
columnBorders: { | ||
// Add column borders | ||
borderLeftWidth: "1px", | ||
borderLeftStyle: "solid", | ||
borderColor: theme.palette.type === "light" ? theme.palette.grey[300] : theme.palette.grey[500] | ||
}, | ||
}) | ||
|
||
const classes = useStyles(); | ||
return ( | ||
<TableCell | ||
classes={{ root: classes.columnBorders }} | ||
{...TableCellProps} | ||
> | ||
{children} | ||
</TableCell> | ||
); | ||
} | ||
|
||
ItemTableCell.propTypes = { | ||
/** Child object passed to display cell data. */ | ||
"children": PropTypes.object, | ||
/** Props applied to the TableCell component. */ | ||
"TableCellProps": PropTypes.object | ||
}; | ||
|
||
ItemTableCell.defaultProps = { | ||
"children": {}, | ||
"TableCellProps": {}, | ||
}; |
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,38 @@ | ||
The ItemTableCell wraps an [MUI TableCell](https://material-ui.com/api/table-cell/) and adds styling. | ||
|
||
## Default Usage | ||
```jsx | ||
import { Paper } from '@material-ui/core'; | ||
import ItemTableCell from "./ItemTableCell"; | ||
|
||
<Paper> | ||
<ItemTableCell> | ||
Hello, moto! | ||
</ItemTableCell> | ||
</Paper> | ||
``` | ||
|
||
```jsx static | ||
<Paper> | ||
<ItemTableCell> | ||
Hello, moto! | ||
</ItemTableCell> | ||
</Paper> | ||
``` | ||
|
||
## Forwarded TableCell Props | ||
Props can be passed to the TableCell component using the TableCellProps prop. | ||
```jsx | ||
import { Paper } from '@material-ui/core'; | ||
import ItemTableCell from "./ItemTableCell"; | ||
|
||
<ItemTableCell TableCellProps={{ component: Paper, variant: "footer" }}> | ||
Hello, moto! | ||
</ItemTableCell> | ||
``` | ||
|
||
```jsx static | ||
<ItemTableCell TableCellProps={{ component: Paper, variant: "footer" }}> | ||
Hello, moto! | ||
</ItemTableCell> | ||
``` |
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,3 @@ | ||
import ItemTableCell from "./ItemTableCell"; | ||
|
||
export default ItemTableCell; |
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 from 'react' | ||
import PropTypes from "prop-types"; | ||
import { red } from '@material-ui/core/colors'; | ||
import RelativeTime from 'react-relative-time'; | ||
import ItemTableCell from '../ItemTableCell'; | ||
|
||
/** | ||
* Returns a color showing how old an item is. | ||
* @param {string} time ISO 8601 formatted time string. | ||
* @example | ||
* // Returns "#e57373" | ||
* timeToBackgroundColor("2021-01-04T11:47:00-0500") | ||
* @returns {string} Hex color code. | ||
*/ | ||
const timeToBackgroundColor = (time) => { | ||
const lastUpdated = new Date(time).getTime(); | ||
const now = new Date().getTime(); | ||
const timeDelta = now - lastUpdated; | ||
|
||
const day = 24 * 60 * 60 * 1000; | ||
const week = day * 7; | ||
const month = week * 4; | ||
|
||
let backgroundColor = "white"; | ||
|
||
// 1-6 days old | ||
if (timeDelta > day && timeDelta <= week) { | ||
backgroundColor = red[100]; | ||
} | ||
// 7-28 days old | ||
else if (timeDelta > week && timeDelta <= month) { | ||
backgroundColor = red[300]; | ||
} | ||
// 29+ days old | ||
else if (timeDelta > month) { | ||
backgroundColor = red[500]; | ||
} | ||
|
||
return backgroundColor; | ||
} | ||
|
||
export default function LastUpdatedCell({ time, ItemTableCellProps }) { | ||
// Insert the calculated background color into props so it isn't overriden. | ||
// Inspired by: https://github.com/mui-org/material-ui/issues/19479 | ||
ItemTableCellProps = { | ||
...ItemTableCellProps, | ||
style: { | ||
...ItemTableCellProps.style, | ||
backgroundColor: timeToBackgroundColor(time) | ||
} | ||
}; | ||
|
||
return ( | ||
<ItemTableCell TableCellProps={ItemTableCellProps}> | ||
<RelativeTime value={time} /> | ||
</ItemTableCell> | ||
); | ||
}; | ||
|
||
LastUpdatedCell.propTypes = { | ||
/** ISO 8601 formatted time string, Date object or UNIX time. See: https://www.npmjs.com/package/react-relative-time */ | ||
"time": PropTypes.string.isRequired, | ||
/** Props to be applied to the ItemTableCell. */ | ||
"ItemTableCellProps": PropTypes.object, | ||
}; | ||
|
||
LastUpdatedCell.defaultProps = { | ||
"ItemTableCellProps": {}, | ||
}; |
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,38 @@ | ||
A table cell that takes a time value and returns a relative time with a background color to indicate how old an item is. | ||
|
||
The LastUpdatedCell wraps an [ItemTableCell](/#/Components/ItemTableCell) | ||
|
||
## Default Usage | ||
```jsx | ||
import { Paper } from '@material-ui/core'; | ||
import LastUpdatedCell from "./LastUpdatedCell"; | ||
|
||
let today = new Date(); | ||
let threeDaysAgo = new Date().setDate(today.getDate() - 3); | ||
let lastWeek = new Date().setDate(today.getDate() - 8); | ||
let lastMonth = new Date().setDate(today.getDate() - 28); | ||
|
||
<Paper> | ||
{ /* Today */ } | ||
<LastUpdatedCell time={today} /> | ||
{ /* Three Days Ago */ } | ||
<LastUpdatedCell time={threeDaysAgo} /> | ||
{ /* Last Week */ } | ||
<LastUpdatedCell time={lastWeek} /> | ||
{ /* Last Month */ } | ||
<LastUpdatedCell time={lastMonth} /> | ||
</Paper> | ||
``` | ||
|
||
```jsx static | ||
<Paper> | ||
{ /* Today */ } | ||
<LastUpdatedCell time={today} /> | ||
{ /* Three Days Ago */ } | ||
<LastUpdatedCell time={threeDaysAgo} /> | ||
{ /* Last Week */ } | ||
<LastUpdatedCell time={lastWeek} /> | ||
{ /* Last Month */ } | ||
<LastUpdatedCell time={lastMonth} /> | ||
</Paper> | ||
``` |
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,3 @@ | ||
import LastUpdatedCell from "./LastUpdatedCell"; | ||
|
||
export default LastUpdatedCell; |
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