javascript - Styling preview grid in material-ui-dropzone - Stack Overflow

In my React app I have a form page for a book which has an option to upload a cover photo. I decided to

In my React app I have a form page for a book which has an option to upload a cover photo. I decided to use material-ui-dropzone (/) which works great, but I ran into one issue. The preview does not look very good when only one file is allowed.

I would like it to be centered and as big as possible, but I am really struggling. Relevant code:

const useStyles = makeStyles(() => ({
  dropZone: {
    height: '100%',
    fullWidth: 'true',
  },
  previewContainer: {
    container: 'true',
    width: '100%',
    height: '100%',
  },
  preview: {
    //width: '100%',
    //height: '100%',
    //item: 'true',
    xs: '12',
  },
  previewImg: {
    //height: '100%',
    //width: '100%',
  },
}));

const classes = useStyles();

<DropzoneArea
   acceptedFiles={['image/*']}
   dropzoneClass={classes.dropZone}
   previewGridClasses={{
      container: classes.previewContainer,
      item: classes.preview,
      image: classes.previewImg,
   }}
   dropzoneText={'Upload book cover image'}
   filesLimit={1}
   onChange={(files) => formikProps.setFieldValue('coverImage', files)}
   showAlerts={false}
/>

I have been trying a bunch of things, but so far the only way I even managed to get an actual visual effect was by adding some padding to the preview class. Any help would be very much appreciated.

In my React app I have a form page for a book which has an option to upload a cover photo. I decided to use material-ui-dropzone (https://yuvaleros.github.io/material-ui-dropzone/) which works great, but I ran into one issue. The preview does not look very good when only one file is allowed.

I would like it to be centered and as big as possible, but I am really struggling. Relevant code:

const useStyles = makeStyles(() => ({
  dropZone: {
    height: '100%',
    fullWidth: 'true',
  },
  previewContainer: {
    container: 'true',
    width: '100%',
    height: '100%',
  },
  preview: {
    //width: '100%',
    //height: '100%',
    //item: 'true',
    xs: '12',
  },
  previewImg: {
    //height: '100%',
    //width: '100%',
  },
}));

const classes = useStyles();

<DropzoneArea
   acceptedFiles={['image/*']}
   dropzoneClass={classes.dropZone}
   previewGridClasses={{
      container: classes.previewContainer,
      item: classes.preview,
      image: classes.previewImg,
   }}
   dropzoneText={'Upload book cover image'}
   filesLimit={1}
   onChange={(files) => formikProps.setFieldValue('coverImage', files)}
   showAlerts={false}
/>

I have been trying a bunch of things, but so far the only way I even managed to get an actual visual effect was by adding some padding to the preview class. Any help would be very much appreciated.

Share Improve this question asked Nov 11, 2020 at 16:28 Klaidas BespalovasKlaidas Bespalovas 1232 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I ended up resolving the issue after snooping around the source code of the ponent so I thought I would post the solution here in case someone runs into this in the future. As it turns out the dropzone ponent does not actually apply the item style to the image (even though the docs say it should). I did find a way around this, you can actually provide a custom getPreviewIcon function to the ponent and you can add your style there.

              <DropzoneArea
                acceptedFiles={['image/*']}
                dropzoneClass={classes.dropZone}
                previewGridClasses={{
                  item: classes.preview,
                }}
                getPreviewIcon={(file) => {
                  if (file.file.type.split('/')[0] === 'image')
                    return (
                      <img className={classes.previewImg} role="presentation" src={file.data} />
                    );
                }}
                dropzoneText={'Upload book cover image'}
                filesLimit={1}
                onChange={(files) => formikProps.setFieldValue('coverImage', files[0])}
                showAlerts={false}
              />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744872259a4598330.html

相关推荐

  • javascript - Styling preview grid in material-ui-dropzone - Stack Overflow

    In my React app I have a form page for a book which has an option to upload a cover photo. I decided to

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信