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

150 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import Portal from "@material-ui/core/Portal";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
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
2019-08-09 11:14:35 +00:00
import useWindowScroll from "@saleor/hooks/useWindowScroll";
import { buttonMessages } from "@saleor/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";
const styles = (theme: Theme) =>
createStyles({
button: {
marginRight: theme.spacing.unit
},
cancelButton: {
marginRight: theme.spacing.unit * 2
},
container: {
display: "flex",
paddingBottom: theme.spacing.unit * 2,
paddingTop: theme.spacing.unit * 2,
[theme.breakpoints.down("sm")]: {
marginTop: theme.spacing.unit
}
},
deleteButton: {
"&:hover": {
backgroundColor: theme.palette.error.dark
},
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
root: {
background: theme.palette.background.default,
borderTop: "1px solid transparent",
boxShadow: `0 -5px 5px 0 ${theme.overrides.MuiCard.root.borderColor}`,
2019-11-06 13:11:10 +00:00
height: "100%",
2019-06-19 14:40:52 +00:00
transition: `box-shadow ${theme.transitions.duration.shortest}ms`
},
spacer: {
flex: "1"
},
stop: {
"&$root": {
borderTopColor: theme.overrides.MuiCard.root.borderColor,
boxShadow: `0 0 0 0 ${theme.overrides.MuiCard.root.borderColor}`
}
}
});
interface SaveButtonBarProps extends WithStyles<typeof styles> {
disabled: boolean;
state: ConfirmButtonTransitionState;
labels?: {
cancel?: string;
delete?: string;
save?: string;
};
onCancel: () => void;
onDelete?: () => void;
onSave(event: any);
}
export const SaveButtonBar = withStyles(styles, { name: "SaveButtonBar" })(
({
classes,
disabled,
labels,
state,
onCancel,
onDelete,
onSave,
...props
}: SaveButtonBarProps) => {
const intl = useIntl();
2019-08-09 11:14:35 +00:00
const scrollPosition = useWindowScroll();
2019-06-19 14:40:52 +00:00
const scrolledToBottom =
scrollPosition.y + window.innerHeight >= document.body.scrollHeight;
return (
<AppActionContext.Consumer>
{anchor =>
anchor ? (
<Portal container={anchor.current}>
<div
className={classNames(classes.root, {
[classes.stop]: scrolledToBottom
})}
{...props}
>
<Container className={classes.container}>
{!!onDelete && (
<Button
variant="contained"
onClick={onDelete}
className={classes.deleteButton}
2019-08-27 13:29:00 +00:00
data-tc="button-bar-delete"
2019-06-19 14:40:52 +00:00
>
{labels && labels.delete
? labels.delete
: intl.formatMessage(buttonMessages.delete)}
2019-06-19 14:40:52 +00:00
</Button>
)}
<div className={classes.spacer} />
<Button
className={classes.cancelButton}
variant="text"
onClick={onCancel}
2019-08-27 13:29:00 +00:00
data-tc="button-bar-cancel"
2019-06-19 14:40:52 +00:00
>
{maybe(
() => labels.cancel,
2019-10-21 14:32:35 +00:00
intl.formatMessage(buttonMessages.back)
2019-06-19 14:40:52 +00:00
)}
</Button>
<ConfirmButton
disabled={disabled}
onClick={onSave}
transitionState={state}
2019-08-27 13:29:00 +00:00
data-tc="button-bar-confirm"
2019-06-19 14:40:52 +00:00
>
{maybe(
() => labels.save,
intl.formatMessage(buttonMessages.save)
2019-06-19 14:40:52 +00:00
)}
</ConfirmButton>
</Container>
</div>
</Portal>
) : null
}
</AppActionContext.Consumer>
);
}
);
SaveButtonBar.displayName = "SaveButtonBar";
export default SaveButtonBar;