Skip to content

Commit

Permalink
fix: adjust CvFileUploader drop handler
Browse files Browse the repository at this point in the history
Check 'multiple' attr before passing files to 'addFiles'. if multiple is
falsy, it will only pass the first dropped file.
  • Loading branch information
felipebritor committed Dec 23, 2024
1 parent 1c80292 commit 3ad044f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/components/CvFileUploader/CvFileUploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { ref, computed, onMounted, watch, useAttrs } from 'vue';
import CvFileUploaderItem from './CvFileUploaderItem.vue';
import { carbonPrefix } from '../../global/settings';
import { CvFormItem } from '../CvForm';
Expand All @@ -110,6 +110,8 @@ import {
const emit = defineEmits(['update:modelValue']);
const attrs = useAttrs();
const props = defineProps({
accept: { type: String, default: undefined },
buttonKind: commonCvButtonProps.kind,
Expand Down Expand Up @@ -271,7 +273,10 @@ function onDragEvent(evt) {
if (evt.type === 'drop') {
evt.preventDefault();
addFiles(evt.dataTransfer.files);
const files = attrs.multiple
? evt.dataTransfer.files
: [evt.dataTransfer.files[0]];
addFiles(files);
allowDrop.value = false;
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/components/CvFileUploader/__tests__/CvFileUploader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,61 @@ describe('CvFileUploader', () => {
expect(emitResult[0]).toHaveLength(1);
expect(emitResult[0][0].file.name).toBe(dummyFile.name);
});

it('emits a single file when attr multiple is falsy', async () => {
const dummyFiles = [
new File(['file content'], 'dummy-file1.txt', {
type: 'text/plain',
}),
new File(['file content'], 'dummy-file2.txt', {
type: 'text/plain',
}),
];
const { container, emitted } = render(CvFileUploader);

const dropZone = container.querySelector(
`div.${carbonPrefix}--file-browse-btn`
);
await fireEvent.drop(dropZone, {
dataTransfer: {
files: dummyFiles,
types: ['Files'],
},
});

const emitResult = emitted('update:modelValue').at(0);
expect(emitResult[0]).toHaveLength(1);
});

it('emits all dropped files when attr multiple is truthy', async () => {
const dummyFiles = [
new File(['file content'], 'dummy-file1.txt', {
type: 'text/plain',
}),
new File(['file content'], 'dummy-file2.txt', {
type: 'text/plain',
}),
new File(['file content'], 'dummy-file3.txt', {
type: 'text/plain',
}),
];
const { container, emitted } = render(CvFileUploader, {
attrs: { multiple: true },
});

const dropZone = container.querySelector(
`div.${carbonPrefix}--file-browse-btn`
);
await fireEvent.drop(dropZone, {
dataTransfer: {
files: dummyFiles,
types: ['Files'],
},
});

const emitResult = emitted('update:modelValue').at(0);
expect(emitResult[0]).toHaveLength(3);
});
});

describe('File listing', () => {
Expand Down

0 comments on commit 3ad044f

Please sign in to comment.