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

Migrate Layout components to typescript #5183

Merged
merged 4 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ ReferenceArrayField.defaultProps = {
addLabel: true,
};

interface ReferenceArrayFieldProps extends FieldProps, InjectedFieldProps {
export interface ReferenceArrayFieldProps
extends FieldProps,
InjectedFieldProps {
children: ReactElement;
classes?: Partial<ClassNameMap<ReferenceArrayFieldClassKey>>;
filter?: Filter;
Expand All @@ -147,9 +149,9 @@ const useStyles = makeStyles(
{ name: 'RaReferenceArrayField' }
);

type ReferenceArrayFieldClassKey = 'progress';
export type ReferenceArrayFieldClassKey = 'progress';
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved

interface ReferenceArrayFieldViewProps
export interface ReferenceArrayFieldViewProps
extends Omit<
ReferenceArrayFieldProps,
'basePath' | 'resource' | 'page' | 'perPage'
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ ReferenceField.defaultProps = {
link: 'edit',
};

interface ReferenceFieldProps extends FieldProps, InjectedFieldProps {
export interface ReferenceFieldProps extends FieldProps, InjectedFieldProps {
children: ReactElement;
classes?: Partial<ClassNameMap<ReferenceFieldClassKey>>;
reference: string;
Expand Down Expand Up @@ -237,9 +237,9 @@ ReferenceFieldView.propTypes = {
translateChoice: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
};

type ReferenceFieldClassKey = 'link';
export type ReferenceFieldClassKey = 'link';
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved

interface ReferenceFieldViewProps
export interface ReferenceFieldViewProps
extends FieldProps,
InjectedFieldProps,
UseReferenceProps {
Expand Down
28 changes: 17 additions & 11 deletions packages/ra-ui-materialui/src/input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ const convertStringToNumber = value => {
return isNaN(float) ? null : float;
};

interface Props {
step?: string | number;
min?: string | number;
max?: string | number;
}

/**
* An Input component for a number
*
Expand All @@ -31,11 +25,7 @@ interface Props {
*
* The object passed as `options` props is passed to the material-ui <TextField> component
*/
const NumberInput: FunctionComponent<
Props &
InputProps<TextFieldProps> &
Omit<TextFieldProps, 'label' | 'helperText'>
> = ({
const NumberInput: FunctionComponent<NumberInputProps> = ({
format,
helperText,
label,
Expand Down Expand Up @@ -118,4 +108,20 @@ NumberInput.defaultProps = {
textAlign: 'right',
};

export interface NumberInputProps
extends InputProps<TextFieldProps>,
Omit<
TextFieldProps,
| 'label'
| 'helperText'
| 'onChange'
| 'onBlur'
| 'onFocus'
| 'defaultValue'
> {
step?: string | number;
min?: string | number;
max?: string | number;
}

export default NumberInput;
27 changes: 14 additions & 13 deletions packages/ra-ui-materialui/src/input/ReferenceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import LinearProgress from '../layout/LinearProgress';
import Labeled from './Labeled';
import ReferenceError from './ReferenceError';

interface Props {
allowEmpty: boolean;
basePath: string;
children: ReactElement;
classes: any;
className: string;
label: string;
reference: string;
resource: string;
[key: string]: any;
}
/**
* An Input component for choosing a reference record. Useful for foreign keys.
*
Expand Down Expand Up @@ -110,7 +99,7 @@ interface Props {
* <AutocompleteInput optionText="title" />
* </ReferenceInput>
*/
const ReferenceInput: FunctionComponent<Props & InputProps> = ({
const ReferenceInput: FunctionComponent<ReferenceInputProps> = ({
format,
onBlur,
onChange,
Expand Down Expand Up @@ -165,6 +154,18 @@ ReferenceInput.defaultProps = {
sort: { field: 'id', order: 'DESC' },
};

export interface ReferenceInputProps extends InputProps {
allowEmpty: boolean;
basePath: string;
children: ReactElement;
classes: any;
className: string;
label: string;
reference: string;
resource: string;
[key: string]: any;
}

const sanitizeRestProps = ({
choices,
className,
Expand All @@ -184,7 +185,7 @@ const sanitizeRestProps = ({
...rest
}: any) => sanitizeInputProps(rest);

interface ReferenceInputViewProps {
export interface ReferenceInputViewProps {
allowEmpty?: boolean;
basePath: string;
children: ReactElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
Tooltip,
Typography,
useMediaQuery,
Theme,
} from '@material-ui/core';
import { AppBarProps as MuiAppBarProps } from '@material-ui/core/AppBar';

import { makeStyles } from '@material-ui/core/styles';
import MenuIcon from '@material-ui/icons/Menu';
Expand All @@ -19,6 +21,7 @@ import { toggleSidebar, useTranslate } from 'ra-core';
import LoadingIndicator from './LoadingIndicator';
import DefaultUserMenu from './UserMenu';
import HideOnScroll from './HideOnScroll';
import { ClassesOverride } from '../types';

const useStyles = makeStyles(
theme => ({
Expand Down Expand Up @@ -80,13 +83,12 @@ const useStyles = makeStyles(
* );
*};
*/
const AppBar = props => {
const AppBar = (props: AppBarProps): JSX.Element => {
const {
children,
classes: classesOverride,
className,
color = 'secondary',
logo,
logout,
open,
title,
Expand All @@ -95,7 +97,9 @@ const AppBar = props => {
} = props;
const classes = useStyles(props);
const dispatch = useDispatch();
const isXSmall = useMediaQuery(theme => theme.breakpoints.down('xs'));
const isXSmall = useMediaQuery<Theme>(theme =>
theme.breakpoints.down('xs')
);
const translate = useTranslate();

return (
Expand Down Expand Up @@ -151,6 +155,7 @@ const AppBar = props => {

AppBar.propTypes = {
children: PropTypes.node,
// @ts-ignore
classes: PropTypes.object,
className: PropTypes.string,
color: PropTypes.oneOf([
Expand All @@ -169,4 +174,12 @@ AppBar.defaultProps = {
userMenu: <DefaultUserMenu />,
};

export interface AppBarProps extends Omit<MuiAppBarProps, 'title' | 'classes'> {
classes?: ClassesOverride<typeof useStyles>;
logout?: JSX.Element;
open?: boolean;
title?: string | JSX.Element;
userMenu?: JSX.Element;
}

export default AppBar;
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from 'react';
import { ReactNode } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import CardContent from '@material-ui/core/CardContent';
import { makeStyles } from '@material-ui/core/styles';

import { ClassesOverride } from '../types';

const useStyles = makeStyles(
theme => ({
root: {
Expand All @@ -30,7 +33,7 @@ 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 CardContentInner = (props: CardContentInnerProps): JSX.Element => {
const { className, children } = props;
const classes = useStyles(props);
return (
Expand All @@ -46,4 +49,10 @@ CardContentInner.propTypes = {
children: PropTypes.node,
};

export interface CardContentInnerProps {
className?: string;
children: ReactNode;
classes?: ClassesOverride<typeof useStyles>;
}

export default CardContentInner;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { createMuiTheme } from '@material-ui/core/styles';
* <MyResponsiveComponent />
* <DeviceTestWrapper>
*/
const DeviceTestWrapper = ({ width = 'md', children }) => {
const DeviceTestWrapper = ({
width = 'md',
children,
}: DeviceTestWrapperProps): JSX.Element => {
const theme = createMuiTheme();

// Use https://github.com/ericf/css-mediaquery as ponyfill.
Expand All @@ -37,4 +40,9 @@ const DeviceTestWrapper = ({ width = 'md', children }) => {
);
};

export interface DeviceTestWrapperProps {
width: 'md' | 'xs' | 'sm' | 'lg' | 'xl';
children: JSX.Element;
}

export default DeviceTestWrapper;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Fragment } from 'react';
import { Fragment, HtmlHTMLAttributes, ErrorInfo } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Button from '@material-ui/core/Button';
Expand All @@ -10,9 +10,10 @@ import { makeStyles } from '@material-ui/core/styles';
import ErrorIcon from '@material-ui/icons/Report';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import History from '@material-ui/icons/History';
import { useTranslate } from 'ra-core';

import Title, { TitlePropType } from './Title';
import { useTranslate } from 'ra-core';
import { ClassesOverride } from '../types';

const useStyles = makeStyles(
theme => ({
Expand Down Expand Up @@ -53,7 +54,7 @@ function goBack() {
window.history.go(-1);
}

const Error = props => {
const Error = (props: ErrorProps): JSX.Element => {
const {
error,
errorInfo,
Expand Down Expand Up @@ -89,7 +90,7 @@ const Error = props => {
<div className={classes.toolbar}>
<Button
variant="contained"
icon={<History />}
startIcon={<History />}
onClick={goBack}
>
{translate('ra.action.back')}
Expand All @@ -108,4 +109,11 @@ Error.propTypes = {
title: TitlePropType,
};

export interface ErrorProps extends HtmlHTMLAttributes<HTMLDivElement> {
classes?: ClassesOverride<typeof useStyles>;
className?: string;
error: any;
errorInfo?: ErrorInfo;
title?: string;
}
export default Error;
Loading