Skip to content

Commit

Permalink
feat(file-uploader): add default slot for drop area (#559)
Browse files Browse the repository at this point in the history
* feat(file-uploader): add slot for drop area

* feat(utils): edit isImage to use mime type

* chore: increment version
  • Loading branch information
stubbo authored Jan 25, 2023
1 parent 42caeb9 commit 0a9a0c8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@karnama/vueish",
"version": "0.19.2",
"version": "0.20.0",
"files": [
"dist",
"types"
Expand Down
35 changes: 26 additions & 9 deletions src/components/file-uploader/Demo.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
<template>
<div class="flex justify-evenly mb-6">
<UIButton theme="red" @click="error ? error = '' : error = 'Error message.'">
{{ error ? 'Remove' : 'Set' }} Error state
</UIButton>
</div>
<div class="space-y-6">
<div class="flex justify-evenly">
<UIButton theme="red" @click="error ? error = '' : error = 'Error message.'">
{{ error ? 'Remove' : 'Set' }} Error state
</UIButton>
</div>

<UIFileUploader :error="error"
:upload="upload"
class="border-brand-400"
@validation-error="logError" />

<UIFileUploader :error="error"
:upload="upload"
class="border-brand-400"
@validation-error="logError" />
<UIFileUploader :error="error"
:upload="upload"
class="border-brand-400"
@validation-error="logError">
<template #default="{ uploadIcon }">
<p class="text-pink-600" v-html="uploadIcon" />
<p class="text-lg">
Drop files here to upload them
</p>
<UIButton theme="pink" class="mt-2">
Browse
</UIButton>
</template>
</UIFileUploader>
</div>
</template>

<script lang="ts">
Expand Down
20 changes: 11 additions & 9 deletions src/components/file-uploader/UIFileUploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@

<div class="grow flex flex-col items-center justify-center py-24 px-2 text-center cursor-pointer"
@click="openFileBrowser">
<p class="mb-4 text-brand-600" v-html="uploadIcon" />
<p class="text-lg">
Drag and Drop file
<br>
or
</p>
<UIButton theme="brand" class="mt-2">
Browse
</UIButton>
<slot :upload-icon="uploadIcon">
<p class="mb-4 text-brand-600" v-html="uploadIcon" />
<p class="text-lg">
Drag and Drop file
<br>
or
</p>
<UIButton theme="brand" class="mt-2">
Browse
</UIButton>
</slot>
</div>
<div v-show="files.length"
class="w-full sm:w-1/2 flex flex-col py-6 px-2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`UIFileUploader should display correctly 1`] = `
<div
class="grow flex flex-col items-center justify-center py-24 px-2 text-center cursor-pointer"
>
<p
class="mb-4 text-brand-600"
>
Expand Down Expand Up @@ -44,6 +45,7 @@ exports[`UIFileUploader should display correctly 1`] = `
Browse
</button>
</div>
<div
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"
Expand Down
11 changes: 6 additions & 5 deletions src/composables/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { createFileList, getExtension, getSizeString, isImage } from './index';
describe('utils', () => {
describe('isImage', () => {
it('should correctly determine the image based on the extension', () => {
expect(isImage(new File([''], 'text.txt'))).toBe(false);
expect(isImage(new File([''], 'text.txt', { type: 'text/plain' }))).toBe(false);

expect(isImage(new File([''], 'img.jpeg'))).toBe(true);
expect(isImage(new File([''], 'img.jpg'))).toBe(true);
expect(isImage(new File([''], 'img.png'))).toBe(true);
expect(isImage(new File([''], 'img.gif'))).toBe(true);
expect(isImage(new File([''], 'img.jpeg', { type: 'image/jpeg' }))).toBe(true);
expect(isImage(new File([''], 'img.jpg', { type: 'image/jpeg' }))).toBe(true);
expect(isImage(new File([''], 'img.png', { type: 'image/png' }))).toBe(true);
expect(isImage(new File([''], 'img.gif', { type: 'image/gif' }))).toBe(true);
expect(isImage(new File([''], 'img.webp', { type: 'image/webp' }))).toBe(true);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/composables/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const createFileList = (files: File | File[]): FileList => {
* Determine whether the given file is an image.
*/
export const isImage = (file: File): boolean => {
return !!RegExp(/[/.](gif|jp(e)?g|png)$/i).exec(file.name);
return file.type.startsWith('image/');
};

/**
Expand Down

0 comments on commit 0a9a0c8

Please sign in to comment.