text_field.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Interactive components provided by @radix-ui/themes."""
  2. from __future__ import annotations
  3. from typing import Literal, Union
  4. from reflex.components.component import Component, ComponentNamespace
  5. from reflex.components.core.breakpoints import Responsive
  6. from reflex.components.core.debounce import DebounceInput
  7. from reflex.components.el import elements
  8. from reflex.event import EventHandler
  9. from reflex.vars import Var
  10. from ..base import (
  11. LiteralAccentColor,
  12. LiteralRadius,
  13. RadixThemesComponent,
  14. )
  15. LiteralTextFieldSize = Literal["1", "2", "3"]
  16. LiteralTextFieldVariant = Literal["classic", "surface", "soft"]
  17. class TextFieldRoot(elements.Div, RadixThemesComponent):
  18. """Captures user input with an optional slot for buttons and icons."""
  19. tag = "TextField.Root"
  20. # Text field size "1" - "3"
  21. size: Var[Responsive[LiteralTextFieldSize]]
  22. # Variant of text field: "classic" | "surface" | "soft"
  23. variant: Var[LiteralTextFieldVariant]
  24. # Override theme color for text field
  25. color_scheme: Var[LiteralAccentColor]
  26. # Override theme radius for text field: "none" | "small" | "medium" | "large" | "full"
  27. radius: Var[LiteralRadius]
  28. # Whether the input should have autocomplete enabled
  29. auto_complete: Var[bool]
  30. # The value of the input when initially rendered.
  31. default_value: Var[str]
  32. # Disables the input
  33. disabled: Var[bool]
  34. # Specifies the maximum number of characters allowed in the input
  35. max_length: Var[int]
  36. # Specifies the minimum number of characters required in the input
  37. min_length: Var[int]
  38. # Name of the input, used when sending form data
  39. name: Var[str]
  40. # Placeholder text in the input
  41. placeholder: Var[str]
  42. # Indicates whether the input is read-only
  43. read_only: Var[bool]
  44. # Indicates that the input is required
  45. required: Var[bool]
  46. # Specifies the type of input
  47. type: Var[str]
  48. # Value of the input
  49. value: Var[Union[str, int, float]]
  50. # Fired when the value of the textarea changes.
  51. on_change: EventHandler[lambda e0: [e0.target.value]]
  52. # Fired when the textarea is focused.
  53. on_focus: EventHandler[lambda e0: [e0.target.value]]
  54. # Fired when the textarea is blurred.
  55. on_blur: EventHandler[lambda e0: [e0.target.value]]
  56. # Fired when a key is pressed down.
  57. on_key_down: EventHandler[lambda e0: [e0.key]]
  58. # Fired when a key is released.
  59. on_key_up: EventHandler[lambda e0: [e0.key]]
  60. @classmethod
  61. def create(cls, *children, **props) -> Component:
  62. """Create an Input component.
  63. Args:
  64. *children: The children of the component.
  65. **props: The properties of the component.
  66. Returns:
  67. The component.
  68. """
  69. component = super().create(*children, **props)
  70. if props.get("value") is not None and props.get("on_change") is not None:
  71. # create a debounced input if the user requests full control to avoid typing jank
  72. return DebounceInput.create(component)
  73. return component
  74. class TextFieldSlot(RadixThemesComponent):
  75. """Contains icons or buttons associated with an Input."""
  76. tag = "TextField.Slot"
  77. # Override theme color for text field slot
  78. color_scheme: Var[LiteralAccentColor]
  79. class TextField(ComponentNamespace):
  80. """TextField components namespace."""
  81. slot = staticmethod(TextFieldSlot.create)
  82. __call__ = staticmethod(TextFieldRoot.create)
  83. input = text_field = TextField()