gr.Dropdown now has correct behavior in static mode as well as when 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>
This commit is contained in:
Abubakar Abid 2023-08-02 12:32:45 -04:00 committed by GitHub
parent f5539c7618
commit 7d89716519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 13 deletions

View File

@ -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

View File

@ -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:

View File

@ -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}} />

View File

@ -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,
@ -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) {
@ -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 {
@ -137,6 +136,7 @@
value: option,
selected: true
});
filterInput.blur();
}
}
}
@ -154,6 +154,7 @@
}
inputValue = activeOption;
showOptions = false;
filterInput.blur();
} else if (multiselect && Array.isArray(value)) {
value.includes(activeOption) ? remove(activeOption) : add(activeOption);
inputValue = "";
@ -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}