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

Convert products demo example into typescript #4758

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react';
import MuiGridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
import { makeStyles } from '@material-ui/core/styles';
import withWidth from '@material-ui/core/withWidth';
import { Link } from 'react-router-dom';
import { NumberField } from 'react-admin';
import withWidth, { WithWidth } from '@material-ui/core/withWidth';
import { linkToRecord } from 'ra-core';
import React, { FC } from 'react';
import { NumberField } from 'react-admin';
import { Link } from 'react-router-dom';
import { DatagridProps, Product } from '../types';
import { Breakpoint } from '@material-ui/core/styles/createBreakpoints';

const useStyles = makeStyles(theme => ({
root: {
Expand All @@ -33,18 +35,21 @@ const useStyles = makeStyles(theme => ({
},
}));

const getColsForWidth = width => {
const getColsForWidth = (width: Breakpoint) => {
if (width === 'xs') return 2;
if (width === 'sm') return 3;
if (width === 'md') return 4;
if (width === 'lg') return 5;
return 6;
};

const times = (nbChildren, fn) =>
const times = (nbChildren: number, fn: (key: number) => any) =>
Array.from({ length: nbChildren }, (_, key) => fn(key));

const LoadingGridList = ({ width, nbItems = 10 }) => {
const LoadingGridList: FC<GridProps & { nbItems?: number }> = ({
width,
nbItems = 10,
}) => {
const classes = useStyles();
return (
<div className={classes.root}>
Expand All @@ -64,8 +69,11 @@ const LoadingGridList = ({ width, nbItems = 10 }) => {
);
};

const LoadedGridList = ({ ids, data, basePath, width }) => {
const LoadedGridList: FC<GridProps> = ({ ids, data, basePath, width }) => {
const classes = useStyles();

if (!ids || !data) return null;

return (
<div className={classes.root}>
<MuiGridList
Expand All @@ -75,6 +83,7 @@ const LoadedGridList = ({ ids, data, basePath, width }) => {
>
{ids.map(id => (
<GridListTile
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

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

It seems another attempt was done at migrating this file (see #4592). Would you mind collaborating with the author of that PR instead of including GridList in your PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

component={Link}
key={id}
to={linkToRecord(basePath, data[id].id)}
Expand Down Expand Up @@ -106,7 +115,9 @@ const LoadedGridList = ({ ids, data, basePath, width }) => {
);
};

const GridList = ({ loaded, ...props }) =>
interface GridProps extends DatagridProps<Product>, WithWidth {}

const GridList: FC<GridProps> = ({ loaded, ...props }) =>
loaded ? <LoadedGridList {...props} /> : <LoadingGridList {...props} />;

export default withWidth()(GridList);
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import { makeStyles } from '@material-ui/core/styles';
import React, { FC } from 'react';
import { FieldProps, Product } from '../types';

const useStyles = makeStyles({
root: { display: 'inline-block', marginTop: '1em', zIndex: 2 },
Expand All @@ -14,8 +15,11 @@ const useStyles = makeStyles({
},
});

const Poster = ({ record }) => {
const Poster: FC<FieldProps<Product>> = ({ record }) => {
const classes = useStyles();

if (!record) return null;

return (
<Card className={classes.root}>
<CardContent className={classes.content}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { FC } from 'react';
import {
Create,
FormTab,
Expand All @@ -12,6 +12,7 @@ import {
import { InputAdornment } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import RichTextInput from 'ra-input-rich-text';
import { CreateComponentProps } from '../types';

export const styles = {
price: { width: '7em' },
Expand All @@ -24,7 +25,7 @@ export const styles = {

const useStyles = makeStyles(styles);

const ProductCreate = props => {
const ProductCreate: FC<CreateComponentProps> = props => {
const classes = useStyles();
return (
<Create {...props}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { FC } from 'react';
import {
Datagrid,
DateField,
Expand All @@ -22,22 +22,26 @@ import CustomerReferenceField from '../visitors/CustomerReferenceField';
import StarRatingField from '../reviews/StarRatingField';
import Poster from './Poster';
import { styles as createStyles } from './ProductCreate';
import { Product, EditComponentProps } from '../types';

const ProductTitle = ({ record }) => <span>Poster #{record.reference}</span>;
interface ProductTitleProps {
record?: Product;
}

const styles = {
const ProductTitle: FC<ProductTitleProps> = ({ record }) =>
record ? <span>Poster #{record.reference}</span> : null;

const useStyles = makeStyles({
...createStyles,
comment: {
maxWidth: '20em',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
};

const useStyles = makeStyles(styles);
});

const ProductEdit = props => {
const ProductEdit: FC<EditComponentProps> = props => {
const classes = useStyles();
return (
<Edit {...props} title={<ProductTitle />}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import React from 'react';
import Chip from '@material-ui/core/Chip';
import { makeStyles } from '@material-ui/core/styles';
import { InputProps } from 'ra-core';
import React, { FC } from 'react';
import {
Filter,
List,
Expand All @@ -9,8 +12,7 @@ import {
SelectInput,
useTranslate,
} from 'react-admin';
import Chip from '@material-ui/core/Chip';
import { makeStyles } from '@material-ui/core/styles';
import { FilterProps, ListComponentProps } from '../types';
import GridList from './GridList';

const useQuickFilterStyles = makeStyles(theme => ({
Expand All @@ -19,13 +21,23 @@ const useQuickFilterStyles = makeStyles(theme => ({
},
}));

const QuickFilter = ({ label }) => {
const QuickFilter: FC<InputProps> = ({ label }) => {
const translate = useTranslate();
const classes = useQuickFilterStyles();
return <Chip className={classes.root} label={translate(label)} />;
};

export const ProductFilter = props => (
interface FilterParams {
q?: string;
category_id?: string;
width_gte?: number;
width_lte?: number;
height_gte?: number;
height_lte?: number;
stock_lte?: number;
}

export const ProductFilter: FC<FilterProps<FilterParams>> = props => (
<Filter {...props}>
<SearchInput source="q" alwaysOn />
<ReferenceInput
Expand All @@ -47,7 +59,7 @@ export const ProductFilter = props => (
</Filter>
);

const ProductList = props => (
const ProductList: FC<ListComponentProps> = props => (
<List
{...props}
filters={<ProductFilter />}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import React, { FC } from 'react';
import { ReferenceField, TextField } from 'react-admin';
import { FieldProps } from './../types';

const ProductReferenceField = props => (
const ProductReferenceField: FC<FieldProps & { resource: string }> = props => (
MohammedFaragallah marked this conversation as resolved.
Show resolved Hide resolved
<ReferenceField
label="Product"
source="product_id"
Expand Down
8 changes: 8 additions & 0 deletions examples/demo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export interface ShowComponentProps<
id: string;
}

export interface CreateComponentProps<
Params extends ResourceMatch = { id: string },
C extends StaticContext = StaticContext,
S = H.LocationState
> extends ResourceComponentProps<Params, C, S> {
id: string;
}

declare global {
interface Window {
restServer: any;
Expand Down