-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
convert ra-ui-materialui layout to typescript #4875
Changes from 2 commits
bcf850e
11ac651
cf8e9e6
fab3749
4e2532f
e244a9c
726c377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import React from 'react'; | ||
import React, { FC } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import CardContent from '@material-ui/core/CardContent'; | ||
import CardContent, { CardContentProps } from '@material-ui/core/CardContent'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
const useStyles = makeStyles( | ||
|
@@ -30,11 +30,11 @@ const useStyles = makeStyles( | |
* padding double the spacing between each CardContent, leading to too much | ||
* wasted space. Use this component as a CardContent alternative. | ||
*/ | ||
const CardContentInner = props => { | ||
const { className, children } = props; | ||
const CardContentInner: FC<CardContentProps> = props => { | ||
const { className, children, ...rest } = props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary for now, may have side effects... Please revert this change. |
||
const classes = useStyles(props); | ||
return ( | ||
<CardContent className={classnames(classes.root, className)}> | ||
<CardContent className={classnames(classes.root, className)} {...rest}> | ||
{children} | ||
</CardContent> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { Fragment } from 'react'; | ||
import React, { Fragment, FC, ErrorInfo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import Button from '@material-ui/core/Button'; | ||
|
@@ -10,7 +10,7 @@ import ErrorIcon from '@material-ui/icons/Report'; | |
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import History from '@material-ui/icons/History'; | ||
|
||
import Title, { TitlePropType } from './Title'; | ||
import Title, { TitlePropType, TitleProps } from './Title'; | ||
import { useTranslate } from 'ra-core'; | ||
|
||
const useStyles = makeStyles( | ||
|
@@ -52,7 +52,15 @@ function goBack() { | |
window.history.go(-1); | ||
} | ||
|
||
const Error = props => { | ||
export interface ErrorComponentProps { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ErrocComponentProps => ErrorProps (we never include the name "component" in other interface names) |
||
className?: string; | ||
error: Error; | ||
errorInfo: ErrorInfo; | ||
title: TitleProps['defaultTitle']; | ||
classes?: object; | ||
} | ||
|
||
const Error: FC<ErrorComponentProps> = props => { | ||
const { | ||
error, | ||
errorInfo, | ||
|
@@ -86,12 +94,8 @@ const Error = props => { | |
</ExpansionPanel> | ||
)} | ||
<div className={classes.toolbar}> | ||
<Button | ||
variant="contained" | ||
icon={<History />} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
onClick={goBack} | ||
> | ||
{translate('ra.action.back')} | ||
<Button variant="contained" onClick={goBack}> | ||
<History /> {translate('ra.action.back')} | ||
</Button> | ||
</div> | ||
</div> | ||
|
@@ -102,8 +106,8 @@ const Error = props => { | |
Error.propTypes = { | ||
classes: PropTypes.object, | ||
className: PropTypes.string, | ||
error: PropTypes.object.isRequired, | ||
errorInfo: PropTypes.object, | ||
error: PropTypes.any.isRequired, | ||
errorInfo: PropTypes.any, | ||
title: TitlePropType, | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,37 @@ import React, { | |
useEffect, | ||
useRef, | ||
useState, | ||
ComponentType, | ||
ReactNode, | ||
ErrorInfo, | ||
FC, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import classnames from 'classnames'; | ||
import { withRouter } from 'react-router-dom'; | ||
import { withRouter, RouteComponentProps } from 'react-router-dom'; | ||
import { | ||
createMuiTheme, | ||
withStyles, | ||
createStyles, | ||
} from '@material-ui/core/styles'; | ||
import { ThemeProvider } from '@material-ui/styles'; | ||
import { ThemeProvider, ClassNameMap } from '@material-ui/styles'; | ||
import compose from 'recompose/compose'; | ||
|
||
import DefaultAppBar from './AppBar'; | ||
import DefaultSidebar from './Sidebar'; | ||
import DefaultMenu from './Menu'; | ||
import DefaultNotification from './Notification'; | ||
import DefaultError from './Error'; | ||
import DefaultMenu, { MenuProps } from './Menu'; | ||
import DefaultNotification, { NotificationProps } from './Notification'; | ||
import DefaultError, { ErrorComponentProps } from './Error'; | ||
import defaultTheme from '../defaultTheme'; | ||
import { ComponentPropType } from 'ra-core'; | ||
import { | ||
ComponentPropType, | ||
DashboardComponent, | ||
TitleComponent, | ||
ReduxState, | ||
} from 'ra-core'; | ||
import { DrawerProps } from '@material-ui/core'; | ||
import { ThemeOptions } from '@material-ui/core/styles/createMuiTheme'; | ||
|
||
const styles = theme => | ||
createStyles({ | ||
|
@@ -74,11 +85,59 @@ const sanitizeRestProps = ({ | |
location, | ||
match, | ||
...props | ||
}) => props; | ||
}: Partial<LayoutProps>): Omit< | ||
LayoutProps, | ||
keyof RouteComponentProps | 'title' | ||
> => props; | ||
|
||
interface LayoutProps extends RouteComponentProps { | ||
appBar?: ComponentType<any>; | ||
classes?: ClassNameMap; | ||
className?: string; | ||
customRoutes?: any[]; | ||
dashboard?: DashboardComponent; | ||
error?: ComponentType<ErrorComponentProps>; | ||
logout?: ReactNode; | ||
menu?: ComponentType<MenuProps>; | ||
notification?: ComponentType<NotificationProps>; | ||
open?: boolean; | ||
sidebar?: ComponentType<DrawerProps>; | ||
title: TitleComponent; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's optional at build time (but required at runtime). |
||
} | ||
|
||
class Layout extends Component { | ||
state = { hasError: false, errorMessage: null, errorInfo: null }; | ||
type LayoutState = { | ||
hasError?: boolean; | ||
errorMessage: Error | null; | ||
errorInfo: ErrorInfo | null; | ||
}; | ||
|
||
class Layout extends Component<LayoutProps, LayoutState> { | ||
static propTypes = { | ||
appBar: ComponentPropType, | ||
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | ||
classes: PropTypes.object, | ||
className: PropTypes.string, | ||
customRoutes: PropTypes.array, | ||
dashboard: ComponentPropType, | ||
error: ComponentPropType, | ||
history: PropTypes.object.isRequired, | ||
logout: PropTypes.element, | ||
menu: ComponentPropType, | ||
notification: ComponentPropType, | ||
open: PropTypes.bool, | ||
sidebar: ComponentPropType, | ||
title: PropTypes.node.isRequired, | ||
}; | ||
|
||
static defaultProps = { | ||
appBar: DefaultAppBar, | ||
error: DefaultError, | ||
menu: DefaultMenu, | ||
notification: DefaultNotification, | ||
sidebar: DefaultSidebar, | ||
}; | ||
|
||
state = { hasError: false, errorMessage: null, errorInfo: null }; | ||
constructor(props) { | ||
super(props); | ||
/** | ||
|
@@ -146,32 +205,7 @@ class Layout extends Component { | |
} | ||
} | ||
|
||
Layout.propTypes = { | ||
appBar: ComponentPropType, | ||
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | ||
classes: PropTypes.object, | ||
className: PropTypes.string, | ||
customRoutes: PropTypes.array, | ||
dashboard: ComponentPropType, | ||
error: ComponentPropType, | ||
history: PropTypes.object.isRequired, | ||
logout: PropTypes.element, | ||
menu: ComponentPropType, | ||
notification: ComponentPropType, | ||
open: PropTypes.bool, | ||
sidebar: ComponentPropType, | ||
title: PropTypes.node.isRequired, | ||
}; | ||
|
||
Layout.defaultProps = { | ||
appBar: DefaultAppBar, | ||
error: DefaultError, | ||
menu: DefaultMenu, | ||
notification: DefaultNotification, | ||
sidebar: DefaultSidebar, | ||
}; | ||
|
||
const mapStateToProps = state => ({ | ||
const mapStateToProps = (state: ReduxState) => ({ | ||
open: state.admin.ui.sidebarOpen, | ||
}); | ||
|
||
|
@@ -184,7 +218,14 @@ const EnhancedLayout = compose( | |
withStyles(styles, { name: 'RaLayout' }) | ||
)(Layout); | ||
|
||
const LayoutWithTheme = ({ theme: themeOverride, ...props }) => { | ||
interface LayoutWithThemeProps extends LayoutProps { | ||
theme?: ThemeOptions; | ||
} | ||
|
||
const LayoutWithTheme: FC<LayoutWithThemeProps> = ({ | ||
theme: themeOverride, | ||
...props | ||
}) => { | ||
const themeProp = useRef(themeOverride); | ||
const [theme, setTheme] = useState(createMuiTheme(themeOverride)); | ||
|
||
|
@@ -203,7 +244,7 @@ const LayoutWithTheme = ({ theme: themeOverride, ...props }) => { | |
}; | ||
|
||
LayoutWithTheme.propTypes = { | ||
theme: PropTypes.object, | ||
theme: PropTypes.any, | ||
}; | ||
|
||
LayoutWithTheme.defaultProps = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React from 'react'; | ||
import React, { FC } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import classnames from 'classnames'; | ||
|
@@ -33,7 +33,13 @@ const useStyles = makeStyles( | |
{ name: 'RaLoading' } | ||
); | ||
|
||
const Loading = props => { | ||
interface Props { | ||
className?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you miss one prop: |
||
loadingPrimary?: string; | ||
loadingSecondary?: string; | ||
} | ||
|
||
const Loading: FC<Props> = props => { | ||
const { | ||
className, | ||
loadingPrimary = 'ra.page.loading', | ||
|
@@ -53,7 +59,6 @@ const Loading = props => { | |
}; | ||
|
||
Loading.propTypes = { | ||
classes: PropTypes.object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't remove this one. When |
||
className: PropTypes.string, | ||
loadingPrimary: PropTypes.string, | ||
loadingSecondary: PropTypes.string, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't mean to push that with this PR but I checked everywhere there is no use for variant
title