input.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. def _get_imports(self) -> imports.ImportDict:
  36. return imports.merge_imports(
  37. super()._get_imports(),
  38. {"/utils/state": {ImportVar(tag="set_val")}},
  39. )
  40. def get_controlled_triggers(self) -> Dict[str, Var]:
  41. """Get the event triggers that pass the component's value to the handler.
  42. Returns:
  43. A dict mapping the event trigger to the var that is passed to the handler.
  44. """
  45. return {
  46. "on_change": EVENT_ARG.target.value,
  47. "on_focus": EVENT_ARG.target.value,
  48. "on_blur": EVENT_ARG.target.value,
  49. "on_key_down": EVENT_ARG.key,
  50. "on_key_up": EVENT_ARG.key,
  51. }
  52. @classmethod
  53. def create(cls, *children, **props) -> Component:
  54. """Create an Input component.
  55. Args:
  56. children: The children of the component.
  57. props: The properties of the component.
  58. Returns:
  59. The component.
  60. """
  61. if isinstance(props.get("value"), Var) and props.get("on_change"):
  62. # create a debounced input if the user requests full control to avoid typing jank
  63. return DebounceInput.create(
  64. super().create(*children, **props), debounce_timeout=0
  65. )
  66. return super().create(*children, **props)
  67. class InputGroup(ChakraComponent):
  68. """The InputGroup component is a component that is used to group a set of inputs."""
  69. tag = "InputGroup"
  70. class InputLeftAddon(ChakraComponent):
  71. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  72. tag = "InputLeftAddon"
  73. class InputRightAddon(ChakraComponent):
  74. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  75. tag = "InputRightAddon"
  76. class InputLeftElement(ChakraComponent):
  77. """The InputLeftElement component is a component that is used to add an element to the left of an input."""
  78. tag = "InputLeftElement"
  79. class InputRightElement(ChakraComponent):
  80. """The InputRightElement component is a component that is used to add an element to the right of an input."""
  81. tag = "InputRightElement"