input.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.constants import EventTriggers, MemoizationMode
  11. from reflex.utils import imports
  12. from reflex.vars import Var
  13. class Input(ChakraComponent):
  14. """The Input component is a component that is used to get user input in a text field."""
  15. tag: str = "Input"
  16. # State var to bind the input.
  17. value: Var[str]
  18. # The default value of the input.
  19. default_value: Var[str]
  20. # The placeholder text.
  21. placeholder: Var[str]
  22. # The type of input.
  23. type_: Var[str] = "text" # type: ignore
  24. # The border color when the input is invalid.
  25. error_border_color: Var[str]
  26. # The border color when the input is focused.
  27. focus_border_color: Var[str]
  28. # 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
  29. is_disabled: Var[bool]
  30. # 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
  31. is_invalid: Var[bool]
  32. # If true, the form control will be readonly.
  33. is_read_only: Var[bool]
  34. # 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
  35. is_required: Var[bool]
  36. # "outline" | "filled" | "flushed" | "unstyled"
  37. variant: Var[LiteralInputVariant]
  38. # "lg" | "md" | "sm" | "xs"
  39. size: Var[LiteralButtonSize]
  40. # The name of the form field
  41. name: Var[str]
  42. def _get_imports(self) -> imports.ImportDict:
  43. return imports.merge_imports(
  44. super()._get_imports(),
  45. {"/utils/state": {imports.ImportVar(tag="set_val")}},
  46. )
  47. def get_event_triggers(self) -> Dict[str, Any]:
  48. """Get the event triggers that pass the component's value to the handler.
  49. Returns:
  50. A dict mapping the event trigger to the var that is passed to the handler.
  51. """
  52. return {
  53. **super().get_event_triggers(),
  54. EventTriggers.ON_CHANGE: lambda e0: [e0.target.value],
  55. EventTriggers.ON_FOCUS: lambda e0: [e0.target.value],
  56. EventTriggers.ON_BLUR: lambda e0: [e0.target.value],
  57. EventTriggers.ON_KEY_DOWN: lambda e0: [e0.key],
  58. EventTriggers.ON_KEY_UP: lambda e0: [e0.key],
  59. }
  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. if (
  70. isinstance(props.get("value"), Var) and props.get("on_change")
  71. ) or "debounce_timeout" in props:
  72. # Currently default to 50ms, which appears to be a good balance
  73. debounce_timeout = props.pop("debounce_timeout", 50)
  74. # create a debounced input if the user requests full control to avoid typing jank
  75. return DebounceInput.create(
  76. super().create(*children, **props), debounce_timeout=debounce_timeout
  77. )
  78. return super().create(*children, **props)
  79. class InputGroup(ChakraComponent):
  80. """The InputGroup component is a component that is used to group a set of inputs."""
  81. tag: str = "InputGroup"
  82. _memoization_mode = MemoizationMode(recursive=False)
  83. class InputLeftAddon(ChakraComponent):
  84. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  85. tag: str = "InputLeftAddon"
  86. class InputRightAddon(ChakraComponent):
  87. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  88. tag: str = "InputRightAddon"
  89. class InputLeftElement(ChakraComponent):
  90. """The InputLeftElement component is a component that is used to add an element to the left of an input."""
  91. tag: str = "InputLeftElement"
  92. class InputRightElement(ChakraComponent):
  93. """The InputRightElement component is a component that is used to add an element to the right of an input."""
  94. tag: str = "InputRightElement"