Skip to content

Commit

Permalink
Enhancement: Add focus event to textbox and number component (#5005)
Browse files Browse the repository at this point in the history
* Add focus event to textbox and number component

* add changeset

* Combine Blurrable and Focusable into Focusable event

* Add focus and blur to Dropdown and Colorpicker components

* add focus to home page template

* Add Focus to doc

* Fix linting error

* fixes

---------

Co-authored-by: gradio-pr-bot <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>
Co-authored-by: Ali Abid <[email protected]>
  • Loading branch information
4 people authored Aug 1, 2023
1 parent 95ea19d commit f5539c7
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 50 deletions.
7 changes: 7 additions & 0 deletions .changeset/silver-signs-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gradio/app": minor
"@gradio/form": minor
"gradio": minor
---

feat:Enhancement: Add focus event to textbox and number component
4 changes: 2 additions & 2 deletions gradio/components/color_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from gradio.components.base import IOComponent, _Keywords
from gradio.events import (
Blurrable,
Changeable,
Focusable,
Inputable,
Submittable,
)
Expand All @@ -20,7 +20,7 @@

@document()
class ColorPicker(
Changeable, Inputable, Submittable, Blurrable, IOComponent, StringSerializable
Changeable, Inputable, Submittable, Focusable, IOComponent, StringSerializable
):
"""
Creates a color picker for user to select a color as string input.
Expand Down
4 changes: 2 additions & 2 deletions gradio/components/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from gradio.components.base import FormComponent, IOComponent, _Keywords
from gradio.deprecation import warn_style_method_deprecation
from gradio.events import (
Blurrable,
Changeable,
EventListenerMethod,
Focusable,
Inputable,
Selectable,
)
Expand All @@ -27,7 +27,7 @@ class Dropdown(
Changeable,
Inputable,
Selectable,
Blurrable,
Focusable,
IOComponent,
SimpleSerializable,
):
Expand Down
4 changes: 2 additions & 2 deletions gradio/components/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from gradio.components.base import FormComponent, IOComponent, _Keywords
from gradio.events import (
Blurrable,
Changeable,
Focusable,
Inputable,
Submittable,
)
Expand All @@ -28,7 +28,7 @@ class Number(
Changeable,
Inputable,
Submittable,
Blurrable,
Focusable,
IOComponent,
NumberSerializable,
NeighborInterpretable,
Expand Down
4 changes: 2 additions & 2 deletions gradio/components/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
)
from gradio.deprecation import warn_style_method_deprecation
from gradio.events import (
Blurrable,
Changeable,
EventListenerMethod,
Focusable,
Inputable,
Selectable,
Submittable,
Expand All @@ -34,7 +34,7 @@ class Textbox(
Inputable,
Selectable,
Submittable,
Blurrable,
Focusable,
IOComponent,
StringSerializable,
TokenInterpretable,
Expand Down
10 changes: 8 additions & 2 deletions gradio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,15 @@ def __init__(self):
"""


@document("*blur", inherit=True)
class Blurrable(EventListener):
@document("*focus", "*blur", inherit=True)
class Focusable(EventListener):
def __init__(self):
self.focus = EventListenerMethod(self, "focus")
"""
This listener is triggered when the component is focused (e.g. when the user clicks inside a textbox).
This method can be used when this component is in a Gradio Blocks.
"""

self.blur = EventListenerMethod(self, "blur")
"""
This listener is triggered when the component's is unfocused/blurred (e.g. when the user clicks outside of a textbox).
Expand Down
1 change: 1 addition & 0 deletions js/app/src/components/ColorPicker/ColorPicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
on:input
on:submit
on:blur
on:focus
disabled={mode === "static"}
/>
</Block>
1 change: 1 addition & 0 deletions js/app/src/components/Dropdown/Dropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
on:input
on:select
on:blur
on:focus
disabled={mode === "static"}
/>
</Block>
1 change: 1 addition & 0 deletions js/app/src/components/Number/Number.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
on:input
on:submit
on:blur
on:focus
/>
</Block>
1 change: 1 addition & 0 deletions js/app/src/components/Textbox/Textbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
on:submit
on:blur
on:select
on:focus
disabled={mode === "static"}
/>
</Block>
4 changes: 3 additions & 1 deletion js/form/src/ColorPicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
input: undefined;
submit: undefined;
blur: undefined;
focus: undefined;
}>();
function handle_change() {
Expand All @@ -22,6 +23,7 @@
dispatch("input");
}
}
afterUpdate(() => {
value_is_output = false;
});
Expand All @@ -31,7 +33,7 @@
<!-- svelte-ignore a11y-label-has-associated-control -->
<label class="block">
<BlockTitle {show_label} {info}>{label}</BlockTitle>
<input type="color" on:blur bind:value {disabled} />
<input type="color" bind:value on:focus on:blur {disabled} />
</label>

<style>
Expand Down
55 changes: 30 additions & 25 deletions js/form/src/Dropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
input: undefined;
select: SelectData;
blur: undefined;
focus: undefined;
}>();
let inputValue: string | undefined,
Expand Down Expand Up @@ -86,6 +87,33 @@
e.preventDefault();
}
function handle_blur(e: FocusEvent) {
if (multiselect) {
inputValue = "";
} else if (!allow_custom_value) {
if (value !== inputValue) {
if (typeof value === "string" && inputValue == "") {
inputValue = value;
} else {
value = undefined;
inputValue = "";
}
}
}
showOptions = false;
dispatch("blur");
}
function handle_focus(e: FocusEvent){
dispatch("focus");
showOptions = !showOptions;
if (showOptions) {
filtered = choices;
} else {
filterInput.blur();
}
}
function handleOptionMousedown(e: any): void {
const option = e.detail.target.dataset.value;
if (allow_custom_value) {
Expand Down Expand Up @@ -113,15 +141,6 @@
}
}
function handleFocus(): void {
showOptions = !showOptions;
if (showOptions) {
filtered = choices;
} else {
filterInput.blur();
}
}
function handleKeydown(e: any) {
if (e.key === "Enter" && activeOption != undefined) {
if (!multiselect) {
Expand Down Expand Up @@ -208,28 +227,14 @@
autocomplete="off"
bind:value={inputValue}
bind:this={filterInput}
on:focus={handleFocus}
on:keydown={handleKeydown}
on:keyup={() => {
if (allow_custom_value) {
value = inputValue;
}
}}
on:blur={() => {
if (multiselect) {
inputValue = "";
} else if (!allow_custom_value) {
if (value !== inputValue) {
if (typeof value === "string" && inputValue == "") {
inputValue = value;
} else {
value = undefined;
inputValue = "";
}
}
}
showOptions = false;
}}
on:blur={handle_blur}
on:focus={handle_focus}
/>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
Expand Down
8 changes: 3 additions & 5 deletions js/form/src/Number.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
submit: undefined;
blur: undefined;
input: undefined;
focus: undefined;
}>();
function handle_change(): void {
Expand All @@ -40,10 +41,6 @@
dispatch("submit");
}
}
function handle_blur(e: FocusEvent): void {
dispatch("blur");
}
</script>

<label class="block" class:container>
Expand All @@ -55,7 +52,8 @@
max={maximum}
{step}
on:keypress={handle_keypress}
on:blur={handle_blur}
on:blur
on:focus
{disabled}
/>
</label>
Expand Down
17 changes: 9 additions & 8 deletions js/form/src/Textbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
blur: undefined;
select: SelectData;
input: undefined;
focus: undefined;
}>();
function handle_change() {
Expand All @@ -46,10 +47,6 @@
});
$: value, handle_change();
function handle_blur() {
dispatch("blur");
}
async function handle_copy() {
if ("clipboard" in navigator) {
await navigator.clipboard.writeText(value);
Expand Down Expand Up @@ -153,8 +150,9 @@
{disabled}
{autofocus}
on:keypress={handle_keypress}
on:blur={handle_blur}
on:blur
on:select={handle_select}
on:focus
style={text_align ? "text-align: " + text_align : ""}
/>
{:else if type === "password"}
Expand All @@ -168,8 +166,9 @@
{disabled}
{autofocus}
on:keypress={handle_keypress}
on:blur={handle_blur}
on:blur
on:select={handle_select}
on:focus
autocomplete=""
/>
{:else if type === "email"}
Expand All @@ -183,8 +182,9 @@
{disabled}
{autofocus}
on:keypress={handle_keypress}
on:blur={handle_blur}
on:blur
on:select={handle_select}
on:focus
autocomplete="email"
/>
{/if}
Expand All @@ -208,8 +208,9 @@
{disabled}
{autofocus}
on:keypress={handle_keypress}
on:blur={handle_blur}
on:blur
on:select={handle_select}
on:focus
style={text_align ? "text-align: " + text_align : ""}
/>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/homepage/src/docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def add_demos():
"Pause()",
"Stream()",
"Blur()",
"Focus()",
"Upload()",
]

Expand Down
1 change: 1 addition & 0 deletions website/homepage/src/docs/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ <h2 id="components-header" class="text-4xl font-light mb-2 pt-2 text-orange-500"
<th class="p-3 font-normal bg-white border-t">Pause</th>
<th class="p-3 font-normal bg-white border-t">Stream</th>
<th class="p-3 font-normal bg-white border-t">Blur</th>
<th class="p-3 font-normal bg-white border-t">Focus</th>
<th class="p-3 font-normal bg-white border-t border-r">Upload</th>
</tr>
</thead>
Expand Down

1 comment on commit f5539c7

@vercel
Copy link

@vercel vercel bot commented on f5539c7 Aug 1, 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.