textarea.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """A textarea 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.vars import Var
  7. class TextArea(ChakraComponent):
  8. """A text area component."""
  9. tag = "Textarea"
  10. # State var to bind the input.
  11. value: Var[str]
  12. # The default value of the textarea.
  13. default_value: Var[str]
  14. # The placeholder text.
  15. placeholder: Var[str]
  16. # The border color when the input is invalid.
  17. error_border_color: Var[str]
  18. # The border color when the input is focused.
  19. focus_border_color: Var[str]
  20. # If true, the form control will be disabled.
  21. is_disabled: Var[bool]
  22. # If true, the form control will be invalid.
  23. is_invalid: Var[bool]
  24. # If true, the form control will be read-only.
  25. is_read_only: Var[bool]
  26. # If true, the form control will be required.
  27. is_required: Var[bool]
  28. # "outline" | "filled" | "flushed" | "unstyled"
  29. variant: Var[str]
  30. # Time in milliseconds to wait between end of input and triggering on_change
  31. debounce_timeout: Var[int]
  32. def get_controlled_triggers(self) -> Dict[str, Var]:
  33. """Get the event triggers that pass the component's value to the handler.
  34. Returns:
  35. A dict mapping the event trigger to the var that is passed to the handler.
  36. """
  37. return {
  38. "on_change": EVENT_ARG.target.value,
  39. "on_focus": EVENT_ARG.target.value,
  40. "on_blur": EVENT_ARG.target.value,
  41. "on_key_down": EVENT_ARG.key,
  42. "on_key_up": EVENT_ARG.key,
  43. }
  44. @classmethod
  45. def create(cls, *children, **props) -> Component:
  46. """Create an Input component.
  47. Args:
  48. children: The children of the component.
  49. props: The properties of the component.
  50. Returns:
  51. The component.
  52. """
  53. if isinstance(props.get("value"), Var) and props.get("on_change"):
  54. # create a debounced input if the user requests full control to avoid typing jank
  55. return DebounceInput.create(
  56. super().create(*children, **props),
  57. # Currently default to 50ms, which appears to be a good balance
  58. debounce_timeout=props.get("debounce_timeout", 50),
  59. )
  60. return super().create(*children, **props)