editable.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """An editable component."""
  2. from __future__ import annotations
  3. from typing import Any, Union
  4. from reflex.components.libs.chakra import ChakraComponent
  5. from reflex.constants import EventTriggers
  6. from reflex.vars import Var
  7. class Editable(ChakraComponent):
  8. """The wrapper component that provides context value."""
  9. tag: str = "Editable"
  10. # If true, the Editable will be disabled.
  11. is_disabled: Var[bool]
  12. # If true, the read only view, has a tabIndex set to 0 so it can receive focus via the keyboard or click.
  13. is_preview_focusable: Var[bool]
  14. # The placeholder text when the value is empty.
  15. placeholder: Var[str]
  16. # If true, the input's text will be highlighted on focus.
  17. select_all_on_focus: Var[bool]
  18. # If true, the Editable will start with edit mode by default.
  19. start_with_edit_view: Var[bool]
  20. # If true, it'll update the value onBlur and turn off the edit mode.
  21. submit_on_blur: Var[bool]
  22. # The value of the Editable in both edit & preview mode
  23. value: Var[str]
  24. # The initial value of the Editable in both edit and preview mode.
  25. default_value: Var[str]
  26. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  27. """Get the event triggers that pass the component's value to the handler.
  28. Returns:
  29. A dict mapping the event trigger to the var that is passed to the handler.
  30. """
  31. return {
  32. **super().get_event_triggers(),
  33. EventTriggers.ON_CHANGE: lambda e0: [e0],
  34. EventTriggers.ON_EDIT: lambda e0: [e0],
  35. EventTriggers.ON_SUBMIT: lambda e0: [e0],
  36. EventTriggers.ON_CANCEL: lambda e0: [e0],
  37. }
  38. class EditableInput(ChakraComponent):
  39. """The edit view of the component. It shows when you click or focus on the text."""
  40. tag: str = "EditableInput"
  41. class EditableTextarea(ChakraComponent):
  42. """Use the textarea element to handle multi line text input in an editable context."""
  43. tag: str = "EditableTextarea"
  44. class EditablePreview(ChakraComponent):
  45. """The read-only view of the component."""
  46. tag: str = "EditablePreview"