英文:
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.
I want the following:
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 '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.
I want the following:
I was able to get it to work by overriding the the dropZoneText prop as follows:
<DropzoneArea
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.
答案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(() => {
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'>Choose file or grag it here</div>\n" +
"<div class='new_drop_zone_description'>Supports .xls or .xlsx formats</div>\n" +
"<div class='new_drop_zone_button_wrapper'>" +
"<div class='new_drop_zone_button'>Add File</div>" +
'</div>' +
'</div>';
}
}, []);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论