Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Custom DialogTitle to @comet/admin #2609

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/curvy-bulldogs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@comet/admin": minor
---

Add new custom `Dialog`

- `title` props allows setting DialogTitle
- optional `onClose` prop adds Close Button to the Component

Example:

- `<Dialog title="Dialog Title" onClose={() => {...}} />`
106 changes: 106 additions & 0 deletions packages/admin/admin/src/common/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Close } from "@comet/admin-icons";
import {
ComponentsOverrides,
css,
Dialog as MuiDialog,
DialogProps as MuiDialogProps,
DialogTitle as MuiDialogTitle,
IconButton as MuiIconButton,
Theme,
useThemeProps,
} from "@mui/material";
import { ReactNode } from "react";

import { createComponentSlot } from "../helpers/createComponentSlot";
import { ThemedComponentBaseProps } from "../helpers/ThemedComponentBaseProps";

export type DialogClassKey = "root" | "iconButton" | "titleWrapper" | "dialogTitle";

export type DialogProps = ThemedComponentBaseProps<{
iconButton: typeof MuiIconButton;
root: typeof MuiDialog;
dialogTitle: typeof MuiDialogTitle;
titleWrapper: "div";
}> & {
children?: ReactNode;
title?: ReactNode;
onClose?: () => void;
iconMapping?: {
closeIcon?: ReactNode;
};
} & MuiDialogProps;

export function Dialog(inProps: DialogProps) {
const {
slotProps,
open = false,
title,
iconMapping = {},
children,
onClose,
...restProps
} = useThemeProps({ props: inProps, name: "CometAdminDialog" });
const { closeIcon = <Close color="inherit" /> } = iconMapping;
return (
<Root open={open} {...slotProps?.root} {...restProps}>
<DialogTitle {...slotProps?.dialogTitle}>
<TitleWrapper {...slotProps?.titleWrapper}>{title}</TitleWrapper>
{onClose && (
<IconButton {...slotProps?.iconButton} onClick={onClose}>
{closeIcon}
</IconButton>
)}
</DialogTitle>
{children}
</Root>
);
}

const Root = createComponentSlot(MuiDialog)<DialogClassKey>({
componentName: "Dialog",
slotName: "root",
})();

const DialogTitle = createComponentSlot(MuiDialogTitle)<DialogClassKey>({
componentName: "Dialog",
slotName: "dialogTitle",
})(css`
display: flex;
align-items: center;
`);

const IconButton = createComponentSlot(MuiIconButton)<DialogClassKey>({
componentName: "Dialog",
slotName: "iconButton",
})(css`
color: inherit;
position: absolute;
right: 20px;
`);

const TitleWrapper = createComponentSlot("div")<DialogClassKey>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically a div is not allowed inside h2.

Suggested change
const TitleWrapper = createComponentSlot("div")<DialogClassKey>({
const TitleWrapper = createComponentSlot("span")<DialogClassKey>({

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't the styles of TitleWrapper just be part of DialogTitle directly? 🤔

componentName: "Dialog",
slotName: "titleWrapper",
})(css`
color: inherit;
width: 100%;
Comment on lines +85 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be the default when using a div.

Suggested change
color: inherit;
width: 100%;

padding-right: 40px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This padding should only be added when there is a close-button, otherwise the spacings left and right of the text are inconsistent.

min-height: 20px;
`);

declare module "@mui/material/styles" {
interface ComponentsPropsList {
CometAdminDialog: DialogProps;
}

interface ComponentNameToClassKey {
CometAdminDialog: DialogClassKey;
}

interface Components {
CometAdminDialog?: {
defaultProps?: Partial<ComponentsPropsList["CometAdminDialog"]>;
styleOverrides?: ComponentsOverrides<Theme>["CometAdminDialog"];
};
}
}
1 change: 1 addition & 0 deletions packages/admin/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { SplitButtonContext, SplitButtonContextOptions } from "./common/buttons/
export { useSplitButtonContext } from "./common/buttons/split/useSplitButtonContext";
export { ClearInputAdornment, ClearInputAdornmentProps } from "./common/ClearInputAdornment";
export { CometLogo } from "./common/CometLogo";
export { Dialog, DialogClassKey, DialogProps } from "./common/Dialog";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add an import restriction to prevent using MUIs Dialog.
This is how it's done for Alert for example:

{
name: "@mui/material",
importNames: ["Alert"],
message: "Please use Alert from @comet/admin instead",
},

export { FieldSet, FieldSetClassKey, FieldSetProps } from "./common/FieldSet";
export { HoverActions, HoverActionsClassKey, HoverActionsProps } from "./common/HoverActions";
export { Loading, LoadingProps } from "./common/Loading";
Expand Down
30 changes: 20 additions & 10 deletions storybook/src/admin/mui/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CancelButton, Field, FinalFormCheckbox, FinalFormInput, FinalFormSelect, OkayButton } from "@comet/admin";
import { CancelButton, Dialog, Field, FinalFormCheckbox, FinalFormInput, FinalFormSelect, OkayButton } from "@comet/admin";
import { Save } from "@comet/admin-icons";
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogProps, DialogTitle, FormControlLabel, MenuItem } from "@mui/material";
import { Button, DialogActions, DialogContent, DialogContentText, DialogProps, FormControlLabel, MenuItem } from "@mui/material";
import { select } from "@storybook/addon-knobs";
import { storiesOf } from "@storybook/react";
import * as React from "react";
Expand All @@ -19,6 +19,16 @@ const dialogSizeOptions: DialogSizeOptions = {
FullWidth: "fullWidth",
};

const dialogTitleOptions = {
"Text title": "Dialog Title Example",
None: "",
};

const dialogOnCloseOptions = {
"no callback": null,
"provided Callback": "callback",
};

const selectOptions = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
Expand All @@ -27,6 +37,8 @@ const selectOptions = [

function Story() {
const selectedDialogSize = select("Dialog size", dialogSizeOptions, "sm");
const selectedDialogTitle = select("Dialog Title", dialogTitleOptions, "Dialog Title Example");
const selectedDialogOnClose = select("Dialog onClose", dialogOnCloseOptions, "no callback");

return (
<div>
Expand All @@ -39,6 +51,8 @@ function Story() {
<form onSubmit={handleSubmit}>
<Dialog
scroll="body"
onClose={selectedDialogOnClose === "callback" ? () => console.log("Dialog closed") : undefined}
title={selectedDialogTitle}
open={true}
fullWidth={selectedDialogSize === "fullWidth"}
maxWidth={selectedDialogSize !== "fullWidth" && selectedDialogSize}
Expand All @@ -56,20 +70,16 @@ storiesOf("@comet/admin/mui", module).add("Dialog", () => <Story />);

function ConfirmationDialogContent(): React.ReactElement {
return (
<>
<DialogTitle>This is a small confirmation dialog.</DialogTitle>
<DialogActions>
<CancelButton />
<OkayButton />
</DialogActions>
</>
<DialogActions>
<CancelButton />
<OkayButton />
</DialogActions>
);
}

function DefaultDialogContent(): React.ReactElement {
return (
<>
<DialogTitle>Form in Dialog</DialogTitle>
<DialogContent>
<DialogContentText>
Lorem ipsum nullam quis risus eget urna mollis ornare vel eu leo. Nullam id dolor id nibh ultricies vehicula ut id elit. Vivamus
Expand Down
Loading