Rafe
Rafe

Reputation: 2065

How do I change the size of an image in an ImageField?

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 Screen Shot 2019-04-06 at 2 49 47 PM

With styles Screen Shot 2019-04-06 at 2 49 32 PM

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

Answers (2)

Rafe
Rafe

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

Reference docs

Upvotes: 1

Shawn K
Shawn K

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

Related Questions