input.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """An input component."""
  2. from typing import Dict
  3. from reflex.components.component import EVENT_ARG, Component
  4. from reflex.components.forms.debounce import DebounceInput
  5. from reflex.components.libs.chakra import ChakraComponent
  6. from reflex.utils import imports
  7. from reflex.vars import ImportVar, Var
  8. class Input(ChakraComponent):
  9. """The Input component is a component that is used to get user input in a text field."""
  10. tag = "Input"
  11. # State var to bind the input.
  12. value: Var[str]
  13. # The default value of the input.
  14. default_value: Var[str]
  15. # The placeholder text.
  16. placeholder: Var[str]
  17. # The type of input.
  18. type_: Var[str] = "text" # type: ignore
  19. # The border color when the input is invalid.
  20. error_border_color: Var[str]
  21. # The border color when the input is focused.
  22. focus_border_color: Var[str]
  23. # 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
  24. is_disabled: Var[bool]
  25. # 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
  26. is_invalid: Var[bool]
  27. # If true, the form control will be readonly.
  28. is_read_only: Var[bool]
  29. # 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
  30. is_required: Var[bool]
  31. # "outline" | "filled" | "flushed" | "unstyled"
  32. variant: Var[str]
  33. # "lg" | "md" | "sm" | "xs"
  34. size: Var[str]
  35. # Time in milliseconds to wait between end of input and triggering on_change
  36. debounce_timeout: Var[int]
  37. def _get_imports(self) -> imports.ImportDict:
  38. return imports.merge_imports(
  39. super()._get_imports(),
  40. {"/utils/state": {ImportVar(tag="set_val")}},
  41. )
  42. def get_controlled_triggers(self) -> Dict[str, Var]:
  43. """Get the event triggers that pass the component's value to the handler.
  44. Returns:
  45. A dict mapping the event trigger to the var that is passed to the handler.
  46. """
  47. return {
  48. "on_change": EVENT_ARG.target.value,
  49. "on_focus": EVENT_ARG.target.value,
  50. "on_blur": EVENT_ARG.target.value,
  51. "on_key_down": EVENT_ARG.key,
  52. "on_key_up": EVENT_ARG.key,
  53. }
  54. @classmethod
  55. def create(cls, *children, **props) -> Component:
  56. """Create an Input component.
  57. Args:
  58. children: The children of the component.
  59. props: The properties of the component.
  60. Returns:
  61. The component.
  62. """
  63. if isinstance(props.get("value"), Var) and props.get("on_change"):
  64. # create a debounced input if the user requests full control to avoid typing jank
  65. return DebounceInput.create(
  66. super().create(*children, **props),
  67. # Currently default to 50ms, which appears to be a good balance
  68. debounce_timeout=props.get("debounce_timeout", 50),
  69. )
  70. return super().create(*children, **props)
  71. class InputGroup(ChakraComponent):
  72. """The InputGroup component is a component that is used to group a set of inputs."""
  73. tag = "InputGroup"
  74. class InputLeftAddon(ChakraComponent):
  75. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  76. tag = "InputLeftAddon"
  77. class InputRightAddon(ChakraComponent):
  78. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  79. tag = "InputRightAddon"
  80. class InputLeftElement(ChakraComponent):
  81. """The InputLeftElement component is a component that is used to add an element to the left of an input."""
  82. tag = "InputLeftElement"
  83. class InputRightElement(ChakraComponent):
  84. """The InputRightElement component is a component that is used to add an element to the right of an input."""
  85. tag = "InputRightElement"