editable.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """An editable component."""
  2. from typing import Dict
  3. from reflex.components.component import Component
  4. from reflex.components.forms.debounce import DebounceInput
  5. from reflex.components.libs.chakra import ChakraComponent
  6. from reflex.event import EVENT_ARG
  7. from reflex.vars import Var
  8. class Editable(ChakraComponent):
  9. """The wrapper component that provides context value."""
  10. tag = "Editable"
  11. # If true, the Editable will be disabled.
  12. is_disabled: Var[bool]
  13. # If true, the read only view, has a tabIndex set to 0 so it can receive focus via the keyboard or click.
  14. is_preview_focusable: Var[bool]
  15. # The placeholder text when the value is empty.
  16. placeholder: Var[str]
  17. # If true, the input's text will be highlighted on focus.
  18. select_all_on_focus: Var[bool]
  19. # If true, the Editable will start with edit mode by default.
  20. start_with_edit_view: Var[bool]
  21. # If true, it'll update the value onBlur and turn off the edit mode.
  22. submit_on_blur: Var[bool]
  23. # The value of the Editable in both edit & preview mode
  24. value: Var[str]
  25. # The initial value of the Editable in both edit and preview mode.
  26. default_value: Var[str]
  27. @classmethod
  28. def create(cls, *children, **props) -> Component:
  29. """Create an Editable component.
  30. Args:
  31. children: The children of the component.
  32. props: The properties of the component.
  33. Returns:
  34. The component.
  35. """
  36. if (
  37. isinstance(props.get("value"), Var) and props.get("on_change")
  38. ) or "debounce_timeout" in props:
  39. # Create a debounced input if the user requests full control to avoid typing jank
  40. # Currently default to 50ms, which appears to be a good balance
  41. debounce_timeout = props.pop("debounce_timeout", 50)
  42. return DebounceInput.create(
  43. super().create(*children, **props),
  44. debounce_timeout=debounce_timeout,
  45. )
  46. return super().create(*children, **props)
  47. def get_controlled_triggers(self) -> Dict[str, Var]:
  48. """Get the event triggers that pass the component's value to the handler.
  49. Returns:
  50. A dict mapping the event trigger to the var that is passed to the handler.
  51. """
  52. return {
  53. "on_change": EVENT_ARG,
  54. "on_edit": EVENT_ARG,
  55. "on_submit": EVENT_ARG,
  56. "on_cancel": EVENT_ARG,
  57. }
  58. class EditableInput(ChakraComponent):
  59. """The edit view of the component. It shows when you click or focus on the text."""
  60. tag = "EditableInput"
  61. class EditableTextarea(ChakraComponent):
  62. """Use the textarea element to handle multi line text input in an editable context."""
  63. tag = "EditableTextarea"
  64. class EditablePreview(ChakraComponent):
  65. """The read-only view of the component."""
  66. tag = "EditablePreview"