Added remove button for every file in file preview, to remove individual file in gr.File() (#7299)

* Add remove button for every file in file preview

* add changeset

* tweak remove file button

* format

* dispatch change event

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Shubham raj 2024-02-09 22:25:47 +05:30 committed by GitHub
parent 7b10d97b0f
commit f35f615e33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/file": minor
"gradio": minor
---
feat:Added remove button for every file in file preview, to remove individual file in gr.File()

View File

@ -7,6 +7,7 @@
const dispatch = createEventDispatcher<{ const dispatch = createEventDispatcher<{
select: SelectData; select: SelectData;
change: FileData[] | FileData;
}>(); }>();
export let value: FileData | FileData[]; export let value: FileData | FileData[];
export let selectable = false; export let selectable = false;
@ -29,6 +30,13 @@
filename_ext filename_ext
}; };
}); });
function remove_file(index: number): void {
normalized_files.splice(index, 1);
normalized_files = [...normalized_files];
value = normalized_files;
dispatch("change", normalized_files);
}
</script> </script>
<div <div
@ -66,6 +74,21 @@
{i18n("file.uploading")} {i18n("file.uploading")}
{/if} {/if}
</td> </td>
{#if normalized_files.length > 1}
<td>
<button
class="label-clear-button"
aria-label="Remove this file"
on:click={() => remove_file(i)}
on:keydown={(event) => {
if (event.key === "Enter") {
remove_file(i);
}
}}
>×
</button>
</td>
{/if}
</tr> </tr>
{/each} {/each}
</tbody> </tbody>
@ -73,6 +96,16 @@
</div> </div>
<style> <style>
.label-clear-button {
color: var(--body-text-color-subdued);
position: relative;
left: -3px;
}
.label-clear-button:hover {
color: var(--body-text-color);
}
.file-preview { .file-preview {
table-layout: fixed; table-layout: fixed;
width: var(--size-full); width: var(--size-full);

View File

@ -69,7 +69,7 @@
{#if value && (Array.isArray(value) ? value.length > 0 : true)} {#if value && (Array.isArray(value) ? value.length > 0 : true)}
<ModifyUpload {i18n} on:clear={handle_clear} absolute /> <ModifyUpload {i18n} on:clear={handle_clear} absolute />
<FilePreview {i18n} on:select {selectable} {value} {height} /> <FilePreview {i18n} on:select {selectable} {value} {height} on:change />
{:else} {:else}
<Upload <Upload
on:load={handle_upload} on:load={handle_upload}