saleor-dashboard/src/components/SaveButtonBar/SaveButtonBar.tsx

154 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
2020-09-03 17:01:32 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-06-19 14:40:52 +00:00
import Portal from "@material-ui/core/Portal";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
import useWindowScroll from "@saleor/hooks/useWindowScroll";
import { buttonMessages } from "@saleor/intl";
2020-09-08 10:14:13 +00:00
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { maybe } from "../../misc";
import AppActionContext from "../AppLayout/AppActionContext";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "../ConfirmButton/ConfirmButton";
import Container from "../Container";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
2020-09-08 10:14:13 +00:00
applyShadow: {
"&$card": {
boxShadow: "0px 6px 30px rgba(0, 0, 0, 0.16)"
}
},
2019-12-03 15:28:40 +00:00
button: {
marginRight: theme.spacing(1)
},
cancelButton: {
marginRight: theme.spacing(2)
},
2020-09-08 10:14:13 +00:00
card: {
boxShadow: "0px 0px 0px rgba(0, 0, 0, 0.16)",
transition: theme.transitions.duration.shortest + "ms"
},
2020-09-03 17:01:32 +00:00
content: {
"&:last-child": {
paddingBottom: theme.spacing(2)
},
2019-12-03 15:28:40 +00:00
display: "flex",
paddingBottom: theme.spacing(2),
paddingTop: theme.spacing(2),
[theme.breakpoints.down("sm")]: {
marginTop: theme.spacing(1)
}
},
deleteButton: {
"&:hover": {
backgroundColor: theme.palette.error.dark
},
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
root: {
2020-09-03 17:01:32 +00:00
height: 120
2019-12-03 15:28:40 +00:00
},
spacer: {
flex: "1"
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "SaveButtonBar" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface SaveButtonBarProps {
2019-06-19 14:40:52 +00:00
disabled: boolean;
state: ConfirmButtonTransitionState;
labels?: {
cancel?: string;
delete?: string;
save?: string;
};
onCancel: () => void;
onDelete?: () => void;
onSave(event: any);
}
2019-10-30 14:34:24 +00:00
export const SaveButtonBar: React.FC<SaveButtonBarProps> = props => {
const {
2019-06-19 14:40:52 +00:00
disabled,
labels,
state,
onCancel,
onDelete,
onSave,
2019-10-30 14:34:24 +00:00
...rest
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const intl = useIntl();
const scrollPosition = useWindowScroll();
const scrolledToBottom =
2020-09-07 11:28:59 +00:00
scrollPosition.y + window.innerHeight - document.body.scrollHeight >= -5;
2019-10-30 14:34:24 +00:00
return (
<AppActionContext.Consumer>
{anchor =>
anchor ? (
<Portal container={anchor.current}>
2020-09-03 17:01:32 +00:00
<div className={classes.root} {...rest}>
<Container>
2020-09-08 10:14:13 +00:00
<Card
className={classNames(classes.card, {
[classes.applyShadow]: !scrolledToBottom
})}
>
2020-09-03 17:01:32 +00:00
<CardContent className={classes.content}>
{!!onDelete && (
<Button
variant="contained"
onClick={onDelete}
className={classes.deleteButton}
data-test="button-bar-delete"
>
{labels && labels.delete
? labels.delete
: intl.formatMessage(buttonMessages.delete)}
</Button>
)}
<div className={classes.spacer} />
<Button
className={classes.cancelButton}
variant="text"
onClick={onCancel}
data-test="button-bar-cancel"
>
{maybe(
() => labels.cancel,
intl.formatMessage(buttonMessages.back)
)}
</Button>
<ConfirmButton
disabled={disabled}
onClick={onSave}
transitionState={state}
data-test="button-bar-confirm"
>
{maybe(
() => labels.save,
intl.formatMessage(buttonMessages.save)
)}
</ConfirmButton>
</CardContent>
</Card>
2019-10-30 14:34:24 +00:00
</Container>
</div>
</Portal>
) : null
}
</AppActionContext.Consumer>
);
};
2019-06-19 14:40:52 +00:00
SaveButtonBar.displayName = "SaveButtonBar";
export default SaveButtonBar;