quintodrome/ui/src/common/StarButton.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
1.7 KiB
JavaScript
Raw Normal View History

import React, { useCallback } from 'react'
2020-08-22 19:41:25 -08:00
import PropTypes from 'prop-types'
import StarIcon from '@material-ui/icons/Star'
import StarBorderIcon from '@material-ui/icons/StarBorder'
import IconButton from '@material-ui/core/IconButton'
import { makeStyles } from '@material-ui/core/styles'
import { useToggleStar } from './useToggleStar'
2020-08-22 19:41:25 -08:00
const useStyles = makeStyles({
star: {
color: (props) => props.color,
visibility: (props) =>
props.visible === false
? 'hidden'
: props.starred
? 'visible'
: 'inherit',
2020-08-22 19:41:25 -08:00
},
})
2020-11-13 17:11:24 -09:00
export const StarButton = ({
resource,
record,
color,
visible,
size,
component: Button,
addLabel,
disabled,
2020-11-13 17:11:24 -09:00
...rest
}) => {
2020-08-22 19:41:25 -08:00
const classes = useStyles({ color, visible, starred: record.starred })
const [toggleStar, loading] = useToggleStar(resource, record)
2020-08-22 19:41:25 -08:00
const handleToggleStar = useCallback(
(e) => {
e.preventDefault()
toggleStar()
e.stopPropagation()
},
[toggleStar]
)
2020-08-22 19:41:25 -08:00
return (
2020-11-13 17:11:24 -09:00
<Button
2020-08-22 19:41:25 -08:00
onClick={handleToggleStar}
size={'small'}
disabled={disabled || loading}
2020-08-22 19:41:25 -08:00
className={classes.star}
2020-11-13 17:11:24 -09:00
{...rest}
2020-08-22 19:41:25 -08:00
>
{record.starred ? (
<StarIcon fontSize={size} />
) : (
<StarBorderIcon fontSize={size} />
)}
2020-11-13 17:11:24 -09:00
</Button>
2020-08-22 19:41:25 -08:00
)
}
StarButton.propTypes = {
resource: PropTypes.string.isRequired,
record: PropTypes.object.isRequired,
visible: PropTypes.bool,
color: PropTypes.string,
size: PropTypes.string,
2020-11-13 17:11:24 -09:00
component: PropTypes.object,
disabled: PropTypes.bool,
2020-08-22 19:41:25 -08:00
}
StarButton.defaultProps = {
2020-08-25 15:07:51 -08:00
addLabel: true,
record: {},
2020-08-22 19:41:25 -08:00
visible: true,
size: 'small',
2020-08-24 15:38:29 -08:00
color: 'inherit',
2020-11-13 17:11:24 -09:00
component: IconButton,
disabled: false,
2020-08-22 19:41:25 -08:00
}