textarea.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """A textarea component."""
  2. from __future__ import annotations
  3. from typing import Any, 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: Var[str]
  14. # The default value of the textarea.
  15. default_value: Var[str]
  16. # The placeholder text.
  17. placeholder: Var[str]
  18. # The border color when the input is invalid.
  19. error_border_color: Var[str]
  20. # The border color when the input is focused.
  21. focus_border_color: Var[str]
  22. # If true, the form control will be disabled.
  23. is_disabled: Var[bool]
  24. # If true, the form control will be invalid.
  25. is_invalid: Var[bool]
  26. # If true, the form control will be read-only.
  27. is_read_only: Var[bool]
  28. # If true, the form control will be required.
  29. is_required: Var[bool]
  30. # "outline" | "filled" | "flushed" | "unstyled"
  31. variant: Var[LiteralInputVariant]
  32. # The name of the form field
  33. name: Var[str]
  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 (
  57. isinstance(props.get("value"), Var) and props.get("on_change")
  58. ) or "debounce_timeout" in props:
  59. # Currently default to 50ms, which appears to be a good balance
  60. debounce_timeout = props.pop("debounce_timeout", 50)
  61. # create a debounced input if the user requests full control to avoid typing jank
  62. return DebounceInput.create(
  63. super().create(*children, **props), debounce_timeout=debounce_timeout
  64. )
  65. return super().create(*children, **props)