1
0

input.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """An input component."""
  2. from typing import Any, Dict
  3. from reflex.components.component import Component
  4. from reflex.components.forms.debounce import DebounceInput
  5. from reflex.components.libs.chakra import (
  6. ChakraComponent,
  7. LiteralButtonSize,
  8. LiteralInputVariant,
  9. )
  10. from reflex.components.literals import LiteralInputType
  11. from reflex.constants import EventTriggers, MemoizationMode
  12. from reflex.utils import imports
  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] = "text" # type: ignore
  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. def _get_imports(self) -> imports.ImportDict:
  44. return imports.merge_imports(
  45. super()._get_imports(),
  46. {"/utils/state": {imports.ImportVar(tag="set_val")}},
  47. )
  48. def get_event_triggers(self) -> Dict[str, Any]:
  49. """Get the event triggers that pass the component's value to the handler.
  50. Returns:
  51. A dict mapping the event trigger to the var that is passed to the handler.
  52. """
  53. return {
  54. **super().get_event_triggers(),
  55. EventTriggers.ON_CHANGE: lambda e0: [e0.target.value],
  56. EventTriggers.ON_FOCUS: lambda e0: [e0.target.value],
  57. EventTriggers.ON_BLUR: lambda e0: [e0.target.value],
  58. EventTriggers.ON_KEY_DOWN: lambda e0: [e0.key],
  59. EventTriggers.ON_KEY_UP: lambda e0: [e0.key],
  60. }
  61. @classmethod
  62. def create(cls, *children, **props) -> Component:
  63. """Create an Input component.
  64. Args:
  65. *children: The children of the component.
  66. **props: The properties of the component.
  67. Returns:
  68. The component.
  69. """
  70. if (
  71. isinstance(props.get("value"), Var) and props.get("on_change")
  72. ) or "debounce_timeout" in props:
  73. # Currently default to 50ms, which appears to be a good balance
  74. debounce_timeout = props.pop("debounce_timeout", 50)
  75. # create a debounced input if the user requests full control to avoid typing jank
  76. return DebounceInput.create(
  77. super().create(*children, **props), debounce_timeout=debounce_timeout
  78. )
  79. return super().create(*children, **props)
  80. class InputGroup(ChakraComponent):
  81. """The InputGroup component is a component that is used to group a set of inputs."""
  82. tag = "InputGroup"
  83. _memoization_mode = MemoizationMode(recursive=False)
  84. class InputLeftAddon(ChakraComponent):
  85. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  86. tag = "InputLeftAddon"
  87. class InputRightAddon(ChakraComponent):
  88. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  89. tag = "InputRightAddon"
  90. class InputLeftElement(ChakraComponent):
  91. """The InputLeftElement component is a component that is used to add an element to the left of an input."""
  92. tag = "InputLeftElement"
  93. class InputRightElement(ChakraComponent):
  94. """The InputRightElement component is a component that is used to add an element to the right of an input."""
  95. tag = "InputRightElement"