Skip to content

Commit 6f3e0a0

Browse files
Merge pull request #1876 from carbon-design-system/fix-enforce-maxsize
fix(imagecard): enforce maxsize on upload
2 parents 8c713f5 + 3770515 commit 6f3e0a0

6 files changed

Lines changed: 29 additions & 7 deletions

File tree

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ module.exports = {
8484
functions: 50,
8585
},
8686
'./src/components/ImageCard/ImageUploader.jsx': {
87-
statements: 54,
87+
statements: 52,
8888
branches: 39,
89-
lines: 56,
89+
lines: 53,
9090
functions: 26,
9191
},
9292
'./src/components/ImageCard/ImageControls.jsx': { branches: 66 },

src/components/ImageCard/ImageCard.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ const defaultProps = {
4646
browseImages: 'Add from gallery',
4747
insertUrl: 'Insert from URL',
4848
urlInput: 'Type or insert URL',
49-
errorTitle: 'Error: ',
49+
errorTitle: 'Upload error: ',
50+
fileTooLarge: 'Image file is too large',
5051
},
5152
locale: 'en',
5253
content: {},
54+
maxFileSizeInBytes: 1048576,
5355
accept: null,
5456
onUpload: () => {},
5557
onBrowseClick: null,
@@ -68,6 +70,7 @@ const ImageCard = ({
6870
isResizable,
6971
error,
7072
isLoading,
73+
maxFileSizeInBytes,
7174
i18n: { loadingDataLabel, ...otherLabels },
7275
renderIconByName,
7376
locale,
@@ -142,6 +145,7 @@ const ImageCard = ({
142145
onBrowseClick={onBrowseClick}
143146
width={width}
144147
height={height}
148+
maxFileSizeInBytes={maxFileSizeInBytes}
145149
onUpload={handleOnUpload}
146150
i18n={pick(
147151
otherLabels,
@@ -151,7 +155,9 @@ const ImageCard = ({
151155
'uploadByURLButton',
152156
'browseImages',
153157
'insertUrl',
154-
'urlInput'
158+
'urlInput',
159+
'fileTooLarge',
160+
'errorTitle'
155161
)}
156162
hasInsertFromUrl={hasInsertFromUrl}
157163
/>

src/components/ImageCard/ImageUploader.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const i18nDefaults = {
2323
browseImages: 'Add from gallery',
2424
insertUrl: 'Insert from URL',
2525
urlInput: 'Type or insert URL',
26-
errorTitle: 'Error: ',
26+
errorTitle: 'Upload error: ',
27+
fileTooLarge: 'Image file is too large',
2728
wrongFileType: (accept) =>
2829
`This file is not one of the accepted file types, ${accept.join(', ')}`,
2930
};
@@ -32,6 +33,8 @@ const propTypes = {
3233
accept: PropTypes.arrayOf(PropTypes.string),
3334
onBrowseClick: PropTypes.func,
3435
onUpload: PropTypes.func,
36+
/** the maximum file size in bytes */
37+
maxFileSizeInBytes: PropTypes.number,
3538
hasInsertFromUrl: PropTypes.bool,
3639
i18n: PropTypes.shape({
3740
dropContainerLabelText: PropTypes.string,
@@ -41,11 +44,13 @@ const propTypes = {
4144
browseImages: PropTypes.string,
4245
insertUrl: PropTypes.string,
4346
urlInput: PropTypes.string,
47+
fileTooLarge: PropTypes.string,
4448
}),
4549
};
4650

4751
const defaultProps = {
4852
accept: ['APNG', 'AVIF', 'GIF', 'JPEG', 'JPG', 'PNG', 'WebP'],
53+
maxFileSizeInBytes: 1048576,
4954
onBrowseClick: () => {},
5055
onUpload: () => {},
5156
hasInsertFromUrl: false,
@@ -58,6 +63,7 @@ const ImageUploader = ({
5863
i18n,
5964
onUpload,
6065
accept,
66+
maxFileSizeInBytes,
6167
...other
6268
}) => {
6369
const [fromURL, setFromURL] = useState(false);
@@ -96,7 +102,9 @@ const ImageUploader = ({
96102

97103
const handleOnChange = (_event, files) => {
98104
const acceptedFiles = accept.map((i) => i.toLowerCase());
99-
if (
105+
if (files.addedFiles[0].size > maxFileSizeInBytes) {
106+
setError(i18n.fileTooLarge);
107+
} else if (
100108
acceptedFiles.includes(
101109
files.addedFiles[0].name
102110
.match(/([^/.]*?)(?=\?|#|$)/ || [])[0]

src/components/ImageCard/_image-uploader.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
.#{$prefix}--inline-notification {
7979
margin: auto;
80+
margin-top: 1rem;
8081
}
8182
}
8283

src/constants/CardPropTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ export const ImageCardPropTypes = {
443443
values: PropTypes.shape({
444444
hotspots: PropTypes.array,
445445
}),
446+
/** the maximum supported file size in bytes */
447+
maxFileSizeInBytes: PropTypes.number,
446448
onUpload: PropTypes.func,
447449
onBrowseClick: PropTypes.func,
448450
accept: PropTypes.arrayOf(PropTypes.string),

src/utils/__tests__/__snapshots__/publicAPI.test.js.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12745,14 +12745,16 @@ Map {
1274512745
"browseImages": "Add from gallery",
1274612746
"dropContainerDescText": "Max file size is 1MB. Supported file types are: APNG, AVIF, GIF, JPEG, PNG, WebP",
1274712747
"dropContainerLabelText": "Drag file here or click to upload file",
12748-
"errorTitle": "Error: ",
12748+
"errorTitle": "Upload error: ",
12749+
"fileTooLarge": "Image file is too large",
1274912750
"insertUrl": "Insert from URL",
1275012751
"loadingDataLabel": "Loading hotspot data",
1275112752
"uploadByURLButton": "OK",
1275212753
"uploadByURLCancel": "Cancel",
1275312754
"urlInput": "Type or insert URL",
1275412755
},
1275512756
"locale": "en",
12757+
"maxFileSizeInBytes": 1048576,
1275612758
"onBrowseClick": null,
1275712759
"onUpload": [Function],
1275812760
},
@@ -13399,6 +13401,9 @@ Map {
1339913401
"locale": Object {
1340013402
"type": "string",
1340113403
},
13404+
"maxFileSizeInBytes": Object {
13405+
"type": "number",
13406+
},
1340213407
"onBlur": Object {
1340313408
"type": "func",
1340413409
},

0 commit comments

Comments
 (0)