input.py 4.3 KB

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