fix: prevent triggering gr.File.select on delete (#8334)

* fix: prevent triggering gr.File.select on delete

* extract into named function

* go away eslint

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
gtmnayan 2024-05-20 18:45:22 +05:45 committed by GitHub
parent 4d78f29666
commit 0236b1ab12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 7 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/file": patch
"gradio": patch
---
fix:fix: prevent triggering gr.File.select on delete

View File

@ -31,6 +31,20 @@
};
});
function handle_row_click(
event: MouseEvent & { currentTarget: HTMLTableRowElement },
index: number
): void {
const tr = event.currentTarget;
const should_select =
event.target === tr || // Only select if the click is on the row itself
event.composedPath().includes(tr.firstElementChild); // Or if the click is on the name column
if (should_select) {
dispatch("select", { value: normalized_files[index], index });
}
}
function remove_file(index: number): void {
normalized_files.splice(index, 1);
normalized_files = [...normalized_files];
@ -45,15 +59,13 @@
>
<table class="file-preview">
<tbody>
{#each normalized_files as file, i}
{#each normalized_files as file, i (file)}
<tr
class="file"
class:selectable
on:click={() =>
dispatch("select", {
value: file.orig_name,
index: i
})}
on:click={(event) => {
handle_row_click(event, i);
}}
>
<td class="filename" aria-label={file.orig_name}>
<span class="stem">{file.filename_stem}</span>
@ -74,12 +86,15 @@
{i18n("file.uploading")}
{/if}
</td>
{#if normalized_files.length > 1}
<td>
<button
class="label-clear-button"
aria-label="Remove this file"
on:click={() => remove_file(i)}
on:click={() => {
remove_file(i);
}}
on:keydown={(event) => {
if (event.key === "Enter") {
remove_file(i);