textarea.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """A textarea component."""
  2. from __future__ import annotations
  3. from typing import Any, Optional, Union
  4. from reflex.components.chakra import ChakraComponent, LiteralInputVariant
  5. from reflex.components.component import Component
  6. from reflex.components.core.debounce import DebounceInput
  7. from reflex.constants import EventTriggers
  8. from reflex.vars import Var
  9. class TextArea(ChakraComponent):
  10. """A text area component."""
  11. tag = "Textarea"
  12. # State var to bind the input.
  13. value: Optional[Var[str]] = None
  14. # The default value of the textarea.
  15. default_value: Optional[Var[str]] = None
  16. # The placeholder text.
  17. placeholder: Optional[Var[str]] = None
  18. # The border color when the input is invalid.
  19. error_border_color: Optional[Var[str]] = None
  20. # The border color when the input is focused.
  21. focus_border_color: Optional[Var[str]] = None
  22. # If true, the form control will be disabled.
  23. is_disabled: Optional[Var[bool]] = None
  24. # If true, the form control will be invalid.
  25. is_invalid: Optional[Var[bool]] = None
  26. # If true, the form control will be read-only.
  27. is_read_only: Optional[Var[bool]] = None
  28. # If true, the form control will be required.
  29. is_required: Optional[Var[bool]] = None
  30. # "outline" | "filled" | "flushed" | "unstyled"
  31. variant: Optional[Var[LiteralInputVariant]] = None
  32. # The name of the form field
  33. name: Optional[Var[str]] = None
  34. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  35. """Get the event triggers that pass the component's value to the handler.
  36. Returns:
  37. A dict mapping the event trigger to the var that is passed to the handler.
  38. """
  39. return {
  40. **super().get_event_triggers(),
  41. EventTriggers.ON_CHANGE: lambda e0: [e0.target.value],
  42. EventTriggers.ON_FOCUS: lambda e0: [e0.target.value],
  43. EventTriggers.ON_BLUR: lambda e0: [e0.target.value],
  44. EventTriggers.ON_KEY_DOWN: lambda e0: [e0.key],
  45. EventTriggers.ON_KEY_UP: lambda e0: [e0.key],
  46. }
  47. @classmethod
  48. def create(cls, *children, **props) -> Component:
  49. """Create an Input component.
  50. Args:
  51. *children: The children of the component.
  52. **props: The properties of the component.
  53. Returns:
  54. The component.
  55. """
  56. if props.get("value") is not None and props.get("on_change"):
  57. # create a debounced input if the user requests full control to avoid typing jank
  58. return DebounceInput.create(super().create(*children, **props))
  59. return super().create(*children, **props)