Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes dropdown breaking if a user types in invalid value and presses enter #5544

Merged
merged 6 commits into from Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sour-falcons-marry.md
@@ -0,0 +1,6 @@
---
"@gradio/dropdown": patch
"gradio": patch
---

fix:Fixes dropdown breaking if a user types in invalid value and presses enter
58 changes: 58 additions & 0 deletions js/dropdown/dropdown.test.ts
Expand Up @@ -258,4 +258,62 @@ describe("Dropdown", () => {
const options_new = getAllByTestId("dropdown-option");
expect(options_new).toHaveLength(3);
});

test("setting a custom value when allow_custom_choice is false should revert to the first valid choice", async () => {
const { getByLabelText, getAllByTestId, component } = await render(
Dropdown,
{
show_label: true,
loading_status,
value: "",
allow_custom_value: false,
label: "Dropdown",
choices: [
["apple", "apple"],
["zebra", "zebra"],
["pony", "pony"]
],
filterable: true
}
);

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

await item.focus();
await event.keyboard("pie");
expect(item.value).toBe("applepie");
await item.blur();
expect(item.value).toBe("apple");
});

test("setting a custom value when allow_custom_choice is true should keep the value", async () => {
const { getByLabelText, getAllByTestId, component } = await render(
Dropdown,
{
show_label: true,
loading_status,
value: "",
allow_custom_value: true,
label: "Dropdown",
choices: [
["apple", "apple"],
["zebra", "zebra"],
["pony", "pony"]
],
filterable: true
}
);

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

await item.focus();
await event.keyboard("pie");
expect(item.value).toBe("applepie");
await item.blur();
expect(item.value).toBe("applepie");
});
});
4 changes: 2 additions & 2 deletions 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 All @@ -97,7 +97,7 @@
filtered_indices = handle_filter(choices, input_text);
old_choices = choices;
old_input_text = input_text;
if (!allow_custom_value) {
if (!allow_custom_value && filtered_indices.length > 0) {
active_index = filtered_indices[0];
}
}
Expand Down