Reputation: 2065
When I change the image width and height using styles only the column width changes. The image size is not affected.
Any ideas?
With no styles
With styles
Repro CodeSandbox (https://codesandbox.io/embed/54r440jp8k)
import React from "react";
import { Datagrid, ImageField, List, TextField } from "react-admin"; // eslint-disable-line import/no-unresolved
import { withStyles } from "material-ui/styles";
const styles = {
image: {
width: "20px",
height: "20px"
}
};
export const PostList = withStyles(styles)(({ classes, ...props }) => (
<List {...props}>
<Datagrid>
<ImageField source="image.url" className={classes.image} />
<TextField source="id" />
<TextField source="title" />
</Datagrid>
</List>
));
Using
Upvotes: 2
Views: 2659
Reputation: 2065
Building off Shawn K's post (thanks for the direction!)...
I believe this is a complete component (I've tested the classes override from the <List />
component and it works. This also follows react-admin
and material-ui
docs (and standards). However, I am still quite new to this so please reply with any corrections and I will update this.
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import classNames from "classnames";
import Avatar from "@material-ui/core/Avatar";
const styles = {
img: {
width: 36,
height: 36
},
root: {}
};
const ListAvatar = ({ className, record, source, classes }) => {
return (
<Avatar
src={record[source]}
className={classNames(classes.root, classes.img, className)}
/>
);
};
ListAvatar.propTypes = {
label: PropTypes.string,
record: PropTypes.object,
source: PropTypes.string.isRequired,
classes: PropTypes.object.isRequired,
className: PropTypes.string
};
export default withStyles(styles)(ListAvatar);
Upvotes: 1
Reputation: 778
You may need a custom component to do something like that. What about something like this:
ListAvatar.js
import React from 'react';
import Avatar from '@material-ui/core/Avatar';
const ListAvatar = ({ record, size }) => (
<Avatar
src={`${record.url.image}?size=${size}x${size}`}
size={size}
style={{
width: size,
height: size
}}
/>
);
ListAvatar.defaultProps = {
size: 30,
};
export default ListAvatar;
PostList.js
import React from "react";
import { Datagrid, List, TextField } from "react-admin"; // eslint-disable-line import/no-unresolved
import ListAvatar from './ListAvatar.js'
import { withStyles } from "material-ui/styles";
export const PostList = ({ classes, ...props }) => (
<List {...props}>
<Datagrid>
<ListAvatar />
<TextField source="id" />
<TextField source="title" />
</Datagrid>
</List>
);
Upvotes: 4