From 0bfd389b19fc5284441ae81cd7685d07b1f64e4e Mon Sep 17 00:00:00 2001 From: NataliaVizintini <72875628+NataliaVizintini@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:43:33 +0100 Subject: [PATCH] Refactor theming of `FormSection` (#1604) --- packages/admin/admin/src/form/FormSection.tsx | 91 ++++++++++++------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/packages/admin/admin/src/form/FormSection.tsx b/packages/admin/admin/src/form/FormSection.tsx index 4201d1bd8f..130ea68c0b 100644 --- a/packages/admin/admin/src/form/FormSection.tsx +++ b/packages/admin/admin/src/form/FormSection.tsx @@ -1,54 +1,77 @@ -import { ComponentsOverrides, Theme, Typography } from "@mui/material"; -import { createStyles, WithStyles, withStyles } from "@mui/styles"; +import { ComponentsOverrides, Typography } from "@mui/material"; +import { css, styled, Theme, useThemeProps } from "@mui/material/styles"; +import { ThemedComponentBaseProps } from "helpers/ThemedComponentBaseProps"; import * as React from "react"; export type FormSectionClassKey = "root" | "disableMarginBottom" | "title" | "children"; -export interface FormSectionProps { +type OwnerState = Pick; + +const Root = styled("div", { + name: "CometAdminFormSection", + slot: "root", + overridesResolver({ ownerState }: { ownerState: OwnerState }, styles) { + return [styles.root, ownerState.disableMarginBottom && styles.disableMarginBottom]; + }, +})<{ ownerState: OwnerState }>( + ({ theme, ownerState }) => css` + ${!ownerState.disableMarginBottom && + css` + margin-bottom: ${theme.spacing(12)}; + `} + `, +); + +const Title = styled("div", { + name: "CometAdminFormSection", + slot: "title", + overridesResolver(_, styles) { + return [styles.title]; + }, +})( + ({ theme }) => css` + margin-bottom: ${theme.spacing(4)}; + `, +); + +const Children = styled("div", { + name: "CometAdminFormSection", + slot: "children", + overridesResolver(_, styles) { + return [styles.children]; + }, +})(); + +export interface FormSectionProps + extends ThemedComponentBaseProps<{ + root: "div"; + title: "div"; + children: "div"; + }> { children: React.ReactNode; title?: React.ReactNode; disableMarginBottom?: boolean; disableTypography?: boolean; } -const styles = ({ spacing }: Theme) => { - return createStyles({ - root: { - "&:not($disableMarginBottom)": { - marginBottom: spacing(12), - }, - }, - disableMarginBottom: {}, - title: { - marginBottom: spacing(4), - }, - children: {}, +export function FormSection(inProps: FormSectionProps) { + const { children, title, disableMarginBottom, disableTypography, slotProps, ...restProps } = useThemeProps({ + props: inProps, + name: "CometAdminFormSection", }); -}; -function Section({ - children, - title, - disableMarginBottom, - disableTypography, - classes, -}: FormSectionProps & WithStyles): React.ReactElement { - const rootClasses: string[] = [classes.root]; - - if (disableMarginBottom) { - rootClasses.push(classes.disableMarginBottom); - } + const ownerState: OwnerState = { + disableMarginBottom, + }; return ( -
- {title &&
{disableTypography ? title : {title}}
} -
{children}
-
+ + {title && {disableTypography ? title : <Typography variant="h3">{title}</Typography>}} + {children} + ); } -export const FormSection = withStyles(styles, { name: "CometAdminFormSection" })(Section); - declare module "@mui/material/styles" { interface ComponentNameToClassKey { CometAdminFormSection: FormSectionClassKey;