-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Allow empty value in gr.Number #8974
Changes from all commits
8f99430
bb96283
2a1e6e0
73c26f2
b85aa6c
64a0683
c6d0e24
0e7f531
86c0ef8
c84d757
c83b05d
4671c89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@gradio/number": patch | ||
"gradio": patch | ||
--- | ||
|
||
fix:Allow empty value in gr.Number |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import { Block, BlockTitle } from "@gradio/atoms"; | ||
import { StatusTracker } from "@gradio/statustracker"; | ||
import type { LoadingStatus } from "@gradio/statustracker"; | ||
import { afterUpdate, tick } from "svelte"; | ||
import { afterUpdate, tick, onMount } from "svelte"; | ||
|
||
export let gradio: Gradio<{ | ||
change: never; | ||
|
@@ -21,7 +21,7 @@ | |
export let container = true; | ||
export let scale: number | null = null; | ||
export let min_width: number | undefined = undefined; | ||
export let value = 0; | ||
export let value: number | null = 0; | ||
export let show_label: boolean; | ||
export let minimum: number | undefined = undefined; | ||
export let maximum: number | undefined = undefined; | ||
|
@@ -31,13 +31,17 @@ | |
export let interactive: boolean; | ||
|
||
function handle_change(): void { | ||
if (!isNaN(value) && value !== null) { | ||
if (!isNaN(parseInt(input_value)) && value !== null) { | ||
value = parseInt(input_value); | ||
gradio.dispatch("change"); | ||
if (!value_is_output) { | ||
gradio.dispatch("input"); | ||
} | ||
} | ||
} | ||
|
||
$: input_value = value === null ? "" : value.toString(); | ||
|
||
afterUpdate(() => { | ||
value_is_output = false; | ||
}); | ||
|
@@ -46,11 +50,11 @@ | |
await tick(); | ||
if (e.key === "Enter") { | ||
e.preventDefault(); | ||
handle_change(); | ||
gradio.dispatch("submit"); | ||
} | ||
} | ||
|
||
$: value, handle_change(); | ||
$: disabled = !interactive; | ||
</script> | ||
|
||
|
@@ -73,11 +77,13 @@ | |
<BlockTitle {show_label} {info}>{label}</BlockTitle> | ||
<input | ||
aria-label={label} | ||
type="number" | ||
bind:value | ||
type="text" | ||
bind:value={input_value} | ||
min={minimum} | ||
max={maximum} | ||
{step} | ||
on:change={handle_change} | ||
on:input={handle_change} | ||
on:keypress={handle_keypress} | ||
on:blur={() => gradio.dispatch("blur")} | ||
on:focus={() => gradio.dispatch("focus")} | ||
|
@@ -96,7 +102,7 @@ | |
border: var(--input-border-width) solid var(--input-border-color); | ||
border-radius: var(--input-radius); | ||
} | ||
input[type="number"] { | ||
input[type="text"] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this change should be needed since at least according to the original issue, it is in theory possible to set a numeric field blank:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thats a good point, we don't want to lose that :/ |
||
display: block; | ||
position: relative; | ||
outline: none !important; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this change should have no effect since the first branch terminates in a return statement, am I misunderstanding?