Skip to content

Commit 0a9a0c8

Browse files
authored
feat(file-uploader): add default slot for drop area (#559)
* feat(file-uploader): add slot for drop area * feat(utils): edit isImage to use mime type * chore: increment version
1 parent 42caeb9 commit 0a9a0c8

File tree

7 files changed

+49
-27
lines changed

7 files changed

+49
-27
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@karnama/vueish",
3-
"version": "0.19.2",
3+
"version": "0.20.0",
44
"files": [
55
"dist",
66
"types"

src/components/file-uploader/Demo.vue

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
<template>
2-
<div class="flex justify-evenly mb-6">
3-
<UIButton theme="red" @click="error ? error = '' : error = 'Error message.'">
4-
{{ error ? 'Remove' : 'Set' }} Error state
5-
</UIButton>
6-
</div>
2+
<div class="space-y-6">
3+
<div class="flex justify-evenly">
4+
<UIButton theme="red" @click="error ? error = '' : error = 'Error message.'">
5+
{{ error ? 'Remove' : 'Set' }} Error state
6+
</UIButton>
7+
</div>
8+
9+
<UIFileUploader :error="error"
10+
:upload="upload"
11+
class="border-brand-400"
12+
@validation-error="logError" />
713

8-
<UIFileUploader :error="error"
9-
:upload="upload"
10-
class="border-brand-400"
11-
@validation-error="logError" />
14+
<UIFileUploader :error="error"
15+
:upload="upload"
16+
class="border-brand-400"
17+
@validation-error="logError">
18+
<template #default="{ uploadIcon }">
19+
<p class="text-pink-600" v-html="uploadIcon" />
20+
<p class="text-lg">
21+
Drop files here to upload them
22+
</p>
23+
<UIButton theme="pink" class="mt-2">
24+
Browse
25+
</UIButton>
26+
</template>
27+
</UIFileUploader>
28+
</div>
1229
</template>
1330

1431
<script lang="ts">

src/components/file-uploader/UIFileUploader.vue

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323

2424
<div class="grow flex flex-col items-center justify-center py-24 px-2 text-center cursor-pointer"
2525
@click="openFileBrowser">
26-
<p class="mb-4 text-brand-600" v-html="uploadIcon" />
27-
<p class="text-lg">
28-
Drag and Drop file
29-
<br>
30-
or
31-
</p>
32-
<UIButton theme="brand" class="mt-2">
33-
Browse
34-
</UIButton>
26+
<slot :upload-icon="uploadIcon">
27+
<p class="mb-4 text-brand-600" v-html="uploadIcon" />
28+
<p class="text-lg">
29+
Drag and Drop file
30+
<br>
31+
or
32+
</p>
33+
<UIButton theme="brand" class="mt-2">
34+
Browse
35+
</UIButton>
36+
</slot>
3537
</div>
3638
<div v-show="files.length"
3739
class="w-full sm:w-1/2 flex flex-col py-6 px-2

src/components/file-uploader/__snapshots__/UIFileUploader.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports[`UIFileUploader should display correctly 1`] = `
1616
<div
1717
class="grow flex flex-col items-center justify-center py-24 px-2 text-center cursor-pointer"
1818
>
19+
1920
<p
2021
class="mb-4 text-brand-600"
2122
>
@@ -44,6 +45,7 @@ exports[`UIFileUploader should display correctly 1`] = `
4445
Browse
4546
4647
</button>
48+
4749
</div>
4850
<div
4951
class="w-full sm:w-1/2 flex flex-col py-6 px-2 justify-between bg-gray-200 dark:bg-gray-600 items-center"

src/composables/utils/index.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { createFileList, getExtension, getSizeString, isImage } from './index';
33
describe('utils', () => {
44
describe('isImage', () => {
55
it('should correctly determine the image based on the extension', () => {
6-
expect(isImage(new File([''], 'text.txt'))).toBe(false);
6+
expect(isImage(new File([''], 'text.txt', { type: 'text/plain' }))).toBe(false);
77

8-
expect(isImage(new File([''], 'img.jpeg'))).toBe(true);
9-
expect(isImage(new File([''], 'img.jpg'))).toBe(true);
10-
expect(isImage(new File([''], 'img.png'))).toBe(true);
11-
expect(isImage(new File([''], 'img.gif'))).toBe(true);
8+
expect(isImage(new File([''], 'img.jpeg', { type: 'image/jpeg' }))).toBe(true);
9+
expect(isImage(new File([''], 'img.jpg', { type: 'image/jpeg' }))).toBe(true);
10+
expect(isImage(new File([''], 'img.png', { type: 'image/png' }))).toBe(true);
11+
expect(isImage(new File([''], 'img.gif', { type: 'image/gif' }))).toBe(true);
12+
expect(isImage(new File([''], 'img.webp', { type: 'image/webp' }))).toBe(true);
1213
});
1314
});
1415

src/composables/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const createFileList = (files: File | File[]): FileList => {
4040
* Determine whether the given file is an image.
4141
*/
4242
export const isImage = (file: File): boolean => {
43-
return !!RegExp(/[/.](gif|jp(e)?g|png)$/i).exec(file.name);
43+
return file.type.startsWith('image/');
4444
};
4545

4646
/**

0 commit comments

Comments
 (0)