Skip to content

Commit

Permalink
Ensure input value saves on dropdown blur (#5525)
Browse files Browse the repository at this point in the history
* save input on dropdown blur

* set input_value

* add changeset

* add changeset

* add changeset

* add test

* tweak blur logic

* remove unused funcs

* nit

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
  • Loading branch information
3 people committed Sep 13, 2023
1 parent 06bdc62 commit 21f1db4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/lazy-dogs-walk.md
@@ -0,0 +1,6 @@
---
"@gradio/dropdown": patch
"gradio": patch
---

fix:Ensure input value saves on dropdown blur
64 changes: 47 additions & 17 deletions js/dropdown/dropdown.test.ts
@@ -1,5 +1,5 @@
import { test, describe, assert, afterEach, vi } from "vitest";
import { cleanup, fireEvent, render, get_text, wait } from "@gradio/tootils";
import { cleanup, render } from "@gradio/tootils";
import event from "@testing-library/user-event";
import { setupi18n } from "../app/src/i18n";

Expand Down Expand Up @@ -35,7 +35,8 @@ describe("Dropdown", () => {
choices: [
["choice", "choice"],
["choice2", "choice2"]
]
],
filterable: false
});

const item: HTMLInputElement = getByLabelText(
Expand All @@ -54,7 +55,8 @@ describe("Dropdown", () => {
choices: [
["choice", "choice"],
["name2", "choice2"]
]
],
filterable: true
});

const item: HTMLInputElement = getByLabelText(
Expand All @@ -80,7 +82,8 @@ describe("Dropdown", () => {
choices: [
["apple", "apple"],
["zebra", "zebra"]
]
],
filterable: true
});

const item: HTMLInputElement = getByLabelText(
Expand Down Expand Up @@ -110,7 +113,8 @@ describe("Dropdown", () => {
choices: [
["default", "default"],
["other", "other"]
]
],
filterable: false
});

const item: HTMLInputElement = getByLabelText(
Expand All @@ -119,13 +123,36 @@ describe("Dropdown", () => {
const change_event = listen("change");
const select_event = listen("select");

await item.focus();
item.focus();
await event.keyboard("other");
});

test("blurring the textbox should save the input value", async () => {
const { getByLabelText, listen } = await render(Dropdown, {
show_label: true,
loading_status,
value: "default",
label: "Dropdown",
max_choices: undefined,
allow_custom_value: true,
choices: [
["dwight", "dwight"],
["michael", "michael"]
],
filterable: true
});

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

item.focus();
await event.keyboard("kevin");
await item.blur();

assert.equal(item.value, "default");
assert.equal(change_event.callCount, 0);
assert.equal(select_event.callCount, 0);
assert.equal(item.value, "kevin");
assert.equal(change_event.callCount, 1);
});

test("focusing the label should toggle the options", async () => {
Expand All @@ -137,7 +164,8 @@ describe("Dropdown", () => {
choices: [
["default", "default"],
["other", "other"]
]
],
filterable: true
});

const item: HTMLInputElement = getByLabelText(
Expand All @@ -146,8 +174,8 @@ describe("Dropdown", () => {
const blur_event = listen("blur");
const focus_event = listen("focus");

await item.focus();
await item.blur();
item.focus();
item.blur();

assert.equal(blur_event.callCount, 1);
assert.equal(focus_event.callCount, 1);
Expand All @@ -165,14 +193,15 @@ describe("Dropdown", () => {
["apple", "apple"],
["zebra", "zebra"],
["pony", "pony"]
]
],
filterable: true
});

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

await item.focus();
item.focus();
item.value = "";
await event.keyboard("z");
const options = getAllByTestId("dropdown-option");
Expand Down Expand Up @@ -200,7 +229,8 @@ describe("Dropdown", () => {
["apple", "apple"],
["zebra", "zebra"],
["pony", "pony"]
]
],
filterable: true
}
);

Expand All @@ -214,7 +244,7 @@ describe("Dropdown", () => {

expect(options).toHaveLength(3);

await component.$set({
component.$set({
value: "",
choices: [
["apple", "apple"],
Expand All @@ -223,7 +253,7 @@ describe("Dropdown", () => {
]
});

await item.focus();
item.focus();

const options_new = getAllByTestId("dropdown-option");
expect(options_new).toHaveLength(3);
Expand Down
3 changes: 2 additions & 1 deletion js/dropdown/shared/Dropdown.svelte
Expand Up @@ -74,7 +74,7 @@
dispatch("select", {
index: selected_index,
value: choices_values[selected_index],
selected: true
selected: true,
});
}
}
Expand Down Expand Up @@ -132,6 +132,7 @@
if (!allow_custom_value) {
input_text = choices_names[choices_values.indexOf(value as string)];
}
value = input_text;
show_options = false;
active_index = null;
dispatch("blur");
Expand Down
10 changes: 8 additions & 2 deletions js/dropdown/shared/Multiselect.svelte
Expand Up @@ -93,6 +93,12 @@
if (!allow_custom_value) {
input_text = "";
}
if (allow_custom_value && input_text !== "") {
add_selected_choice(input_text);
input_text = "";
}
show_options = false;
active_index = null;
dispatch("blur");
Expand All @@ -106,7 +112,7 @@
typeof option_index === "number"
? choices_values[option_index]
: option_index,
selected: false
selected: false,
});
}
Expand All @@ -119,7 +125,7 @@
typeof option_index === "number"
? choices_values[option_index]
: option_index,
selected: true
selected: true,
});
}
if (selected_indices.length === max_choices) {
Expand Down

0 comments on commit 21f1db4

Please sign in to comment.