自定义material-ui-dropzone的DropZoneArea

huangapple go评论54阅读模式
英文:

Customize DropZoneArea for material-ui-dropzone

问题

以下是您要翻译的部分:

I would like to customize the look of the DropZoneArea besides the simple dropZoneText and Icon props. I have successfully done it, but I get the following react warning Failed prop type: Invalid prop 'dropzoneText' of type 'object' supplied to 'DropzoneAreaBase', expected string.

I understand what the issue is, but I was wondering if anyone knew how to accurately customize it.

Default looks like:
自定义material-ui-dropzone的DropZoneArea

I want the following:

自定义material-ui-dropzone的DropZoneArea

I was able to get it to work by overriding the the dropZoneText prop as follows:

  filesLimit={1}
  showAlerts={false}
  classes={{ root: clsx([classes.dropZoneRoot, uploading ? classes.disableClick : null]) }}
  showPreviewsInDropzone={false}
  showFileNames={false}
  dropzoneClass={classes.dropZone}
  Icon={''}
  dropzoneText={
    <Box className={classes.customDropZone} px={16} py={6} display="flex" flexDirection="column" justifyContent="center" alignItems="center" gridGap={4}>
      {uploading && (
        <Box className={classes.customDropZoneLoader} display="flex" flexDirection="column" justifyContent="center" alignItems="center">
          <CircularProgress size="2rem" />
        </Box>
      )}
      <Typography variant="subtitle1">Drop your file here</Typography>
      <Typography>or</Typography>
      <Box mt={1}>
        <Button color="primary" variant="outlined" style={{ width: 125 }}>
          Select File
        </Button>
      </Box>
      <Box mt={1}>
        <Typography>
          Accepted file types: <br />
          <strong>.png, .jpg, .gif, .txt, .csv, .doc, .docx, .xls, .xlsx, .pdf</strong>
        </Typography>
      </Box>
      <Box mt={1}>
        <Typography>
          Maximum file size: <strong>20MB</strong>
        </Typography>
      </Box>
    </Box>
  }
  maxFileSize={20971520}
  acceptedFiles={[
    'application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, image/jpeg, image/png, application/txt, text/tab-separated-values, text/csv, application/csv, text/x-csv, application/x-csv, text/comma-separated-values, text/x-comma-separated-values'
  ]}
  onChange={onFileDropped}
  onDropRejected={onFileRejected}
>                    
</DropzoneArea>

Does anyone know of a better way to do this and not get the warning? For what it's worth, it works perfectly fine, but just throws the warning in the console.

英文:

I would like to customize the look of the DropZoneArea besides the simple dropZoneText and Icon props. I have successfully done it, but I get the following react warning Failed prop type: Invalid prop &#39;dropzoneText&#39; of type &#39;object&#39; supplied to &#39;DropzoneAreaBase&#39;, expected string.

I understand what the issue is, but I was wondering if anyone knew how to accurately customize it.

Default looks like:
自定义material-ui-dropzone的DropZoneArea

I want the following:

自定义material-ui-dropzone的DropZoneArea

I was able to get it to work by overriding the the dropZoneText prop as follows:

