Skip to content

Commit 96bfaa3

Browse files
committed
Added upload remove + review comments
1 parent 128677e commit 96bfaa3

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ vercel.jsontsconfig.json
55
jest.config.ts
66
.storybook
77
.github
8-
*DS_Store
8+
*DS_Store
9+
*.stories.tsx
10+
*.test.tsx

src/lib/ImageUpload/ImageUpload.scss

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@
4545
}
4646

4747
.file-name {
48-
padding: 0.5rem 2rem;
48+
display: flex;
49+
flex-direction: row;
50+
align-items: center;
51+
justify-content: space-between;
52+
gap: 0.5rem;
53+
padding: 0.5rem 1.5rem;
4954
background: $color__n-90;
5055
border: 1px solid $color__n-60;
5156
border-top: 0;
@@ -59,6 +64,15 @@
5964
text-overflow: ellipsis;
6065
white-space: nowrap;
6166
}
67+
68+
button {
69+
padding: 0;
70+
background: transparent;
71+
border: 0;
72+
height: 24px;
73+
width: 24px;
74+
cursor: pointer;
75+
}
6276
}
6377
}
6478

src/lib/ImageUpload/ImageUpload.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ describe("========== C4 IMAGE UPLOAD - RUNNING TESTS ==========", () => {
117117

118118
test("Triggers correct drag and drop event logic on invalid uploads", async () => {
119119
const fileCache = new WeakMap();
120-
const onImageSelected = jest.fn();
121120
// Set accepted type to png only but provide a jpeg to make it invalid
122121
render(<ImageUpload {...defaultArgs} accept="image/png" />);
123122
const input: HTMLInputElement = screen.getByAltText(defaultAltText);

src/lib/ImageUpload/ImageUpload.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ChangeEvent, DragEvent, useCallback, useEffect, useRef, useState } from "react";
1+
import React, { ChangeEvent, DragEvent, useEffect, useRef, useState } from "react";
22

33
import "./ImageUpload.scss";
44
import { Icon } from "../Icon";
@@ -34,7 +34,6 @@ export const ImageUpload = ({
3434
hasUploaded = false
3535
}: ImageUploadProps) => {
3636
const inputRef = useRef<HTMLInputElement>(null);
37-
const labelRef = useRef<HTMLLabelElement>(null);
3837
const maxSizeInBytes = maxSize * 1024 * 1024;
3938
const [dragActive, setDragActive] = useState(false);
4039
const [uploadedImages, setUploadedImages] = useState<File[]>([]);
@@ -51,7 +50,14 @@ export const ImageUpload = ({
5150
onImageSelected(undefined);
5251
}
5352
if (inputRef.current) {
54-
inputRef.current.files = null;
53+
/**
54+
* Simply assigning inputRef.current.files to null
55+
* was causing an issue where uploading the same image that was cleared
56+
* was not working. To resolve it, we're passing an empty file list
57+
* instead.
58+
*/
59+
const dt = new DataTransfer();
60+
inputRef.current.files = dt.files;
5561
}
5662
}
5763

@@ -206,6 +212,13 @@ export const ImageUpload = ({
206212
</div>
207213
{uploadedImages && uploadedImages.length > 0 && <div className="file-name">
208214
<p>{uploadedImages[0].name}</p>
215+
<button
216+
type="button"
217+
aria-label="Remove upload"
218+
onClick={clearInput}
219+
>
220+
<Icon color="#ff427b" name="x" size="medium" />
221+
</button>
209222
</div>}
210223
</div>}
211224
<div className='c4imgupload'>

0 commit comments

Comments
 (0)