Skip to content

Commit

Permalink
gr.Dropdown now has correct behavior in static mode as well as when…
Browse files Browse the repository at this point in the history
… an option is selected (#5062)

* dropdown fixes

* add changeset

* add changeset

* guide

* guide

* stories

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot authored Aug 2, 2023
1 parent f5539c7 commit 7d89716
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-humans-sniff.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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();
}
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

1 comment on commit 7d89716

@vercel
Copy link

@vercel vercel bot commented on 7d89716 Aug 2, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.