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

Fix layout component type #5473

Merged
merged 4 commits into from
Nov 3, 2020
Merged
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
4 changes: 2 additions & 2 deletions examples/demo/src/layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import { useSelector } from 'react-redux';
import { Layout, Sidebar } from 'react-admin';
import { Layout, LayoutProps, Sidebar } from 'react-admin';
import AppBar from './AppBar';
import Menu from './Menu';
import { darkTheme, lightTheme } from './themes';
import { AppState } from '../types';

const CustomSidebar = (props: any) => <Sidebar {...props} size={200} />;

export default (props: any) => {
export default (props: LayoutProps) => {
const theme = useSelector((state: AppState) =>
state.theme === 'dark' ? darkTheme : lightTheme
);
Expand Down
17 changes: 8 additions & 9 deletions examples/demo/src/layout/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as React from 'react';
import { FC, useState } from 'react';
import { FC, useState, ReactNode } from 'react';
import { useSelector } from 'react-redux';
import SettingsIcon from '@material-ui/icons/Settings';
import LabelIcon from '@material-ui/icons/Label';
import { useMediaQuery, Theme, Box } from '@material-ui/core';
import { useTranslate, DashboardMenuItem, MenuItemLink } from 'react-admin';
import {
useTranslate,
DashboardMenuItem,
MenuItemLink,
MenuProps,
} from 'react-admin';

import visitors from '../visitors';
import orders from '../orders';
Expand All @@ -17,13 +22,7 @@ import { AppState } from '../types';

type MenuName = 'menuCatalog' | 'menuSales' | 'menuCustomers';

interface Props {
dense: boolean;
logout: () => void;
onMenuClick: () => void;
}

const Menu: FC<Props> = ({ onMenuClick, dense, logout }) => {
const Menu: FC<MenuProps> = ({ onMenuClick, logout, dense = false }) => {
const [state, setState] = useState({
menuCatalog: true,
menuSales: true,
Expand Down
3 changes: 2 additions & 1 deletion examples/demo/src/layout/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const darkTheme = {
secondary: {
main: '#FBBA72',
},
type: 'dark', // Switching the dark mode on is a single property value change.
type: 'dark' as 'dark', // Switching the dark mode on is a single property value change.
Copy link
Contributor

Choose a reason for hiding this comment

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

😱 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it mean users will have to cast this as well ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, that's because MUI defines the type type as 'dark' | 'light', and TypeScript is dumb. Nothing we can do to avoid that.

},
overrides: {
MuiAppBar: {
Expand All @@ -32,6 +32,7 @@ export const lightTheme = {
background: {
default: '#fcfcfe',
},
type: 'light' as 'light',
Copy link
Contributor

Choose a reason for hiding this comment

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

👀

},
shape: {
borderRadius: 10,
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/core/CoreAdminRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import {
CustomRoutes,
CatchAllComponent,
LayoutComponent,
LayoutProps,
CoreLayoutProps,
ResourceProps,
RenderResourcesFunction,
ResourceElement,
} from '../types';

export interface AdminRouterProps extends LayoutProps {
export interface AdminRouterProps extends CoreLayoutProps {
layout: LayoutComponent;
catchAll: CatchAllComponent;
children?: AdminChildren;
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/core/CoreAdminUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TitleComponent,
LoginComponent,
LayoutComponent,
LayoutProps,
CoreLayoutProps,
AdminChildren,
CatchAllComponent,
CustomRoutes,
Expand All @@ -16,7 +16,7 @@ import {

export type ChildrenFunction = () => ComponentType[];

const DefaultLayout: FunctionComponent<LayoutProps> = ({ children }) => (
const DefaultLayout: FunctionComponent<CoreLayoutProps> = ({ children }) => (
<>{children}</>
);

Expand Down
6 changes: 2 additions & 4 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,21 +386,19 @@ interface LoginComponentProps extends RouteComponentProps {
export type LoginComponent = ComponentType<LoginComponentProps>;
export type DashboardComponent = ComponentType<WithPermissionsChildrenParams>;

export interface LayoutProps {
appBar?: ComponentType;
export interface CoreLayoutProps {
children?: ReactNode;
dashboard?: DashboardComponent;
logout?: ReactNode;
menu?: ComponentType<{
logout?: ReactNode;
hasDashboard?: boolean;
}>;
sideBar?: ComponentType;
theme?: ThemeOptions;
title?: TitleComponent;
}

export type LayoutComponent = ComponentType<LayoutProps>;
export type LayoutComponent = ComponentType<CoreLayoutProps>;

export interface ResourceComponentInjectedProps {
basePath?: string;
Expand Down
82 changes: 32 additions & 50 deletions packages/ra-ui-materialui/src/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import React, {
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classnames from 'classnames';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import {
createMuiTheme,
withStyles,
createStyles,
} from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';
import { ThemeOptions } from '@material-ui/core';
import { ComponentPropType, CustomRoutes, LayoutProps } from 'ra-core';
import { ComponentPropType, CoreLayoutProps } from 'ra-core';
import compose from 'lodash/flowRight';

import DefaultAppBar from './AppBar';
import DefaultSidebar from './Sidebar';
import DefaultMenu from './Menu';
import DefaultMenu, { MenuProps } from './Menu';
import DefaultNotification from './Notification';
import DefaultError from './Error';
import defaultTheme from '../defaultTheme';
Expand Down Expand Up @@ -78,15 +78,10 @@ const styles = theme =>
},
});

const sanitizeRestProps = ({
staticContext,
history,
location,
match,
...props
}: RestProps) => props;

class Layout extends Component<MuiLayoutProps, LayoutState> {
class LayoutWithoutTheme extends Component<
LayoutWithoutThemeProps,
LayoutState
> {
state = { hasError: false, errorMessage: null, errorInfo: null };

constructor(props) {
Expand All @@ -113,7 +108,6 @@ class Layout extends Component<MuiLayoutProps, LayoutState> {
children,
classes,
className,
customRoutes,
error,
dashboard,
logout,
Expand All @@ -122,14 +116,19 @@ class Layout extends Component<MuiLayoutProps, LayoutState> {
open,
sidebar,
title,
// sanitize react-router props
match,
location,
history,
staticContext,
...props
} = this.props;
const { hasError, errorMessage, errorInfo } = this.state;
return (
<>
<div
className={classnames('layout', classes.root, className)}
{...sanitizeRestProps(props)}
{...props}
>
<div className={classes.appFrame}>
{createElement(appBar, { title, open, logout })}
Expand Down Expand Up @@ -162,7 +161,6 @@ class Layout extends Component<MuiLayoutProps, LayoutState> {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
classes: PropTypes.object,
className: PropTypes.string,
customRoutes: PropTypes.array,
dashboard: ComponentPropType,
error: ComponentPropType,
history: PropTypes.object.isRequired,
Expand All @@ -183,51 +181,39 @@ class Layout extends Component<MuiLayoutProps, LayoutState> {
};
}

export interface MuiLayoutProps
extends LayoutProps,
RouteComponentProps,
export interface LayoutProps
extends CoreLayoutProps,
Omit<HtmlHTMLAttributes<HTMLDivElement>, 'title'> {
className?: string;
classes?: any;
customRoutes?: CustomRoutes;
appBar?: ComponentType<{
title?: string | ReactElement<any>;
open?: boolean;
logout?: ReactNode;
}>;
sidebar?: ComponentType<{ children: JSX.Element }>;
classes?: any;
className?: string;
error?: ComponentType<{
error?: string;
errorInfo?: React.ErrorInfo;
title?: string | ReactElement<any>;
}>;
menu?: ComponentType<MenuProps>;
notification?: ComponentType;
open?: boolean;
sidebar?: ComponentType<{ children: JSX.Element }>;
theme?: ThemeOptions;
}

export type RestProps = Omit<
MuiLayoutProps,
| 'appBar'
| 'children'
| 'classes'
| 'className'
| 'customRoutes'
| 'error'
| 'dashboard'
| 'logout'
| 'menu'
| 'notification'
| 'open'
| 'sidebar'
| 'title'
>;

export interface LayoutState {
hasError: boolean;
errorMessage: string;
errorInfo: ErrorInfo;
}

interface LayoutWithoutThemeProps
extends RouteComponentProps,
Omit<LayoutProps, 'theme'> {
open?: boolean;
}

const mapStateToProps = state => ({
open: state.admin.ui.sidebarOpen,
});
Expand All @@ -239,12 +225,12 @@ const EnhancedLayout = compose(
),
withRouter,
withStyles(styles, { name: 'RaLayout' })
)(Layout);
)(LayoutWithoutTheme);

const LayoutWithTheme = ({
const Layout = ({
theme: themeOverride,
...props
}: LayoutWithThemeProps): JSX.Element => {
}: LayoutProps): JSX.Element => {
const themeProp = useRef(themeOverride);
const [theme, setTheme] = useState(createMuiTheme(themeOverride));

Expand All @@ -262,16 +248,12 @@ const LayoutWithTheme = ({
);
};

LayoutWithTheme.propTypes = {
Layout.propTypes = {
theme: PropTypes.object,
};

LayoutWithTheme.defaultProps = {
Layout.defaultProps = {
theme: defaultTheme,
};

interface LayoutWithThemeProps extends LayoutProps {
theme?: ThemeOptions;
}

export default LayoutWithTheme;
export default Layout;
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/layout/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { FC, ReactElement } from 'react';
import { FC, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { shallowEqual, useSelector } from 'react-redux';
// @ts-ignore
Expand Down Expand Up @@ -100,8 +100,8 @@ export interface MenuProps {
classes?: object;
className?: string;
dense?: boolean;
hasDashboard: boolean;
logout?: ReactElement;
hasDashboard?: boolean;
logout?: ReactNode;
onMenuClick?: () => void;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ra-ui-materialui/src/layout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DashboardMenuItem, { DashboardMenuItemProps } from './DashboardMenuItem';
import DeviceTestWrapper, { DeviceTestWrapperProps } from './DeviceTestWrapper';
import Error, { ErrorProps } from './Error';
import HideOnScroll, { HideOnScrollProps } from './HideOnScroll';
import Layout from './Layout';
import Layout, { LayoutProps } from './Layout';
import Loading from './Loading';
import LinearProgress from './LinearProgress';
import LoadingIndicator from './LoadingIndicator';
Expand Down Expand Up @@ -54,6 +54,7 @@ export type {
DeviceTestWrapperProps,
ErrorProps,
HideOnScrollProps,
LayoutProps,
MenuItemLinkProps,
MenuProps,
ResponsiveProps,
Expand Down