1313from reflex .event import (
1414 EventChain ,
1515 EventHandler ,
16+ checked_input_event ,
17+ float_input_event ,
1618 input_event ,
19+ int_input_event ,
1720 key_event ,
1821 prevent_default ,
1922)
2023from reflex .utils .imports import ImportDict
21- from reflex .utils .types import is_optional
2224from reflex .vars import VarData
2325from reflex .vars .base import LiteralVar , Var
26+ from reflex .vars .number import ternary_operation
2427
2528from .base import BaseHTML
2629
@@ -294,8 +297,8 @@ def _exclude_props(self) -> list[str]:
294297]
295298
296299
297- class Input (BaseHTML ):
298- """Display the input element ."""
300+ class BaseInput (BaseHTML ):
301+ """A base class for input elements ."""
299302
300303 tag = "input"
301304
@@ -392,6 +395,42 @@ class Input(BaseHTML):
392395 # Value of the input
393396 value : Var [str | int | float ]
394397
398+ # Fired when a key is pressed down
399+ on_key_down : EventHandler [key_event ]
400+
401+ # Fired when a key is released
402+ on_key_up : EventHandler [key_event ]
403+
404+
405+ class CheckboxInput (BaseInput ):
406+ """Display the input element."""
407+
408+ # Fired when the input value changes
409+ on_change : EventHandler [checked_input_event ]
410+
411+ # Fired when the input gains focus
412+ on_focus : EventHandler [checked_input_event ]
413+
414+ # Fired when the input loses focus
415+ on_blur : EventHandler [checked_input_event ]
416+
417+
418+ class ValueNumberInput (BaseInput ):
419+ """Display the input element."""
420+
421+ # Fired when the input value changes
422+ on_change : EventHandler [float_input_event , int_input_event , input_event ]
423+
424+ # Fired when the input gains focus
425+ on_focus : EventHandler [float_input_event , int_input_event , input_event ]
426+
427+ # Fired when the input loses focus
428+ on_blur : EventHandler [float_input_event , int_input_event , input_event ]
429+
430+
431+ class Input (BaseInput ):
432+ """Display the input element."""
433+
395434 # Fired when the input value changes
396435 on_change : EventHandler [input_event ]
397436
@@ -401,12 +440,6 @@ class Input(BaseHTML):
401440 # Fired when the input loses focus
402441 on_blur : EventHandler [input_event ]
403442
404- # Fired when a key is pressed down
405- on_key_down : EventHandler [key_event ]
406-
407- # Fired when a key is released
408- on_key_up : EventHandler [key_event ]
409-
410443 @classmethod
411444 def create (cls , * children , ** props ):
412445 """Create an Input component.
@@ -418,20 +451,25 @@ def create(cls, *children, **props):
418451 Returns:
419452 The component.
420453 """
421- from reflex .vars .number import ternary_operation
422-
423454 value = props .get ("value" )
424455
425456 # React expects an empty string(instead of null) for controlled inputs.
426- if value is not None and is_optional (
427- (value_var := Var .create (value ))._var_type
428- ):
457+ if value is not None :
458+ value_var = Var .create (value )
429459 props ["value" ] = ternary_operation (
430- (value_var != Var .create (None ))
431- & (value_var != Var (_js_expr = "undefined" )),
432- value ,
433- Var .create ("" ),
460+ value_var .is_not_none (), value_var , Var .create ("" )
434461 )
462+
463+ input_type = props .get ("type" )
464+
465+ if input_type == "checkbox" :
466+ # Checkbox inputs should use the CheckboxInput class
467+ return CheckboxInput .create (* children , ** props )
468+
469+ if input_type == "number" or input_type == "range" :
470+ # Number inputs should use the ValueNumberInput class
471+ return ValueNumberInput .create (* children , ** props )
472+
435473 return super ().create (* children , ** props )
436474
437475
0 commit comments