Skip to content

Commit

Permalink
fix: add duplicate name checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-a-young committed Feb 18, 2025
1 parent 6ba6348 commit 971fbd0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/form-upload/src/FileList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import FileRow from './FileRow';
const FileList = ({ files, children, onRemoveFile, ...rest }) => {
const list = useMemo(
() =>
files.map((file) => (
<FileRow key={file.id} file={file} onRemove={onRemoveFile}>
files.map((file, index) => (
<FileRow key={file.id} file={file} onRemove={onRemoveFile} index={index}>
{children}
</FileRow>
)),
Expand Down
5 changes: 3 additions & 2 deletions packages/form-upload/src/FileRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const fileTypeIconMap = {
pdf: 'file-pdf',
};

const FileRow = ({ onRemove, children, file }) => {
const FileRow = ({ onRemove, children, file, index }) => {
const remove = () => {
onRemove(file.id);
onRemove(file.id, index);
};

const progressBar = () => <UploadProgressBar upload={file} />;
Expand Down Expand Up @@ -83,6 +83,7 @@ FileRow.propTypes = {
}).isRequired,
options: PropTypes.object,
}),
index: PropTypes.number,
};

export default FileRow;
30 changes: 26 additions & 4 deletions packages/form-upload/src/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const Upload = ({
setFieldError,
onDeliverySuccess,
onDeliveryError,
isCloud
isCloud,
]
);

Expand Down Expand Up @@ -149,8 +149,24 @@ const Upload = ({
onFileUpload,
]);

const removeFile = (fileId) => {
const newFiles = fieldValue.filter((file) => file.id !== fileId);
const validator = useCallback(
(file) => {
const isDuplicate = fieldValue.some((prev) => prev.file.name === file.name);
if (isDuplicate) {
return {

Check warning on line 156 in packages/form-upload/src/Upload.js

View check run for this annotation

Codecov / codecov/patch

packages/form-upload/src/Upload.js#L156

Added line #L156 was not covered by tests
code: 'duplicate-name',
message: 'A file with this name already exists',
};
}

return null;
},
[fieldValue]
);

const removeFile = (fileId, fileIndex) => {
const newFiles = fieldValue.filter((file, index) => file.id !== fileId || index !== fileIndex);

if (newFiles.length !== fieldValue.length) {
setFieldValue(name, newFiles, true);
const removedFile = fieldValue.find((file) => file.id === fileId);
Expand Down Expand Up @@ -254,7 +270,13 @@ const Upload = ({
<Input name={name} style={{ display: 'none' }} />
<InputGroup disabled={disabled} className={classes}>
<Suspense fallback={fallback}>
<Dropzone onDrop={onDrop} multiple={multiple} maxSize={maxSize} accept={allowedFileTypes}>
<Dropzone
onDrop={onDrop}
multiple={multiple}
maxSize={maxSize}
accept={allowedFileTypes}
validator={validator}
>
{({ getRootProps, getInputProps, isDragActive }) => (
<section>
<div
Expand Down

0 comments on commit 971fbd0

Please sign in to comment.