Skip to content

Commit

Permalink
Allow uploading more files in gr.File (#9891)
Browse files Browse the repository at this point in the history
* add upload to icon buttons in file

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
hannahblair and gradio-pr-bot authored Nov 2, 2024
1 parent d1cfe1e commit fc12496
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/empty-monkeys-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@gradio/atoms": minor
"@gradio/file": minor
"@gradio/upload": minor
"gradio": minor
---

feat:Allow uploading more files in gr.File
1 change: 1 addition & 0 deletions js/atoms/src/IconButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class:medium={size === "medium"}
>
<svelte:component this={Icon} />
<slot />
</div>
</button>

Expand Down
40 changes: 36 additions & 4 deletions js/file/shared/FileUpload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { createEventDispatcher, tick } from "svelte";
import { Upload, ModifyUpload } from "@gradio/upload";
import type { FileData, Client } from "@gradio/client";
import { BlockLabel } from "@gradio/atoms";
import { File } from "@gradio/icons";
import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
import { File, Clear, Upload as UploadIcon } from "@gradio/icons";
import FilePreview from "./FilePreview.svelte";
import type { I18nFormatter } from "@gradio/utils";
Expand All @@ -26,7 +26,13 @@
async function handle_upload({
detail
}: CustomEvent<FileData | FileData[]>): Promise<void> {
value = detail;
if (Array.isArray(value)) {
value = [...value, ...(Array.isArray(detail) ? detail : [detail])];
} else if (value) {
value = [value, ...(Array.isArray(detail) ? detail : [detail])];
} else {
value = detail;
}
await tick();
dispatch("change", value);
dispatch("upload", detail);
Expand Down Expand Up @@ -54,7 +60,33 @@
<BlockLabel {show_label} Icon={File} float={!value} label={label || "File"} />

{#if value && (Array.isArray(value) ? value.length > 0 : true)}
<ModifyUpload {i18n} on:clear={handle_clear} />
<IconButtonWrapper>
<IconButton Icon={UploadIcon} label={i18n("common.upload")}>
<Upload
icon_upload={true}
on:load={handle_upload}
filetype={file_types}
{file_count}
{max_file_size}
{root}
bind:dragging
bind:uploading
on:error
{stream_handler}
{upload}
/>
</IconButton>
<IconButton
Icon={Clear}
label={i18n("common.clear")}
on:click={(event) => {
dispatch("clear");
event.stopPropagation();
handle_clear();
}}
/>
</IconButtonWrapper>

<FilePreview
{i18n}
on:select
Expand Down
21 changes: 19 additions & 2 deletions js/upload/src/Upload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
export let max_file_size: number | null = null;
export let upload: Client["upload"];
export let stream_handler: Client["stream"];
export let icon_upload = false;
let upload_id: string;
let file_data: FileData[];
Expand Down Expand Up @@ -265,7 +266,8 @@
class:center
class:boundedheight
class:flex
style:height="100%"
class:icon-mode={icon_upload}
style:height={icon_upload ? "" : "100%"}
tabindex={hidden ? -1 : 0}
on:click={paste_clipboard}
>
Expand All @@ -282,7 +284,8 @@
class:boundedheight
class:flex
class:disable_click
style:height="100%"
class:icon-mode={icon_upload}
style:height={icon_upload ? "" : "100%"}
tabindex={hidden ? -1 : 0}
on:drag|preventDefault|stopPropagation
on:dragstart|preventDefault|stopPropagation
Expand Down Expand Up @@ -344,4 +347,18 @@
input {
display: none;
}
.icon-mode {
position: absolute !important;
width: var(--size-4);
height: var(--size-4);
padding: 0;
min-height: 0;
border-radius: var(--radius-circle);
}
.icon-mode :global(svg) {
width: var(--size-4);
height: var(--size-4);
}
</style>

0 comments on commit fc12496

Please sign in to comment.