input.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """An input component."""
  2. from reflex.components.chakra import (
  3. ChakraComponent,
  4. LiteralButtonSize,
  5. LiteralInputVariant,
  6. )
  7. from reflex.components.component import Component
  8. from reflex.components.core.debounce import DebounceInput
  9. from reflex.components.literals import LiteralInputType
  10. from reflex.constants import MemoizationMode
  11. from reflex.event import EventHandler
  12. from reflex.utils.imports import ImportDict
  13. from reflex.vars import Var
  14. class Input(ChakraComponent):
  15. """The Input component is a component that is used to get user input in a text field."""
  16. tag = "Input"
  17. # State var to bind the input.
  18. value: Var[str]
  19. # The default value of the input.
  20. default_value: Var[str]
  21. # The placeholder text.
  22. placeholder: Var[str]
  23. # The type of input.
  24. type_: Var[LiteralInputType]
  25. # The border color when the input is invalid.
  26. error_border_color: Var[str]
  27. # The border color when the input is focused.
  28. focus_border_color: Var[str]
  29. # If true, the form control will be disabled. This has 2 side effects - The FormLabel will have `data-disabled` attribute - The form element (e.g, Input) will be disabled
  30. is_disabled: Var[bool]
  31. # If true, the form control will be invalid. This has 2 side effects - The FormLabel and FormErrorIcon will have `data-invalid` set to true - The form element (e.g, Input) will have `aria-invalid` set to true
  32. is_invalid: Var[bool]
  33. # If true, the form control will be readonly.
  34. is_read_only: Var[bool]
  35. # If true, the form control will be required. This has 2 side effects - The FormLabel will show a required indicator - The form element (e.g, Input) will have `aria-required` set to true
  36. is_required: Var[bool]
  37. # "outline" | "filled" | "flushed" | "unstyled"
  38. variant: Var[LiteralInputVariant]
  39. # "lg" | "md" | "sm" | "xs"
  40. size: Var[LiteralButtonSize]
  41. # The name of the form field
  42. name: Var[str]
  43. # Fired when the input value changes.
  44. on_change: EventHandler[lambda e0: [e0.target.value]]
  45. # Fired when the input is focused.
  46. on_focus: EventHandler[lambda e0: [e0.target.value]]
  47. # Fired when the input lose focus.
  48. on_blur: EventHandler[lambda e0: [e0.target.value]]
  49. # Fired when a key is pressed down.
  50. on_key_down: EventHandler[lambda e0: [e0.key]]
  51. # Fired when a key is released.
  52. on_key_up: EventHandler[lambda e0: [e0.key]]
  53. def add_imports(self) -> ImportDict:
  54. """Add imports for the Input component.
  55. Returns:
  56. The import dict.
  57. """
  58. return {"/utils/state": "set_val"}
  59. @classmethod
  60. def create(cls, *children, **props) -> Component:
  61. """Create an Input component.
  62. Args:
  63. *children: The children of the component.
  64. **props: The properties of the component.
  65. Returns:
  66. The component.
  67. """
  68. if props.get("value") is not None and props.get("on_change") is not None:
  69. # create a debounced input if the user requests full control to avoid typing jank
  70. return DebounceInput.create(super().create(*children, **props))
  71. return super().create(*children, **props)
  72. class InputGroup(ChakraComponent):
  73. """The InputGroup component is a component that is used to group a set of inputs."""
  74. tag = "InputGroup"
  75. _memoization_mode = MemoizationMode(recursive=False)
  76. class InputLeftAddon(ChakraComponent):
  77. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  78. tag = "InputLeftAddon"
  79. class InputRightAddon(ChakraComponent):
  80. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  81. tag = "InputRightAddon"
  82. class InputLeftElement(ChakraComponent):
  83. """The InputLeftElement component is a component that is used to add an element to the left of an input."""
  84. tag = "InputLeftElement"
  85. class InputRightElement(ChakraComponent):
  86. """The InputRightElement component is a component that is used to add an element to the right of an input."""
  87. tag = "InputRightElement"