Skip to content

Commit

Permalink
Fix dropdown blur bug when values are provided as tuples (#6694)
Browse files Browse the repository at this point in the history
* fix dropdown blur bug

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot committed Dec 7, 2023
1 parent cfd5700 commit dfc61ec
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 116 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-crabs-take.md
@@ -0,0 +1,6 @@
---
"@gradio/dropdown": patch
"gradio": patch
---

fix:Fix dropdown blur bug when values are provided as tuples
37 changes: 37 additions & 0 deletions js/dropdown/dropdown.test.ts
Expand Up @@ -370,4 +370,41 @@ describe("Dropdown", () => {
options = getAllByTestId("dropdown-option");
expect(options[0]).toHaveClass("selected");
});

test("blurring a dropdown should set the input text to the previously selected value", async () => {
const { getByLabelText, getAllByTestId, component } = await render(
Dropdown,
{
show_label: true,
loading_status,
value: "",
allow_custom_value: false,
label: "Dropdown",
choices: [
["apple", "apple_internal_value"],
["zebra", "zebra_internal_value"],
["pony", "pony_internal_value"]
],
filterable: true,
interactive: true
}
);

const item: HTMLInputElement = getByLabelText(
"Dropdown"
) as HTMLInputElement;

expect(item.value).toBe("apple");
await item.focus();
let options = getAllByTestId("dropdown-option");
expect(options[0]).toHaveClass("selected");
await item.blur();
expect(item.value).toBe("apple");

await item.focus();
await event.keyboard("z");
expect(item.value).toBe("applez");
await item.blur();
expect(item.value).toBe("apple");
});
});
3 changes: 2 additions & 1 deletion js/dropdown/shared/Dropdown.svelte
Expand Up @@ -140,8 +140,9 @@
function handle_blur(): void {
if (!allow_custom_value) {
input_text = choices_names[choices_values.indexOf(value as string)];
} else {
value = input_text;
}
value = input_text;
show_options = false;
active_index = null;
dispatch("blur");
Expand Down
140 changes: 25 additions & 115 deletions pnpm-lock.yaml

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

0 comments on commit dfc61ec

Please sign in to comment.