Skip to content

Commit 4ffd43c

Browse files
committed
Transforming image accepted type to enum
1 parent 96bfaa3 commit 4ffd43c

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/lib/ImageUpload/ImageUpload.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { Meta, StoryObj } from "@storybook/react";
33
import { ImageUpload } from "./ImageUpload"
4+
import { ImageType } from "./ImageUpload.types";
45

56
const meta: Meta<typeof ImageUpload> = {
67
component: ImageUpload,
@@ -24,6 +25,6 @@ SampleComponent.parameters = {
2425
};
2526
SampleComponent.args = {
2627
id: "c4_img_uploader",
27-
accept: "image/png, image/jpeg",
28+
accept: [ImageType.png, ImageType.jpeg],
2829
maxSize: 2
2930
}

src/lib/ImageUpload/ImageUpload.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { act, fireEvent, render, screen } from "@testing-library/react";
44
import userEvent from "@testing-library/user-event";
55
import MockDataTransfer from "./_MockDataTransfer";
66
import "@testing-library/jest-dom";
7+
import { ImageType } from "./ImageUpload.types";
78

89

910
const defaultAltText = "Image uploader input";
1011
const defaultArgs = {
1112
id: 'test-uploader',
12-
accept: 'image/jpeg',
13+
accept: [ImageType.jpeg],
1314
multiSelect: false,
1415
maxSize: 2
1516
};
@@ -25,13 +26,13 @@ describe("========== C4 IMAGE UPLOAD - RUNNING TESTS ==========", () => {
2526
render(<ImageUpload {...defaultArgs} />);
2627
const input: HTMLInputElement = screen.getByAltText(defaultAltText);
2728
expect(input).toHaveAttribute('id', defaultArgs.id);
28-
expect(input).toHaveAttribute('accept', defaultArgs.accept);
29+
expect(input).toHaveAttribute('accept', defaultArgs.accept.join(", "));
2930
});
3031

3132
test("Triggers correct onChange event logic on valid uploads", async () => {
3233
const fileCache = new WeakMap();
3334
const user = userEvent.setup();
34-
render(<ImageUpload {...defaultArgs} accept="image/jpeg" />);
35+
render(<ImageUpload {...defaultArgs} accept={[ImageType.jpeg]} />);
3536
const input: HTMLInputElement = screen.getByAltText(defaultAltText);
3637
const file = new File(['(⌐□_□)'], "test.jpeg", { type: "image/jpeg" });
3738

@@ -118,7 +119,7 @@ describe("========== C4 IMAGE UPLOAD - RUNNING TESTS ==========", () => {
118119
test("Triggers correct drag and drop event logic on invalid uploads", async () => {
119120
const fileCache = new WeakMap();
120121
// Set accepted type to png only but provide a jpeg to make it invalid
121-
render(<ImageUpload {...defaultArgs} accept="image/png" />);
122+
render(<ImageUpload {...defaultArgs} accept={[ImageType.png]} />);
122123
const input: HTMLInputElement = screen.getByAltText(defaultAltText);
123124
const file = new File(['(⌐□_□)'], "test.jpeg", { type: "image/jpeg" });
124125

src/lib/ImageUpload/ImageUpload.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,34 @@ import React, { ChangeEvent, DragEvent, useEffect, useRef, useState } from "reac
33
import "./ImageUpload.scss";
44
import { Icon } from "../Icon";
55
import clsx from "clsx";
6-
import { ImageUploadProps } from "./ImageUpload.types";
6+
import { ImageType, ImageUploadProps } from "./ImageUpload.types";
77

88
/**
9-
* A custom image upload input component with support for drag & drop
10-
* as well as multi-file upload by providing the `multiSelect` prop.
9+
* A custom image upload input component with support for drag & drop.
1110
*
1211
* By default, this component can be used as an uncontrolled input and all uploaded files
1312
* can be retrieved on form submission. For further control however, you can pass an optional function
1413
* to the `onImageSelected` prop.
1514
*
16-
* @param accept - String value indicating all the accepted file types (separated by a comma). Options include:
17-
* - image/apng
18-
* - image/avif
15+
* @param accept - List of image types as provided by the `ImageType` enum. Available options include:
1916
* - image/gif
2017
* - image/jpeg
2118
* - image/png
22-
* - image/svg+xml
2319
* - image/webp
2420
* @param id - String HTML identifier for the input field.
2521
* @param maxSize - The max allowed size for file uploads (in Megabytes).
2622
* @param onImageSelected - Optional function for controlled handling of uploads. Triggered on every change to uploader.
2723
* @param hasUploaded - Boolean indicator to trigger image upload cleanup once the upload process has finalized on the frontend.
2824
*/
2925
export const ImageUpload = ({
30-
accept = "image/png, image/jpeg",
26+
accept = [ImageType.png, ImageType.jpeg],
3127
id,
3228
maxSize = 2,
3329
onImageSelected,
3430
hasUploaded = false
3531
}: ImageUploadProps) => {
3632
const inputRef = useRef<HTMLInputElement>(null);
33+
const acceptToStr = accept.join(", ")
3734
const maxSizeInBytes = maxSize * 1024 * 1024;
3835
const [dragActive, setDragActive] = useState(false);
3936
const [uploadedImages, setUploadedImages] = useState<File[]>([]);
@@ -71,9 +68,10 @@ export const ImageUpload = ({
7168
* @param acceptedPreviews - Array of previously generated preview urls for each accepted file.
7269
*/
7370
const isValidFile = (file: File | null, acceptedFiles: File[], declinedFiles: File[], acceptedPreviews: string[]) => {
71+
const acceptedTypes = new Set(accept);
7472
if (file == null) return false;
7573

76-
if (!accept.includes(file.type) || file.size > maxSizeInBytes) {
74+
if (!acceptedTypes.has(file.type as ImageType) || file.size > maxSizeInBytes) {
7775
declinedFiles.push(file);
7876
return false;
7977
} else {
@@ -243,7 +241,7 @@ export const ImageUpload = ({
243241
className='c4imgupload--input'
244242
multiple={false}
245243
type="file"
246-
accept={accept}
244+
accept={acceptToStr}
247245
onClick={() => setError(undefined)}
248246
onChange={onImageChanged}
249247
/>
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
export interface ImageUploadProps {
22
/**
3-
* String value indicating all the accepted file types (separated by a comma). Options include:
4-
* - image/apng
5-
* - image/avif
3+
* List of image types as provided by the `ImageType` enum. Available options include:
64
* - image/gif
75
* - image/jpeg
86
* - image/png
9-
* - image/svg+xml
107
* - image/webp
118
*/
12-
accept?: string;
9+
accept?: ImageType[];
1310
/** String HTML identifier for the input field. */
1411
id: string;
1512
/** The max allowed size for file uploads (in Megabytes). */
@@ -18,4 +15,11 @@ export interface ImageUploadProps {
1815
onImageSelected?: (data: File | undefined) => void;
1916
/** Boolean indicator to trigger image upload cleanup once the upload process has finalized on the frontend. */
2017
hasUploaded?: boolean;
18+
}
19+
20+
export enum ImageType {
21+
gif = "image/gif",
22+
jpeg = "image/jpeg",
23+
png = "image/png",
24+
webp = "image/webp"
2125
}

0 commit comments

Comments
 (0)