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

products GridList to typescript #4870

Merged
merged 1 commit into from
May 28, 2020
Merged
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
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 = 20 }) => {
const LoadingGridList: FC<GridProps & { nbItems?: number }> = ({
width,
nbItems = 20,
}) => {
const classes = useStyles();
return (
<div className={classes.root}>
Expand All @@ -64,8 +69,11 @@ const LoadingGridList = ({ width, nbItems = 20 }) => {
);
};

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
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);