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

gr.Dropdown now has correct behavior in static mode as well as when an option is selected #5062

Merged
merged 7 commits into from Aug 2, 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/eight-humans-sniff.md
@@ -0,0 +1,6 @@
---
"@gradio/form": patch
"gradio": patch
---

fix:`gr.Dropdown` now has correct behavior in static mode as well as when an option is selected
Expand Up @@ -15,12 +15,14 @@ $demo_hello_blocks

## Event Listeners and Interactivity

In the example above, you'll notice that you are able to edit Textbox `name`, but not Textbox `output`. This is because any Component that acts as an input to an event listener is made interactive. However, since Textbox `output` acts only as an output, it is not interactive. You can directly configure the interactivity of a Component with the `interactive=` keyword argument.
In the example above, you'll notice that you are able to edit Textbox `name`, but not Textbox `output`. This is because any Component that acts as an input to an event listener is made interactive. However, since Textbox `output` acts only as an output, Gradio determines that it should not be made interactive. You can override the default behavior and directly configure the interactivity of a Component with the boolean `interactive` keyword argument.

```python
output = gr.Textbox(label="Output", interactive=True)
```

_Note_: What happens if a Gradio component is neither an input nor an output? If a component is constructed with a default value, then it is presumed to be displaying content and is rendered non-interactive. Otherwise, it is rendered interactive. Again, this behavior can be overridden by specifying a value for the `interactive` argument.

## Types of Event Listeners

Take a look at the demo below:
Expand Down
26 changes: 26 additions & 0 deletions js/form/src/Dropdown.stories.svelte
@@ -0,0 +1,26 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import Dropdown from "./Dropdown.svelte";
</script>

<Meta
title="Components/Dropdown"
component={Dropdown}
argTypes={{
multiselect: {
control: [true, false],
description: "Whether to autoplay the video on load",
name: "multiselect",
value: false
}
}}
/>

<Template let:args>
<Dropdown {...args} />
</Template>

<Story name="Default" args={{ value:"swim", choices:["run", "swim", "jump"], label:"Dropdown"}} />
<Story name="Multiselect" args={{ value:["swim", "run"], choices:["run", "swim", "jump"], label:"Multiselect Dropdown", multiselect:true}} />
<Story name="Multiselect Static" args={{ value:["swim", "run"], choices:["run", "swim", "jump"], label:"Multiselect Dropdown", multiselect:true, disabled:true}} />

27 changes: 15 additions & 12 deletions js/form/src/Dropdown.svelte
Expand Up @@ -72,8 +72,11 @@
}

function remove(option: string): void {
value = value as string[];
value = value.filter((v: string) => v !== option);
if (!disabled)
{
value = value as string[];
value = value.filter((v: string) => v !== option);
}
dispatch("select", {
index: choices.indexOf(option),
value: option,
Expand All @@ -87,7 +90,7 @@
e.preventDefault();
}

function handle_blur(e: FocusEvent) {
function handle_blur(e: FocusEvent): void {
if (multiselect) {
inputValue = "";
} else if (!allow_custom_value) {
Expand All @@ -104,14 +107,10 @@
dispatch("blur");
}

function handle_focus(e: FocusEvent){
function handle_focus(e: FocusEvent): void{
dispatch("focus");
showOptions = !showOptions;
if (showOptions) {
filtered = choices;
} else {
filterInput.blur();
}
Comment on lines -112 to -114
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, these lines can never be reached @dawoodkhan82?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your cleanup of this makes sense. But I think these lines should've been hit. How else was the input being blurred? But I also didn't go back and test on main, so not sure.

showOptions = true;
filtered = choices;
}

function handleOptionMousedown(e: any): void {
Expand All @@ -137,6 +136,7 @@
value: option,
selected: true
});
filterInput.blur();
}
}
}
Expand All @@ -154,6 +154,7 @@
}
inputValue = activeOption;
showOptions = false;
filterInput.blur();
} else if (multiselect && Array.isArray(value)) {
value.includes(activeOption) ? remove(activeOption) : add(activeOption);
inputValue = "";
Expand Down Expand Up @@ -209,13 +210,15 @@
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div on:click|preventDefault={() => remove(s)} class="token">
<span>{s}</span>
{#if !disabled}
<div
class:hidden={disabled}
class="token-remove"
title="Remove {s}"
>
<Remove />
</div>
<Remove />
</div>
{/if}
</div>
{/each}
{/if}
Expand Down