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

SimpleList Skeleton for First Load #4292

Merged
merged 2 commits into from
Jan 15, 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
17 changes: 1 addition & 16 deletions packages/ra-ui-materialui/src/list/DatagridLoading.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,7 @@ import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import IconButton from '@material-ui/core/IconButton';
import Checkbox from '@material-ui/core/Checkbox';
import classnames from 'classnames';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(
theme => ({
root: {
backgroundColor: theme.palette.grey[300],
display: 'flex',
},
}),
{ name: 'RaDatagridLoading' }
);

const Placeholder = ({ classes: classesOverride }) => {
const classes = useStyles({ classes: classesOverride });
return <div className={classes.root}>&nbsp;</div>;
};
import Placeholder from './Placeholder';

const times = (nbChildren, fn) =>
Array.from({ length: nbChildren }, (_, key) => fn(key));
Expand Down
25 changes: 25 additions & 0 deletions packages/ra-ui-materialui/src/list/Placeholder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { FC } from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

you should export this component in index.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

import { makeStyles } from '@material-ui/core/styles';
import classnames from 'classnames';

const useStyles = makeStyles(
theme => ({
root: {
backgroundColor: theme.palette.grey[300],
display: 'flex',
},
}),
{ name: 'RaSimpleListLoading' }
djhi marked this conversation as resolved.
Show resolved Hide resolved
);

interface Props {
className?: string;
classes?: Record<'root', string>;
}

const Placeholder: FC<Props> = ({ className, classes: classesOverride }) => {
const classes = useStyles({ classes: classesOverride });
return <div className={classnames(className, classes.root)}>&nbsp;</div>;
};

export default Placeholder;
15 changes: 15 additions & 0 deletions packages/ra-ui-materialui/src/list/SimpleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ListItemText from '@material-ui/core/ListItemText';
import { makeStyles } from '@material-ui/core/styles';
import { Link } from 'react-router-dom';
import { linkToRecord, sanitizeListRestProps } from 'ra-core';
import SimpleListLoading from './SimpleListLoading';

const useStyles = makeStyles(
{
Expand Down Expand Up @@ -53,6 +54,7 @@ const SimpleList = ({
data,
hasBulkActions,
ids,
loaded,
loading,
leftAvatar,
leftIcon,
Expand All @@ -68,6 +70,19 @@ const SimpleList = ({
...rest
}) => {
const classes = useStyles({ classes: classesOverride });

if (loaded === false) {
return (
<SimpleListLoading
classes={classes}
className={className}
hasLeftAvatarOrIcon={!!leftIcon || !!leftAvatar}
hasRightAvatarOrIcon={!!rightIcon || !!rightAvatar}
hasSecondaryText={!!secondaryText}
hasTertiaryText={!!tertiaryText}
/>
);
}
return (
(loading || total > 0) && (
<List className={className} {...sanitizeListRestProps(rest)}>
Expand Down
93 changes: 93 additions & 0 deletions packages/ra-ui-materialui/src/list/SimpleListLoading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, { FC } from 'react';
import PropTypes from 'prop-types';
import Avatar from '@material-ui/core/Avatar';
import List, { ListProps } from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemAvatar from '@material-ui/core/ListItemAvatar';
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction';
import ListItemText from '@material-ui/core/ListItemText';
import { makeStyles } from '@material-ui/core/styles';

import Placeholder from './Placeholder';

const useStyles = makeStyles(
theme => ({
primary: {
width: '30vw',
display: 'inline-block',
marginBottom: theme.spacing(),
},
tertiary: { float: 'right', opacity: 0.541176, minWidth: '10vw' },
}),
{ name: 'RaSimpleListLoading' }
);
const times = (nbChildren, fn) =>
Array.from({ length: nbChildren }, (_, key) => fn(key));

interface Props {
classes?: Record<'tertiary', string>;
className?: string;
hasLeftAvatarOrIcon?: boolean;
hasRightAvatarOrIcon?: boolean;
hasSecondaryText?: boolean;
hasTertiaryText?: boolean;
nbFakeLines?: number;
}

const SimpleListLoading: FC<Props & ListProps> = ({
classes: classesOverride,
className,
hasLeftAvatarOrIcon,
hasRightAvatarOrIcon,
hasSecondaryText,
hasTertiaryText,
nbFakeLines = 5,
...rest
}) => {
const classes = useStyles({ classes: classesOverride });

return (
<List className={className} {...rest}>
{times(nbFakeLines, key => (
<ListItem>
{hasLeftAvatarOrIcon && (
<ListItemAvatar>
<Avatar>&nbsp;</Avatar>
</ListItemAvatar>
)}
<ListItemText
primary={
<div>
<Placeholder className={classes.primary} />
{hasTertiaryText && (
<span className={classes.tertiary}>
<Placeholder />
</span>
)}
</div>
}
secondary={
hasSecondaryText ? <Placeholder /> : undefined
}
/>
{hasRightAvatarOrIcon && (
<ListItemSecondaryAction>
<Avatar>&nbsp;</Avatar>
</ListItemSecondaryAction>
)}
</ListItem>
))}
</List>
);
};

SimpleListLoading.propTypes = {
className: PropTypes.string,
hasLeftAvatarOrIcon: PropTypes.bool,
hasRightAvatarOrIcon: PropTypes.bool,
hasSecondaryText: PropTypes.bool,
hasTertiaryText: PropTypes.bool,
nbFakeLines: PropTypes.number,
};

export default SimpleListLoading;