&lt;DropzoneArea
  filesLimit={1}
  showAlerts={false}
  classes={{ root: clsx([classes.dropZoneRoot, uploading ? classes.disableClick : null]) }}
  showPreviewsInDropzone={false}
  showFileNames={false}
  dropzoneClass={classes.dropZone}
  Icon={&#39;&#39;}
  dropzoneText={
    &lt;Box className={classes.customDropZone} px={16} py={6} display=&quot;flex&quot; flexDirection=&quot;column&quot; justifyContent=&quot;center&quot; alignItems=&quot;center&quot; gridGap={4}&gt;
      {uploading &amp;&amp; (
        &lt;Box className={classes.customDropZoneLoader} display=&quot;flex&quot; flexDirection=&quot;column&quot; justifyContent=&quot;center&quot; alignItems=&quot;center&quot;&gt;
          &lt;CircularProgress size=&quot;2rem&quot; /&gt;
        &lt;/Box&gt;
      )}
      &lt;Typography variant=&quot;subtitle1&quot;&gt;Drop your file here&lt;/Typography&gt;
      &lt;Typography&gt;or&lt;/Typography&gt;
      &lt;Box mt={1}&gt;
        &lt;Button color=&quot;primary&quot; variant=&quot;outlined&quot; style={{ width: 125 }}&gt;
          Select File
        &lt;/Button&gt;
      &lt;/Box&gt;
      &lt;Box mt={1}&gt;
        &lt;Typography&gt;
          Accepted file types: &lt;br /&gt;
          &lt;strong&gt;.png, .jpg, .gif, .txt, .csv, .doc, .docx, .xls, .xlsx, .pdf&lt;/strong&gt;
        &lt;/Typography&gt;
      &lt;/Box&gt;
      &lt;Box mt={1}&gt;
        &lt;Typography&gt;
          Maximum file size: &lt;strong&gt;20MB&lt;/strong&gt;
        &lt;/Typography&gt;
      &lt;/Box&gt;
    &lt;/Box&gt;
  }
  maxFileSize={20971520}
  acceptedFiles={[
    &#39;application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, image/jpeg, image/png, application/txt, text/tab-separated-values, text/csv, application/csv, text/x-csv, application/x-csv, text/comma-separated-values, text/x-comma-separated-values&#39;
  ]}
  onChange={onFileDropped}
  onDropRejected={onFileRejected}
&gt;                    
&lt;/DropzoneArea&gt;

Does anyone know of a better way to do this and not get the warning? For what it's worth, it works perfectly fine, but just throws the warning in the console.

答案1

得分: 1

很遗憾,看起来该存储库的维护者无法修复它。https://github.com/Yuvaleros/material-ui-dropzone/issues/35 - 这是一个相当旧的存储库,所以我怀疑它会被修复。

如果你在使用Typescript,你可以将<Box>包装器强制转换为字符串,让DropzoneArea组件正常工作,但问题在于库本身没有使用对象/ReactNode作为预期类型。

如果你真的想尝试修复它,你可以使用npm包像patch-package在你的项目中本地修复它。https://www.npmjs.com/package/patch-package

英文:

Unfortunately, it seems the maintainer of that repository is not capable of fixing it. https://github.com/Yuvaleros/material-ui-dropzone/issues/35 - It's a rather old repository so I doubt it would get fixed.

If you were using Typescript, you could cast the <Box> wrapper as a string to make the DropzoneArea component happy, but the issue is with the library itself not using object / ReactNode as an expected type.

If you really want to try and fix that, you could use an npm package like patch-package to fix it locally for your project. https://www.npmjs.com/package/patch-package

答案2

得分: 0

我已经自定义如下希望这对某人有所帮助
将此添加到您添加了DropzoneArea的组件中

     useEffect(() => {
        const container = document.getElementsByClassName(
          'dropZone',
        ) as HTMLCollectionOf<HTMLElement>;
        if (container[0]?.children[0]) {
          container[0].children[0].innerHTML =
            "<div class='new_drop_zone_wrapper'>\n" +
            "<div class='new_drop_zone_title'>选择文件或拖放至此</div>\n" +
            "<div class='new_drop_zone_description'>支持 .xls 或 .xlsx 格式</div>\n" +
            "<div class='new_drop_zone_button_wrapper'>" +
            "<div class='new_drop_zone_button'>添加文件</div>" +
            '</div>' +
            '</div>';
        }
      }, []);
英文:

I have customised like the following. Hope this will help someone.
(Add this to the component where you have added DropzoneArea)

 useEffect(() =&gt; {
    const container = document.getElementsByClassName(
      &#39;dropZone&#39;,
    ) as HTMLCollectionOf&lt;HTMLElement&gt;;
    if (container[0]?.children[0]) {
      container[0].children[0].innerHTML =
        &quot;&lt;div class=&#39;new_drop_zone_wrapper&#39;&gt;\n&quot; +
        &quot;&lt;div class=&#39;new_drop_zone_title&#39;&gt;Choose file or grag it here&lt;/div&gt;\n&quot; +
        &quot;&lt;div class=&#39;new_drop_zone_description&#39;&gt;Supports .xls or .xlsx formats&lt;/div&gt;\n&quot; +
        &quot;&lt;div class=&#39;new_drop_zone_button_wrapper&#39;&gt;&quot; +
        &quot;&lt;div class=&#39;new_drop_zone_button&#39;&gt;Add File&lt;/div&gt;&quot; +
        &#39;&lt;/div&gt;&#39; +
        &#39;&lt;/div&gt;&#39;;
    }
  }, []);

huangapple
  • 本文由 发表于 2023年2月7日 03:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定