slider.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """A slider component."""
  2. from __future__ import annotations
  3. from typing import Any, Literal, Optional, Union
  4. from reflex.components.chakra import ChakraComponent, LiteralChakraDirection
  5. from reflex.components.component import Component
  6. from reflex.constants import EventTriggers
  7. from reflex.vars import Var
  8. LiteralLayout = Literal["horizontal", "vertical"]
  9. class Slider(ChakraComponent):
  10. """The wrapper that provides context and functionality for all children."""
  11. tag = "Slider"
  12. # State var to bind the input.
  13. value: Optional[Var[int]] = None
  14. # The color scheme.
  15. color_scheme: Optional[Var[str]] = None
  16. # The placeholder text.
  17. default_value: Optional[Var[int]] = None
  18. # The writing mode ("ltr" | "rtl")
  19. direction: Optional[Var[LiteralChakraDirection]] = None
  20. # If false, the slider handle will not capture focus when value changes.
  21. focus_thumb_on_change: Optional[Var[bool]] = None
  22. # If true, the slider will be disabled
  23. is_disabled: Optional[Var[bool]] = None
  24. # If true, the slider will be in `read-only` state.
  25. is_read_only: Optional[Var[bool]] = None
  26. # If true, the value will be incremented or decremented in reverse.
  27. is_reversed: Optional[Var[bool]] = None
  28. # The minimum value of the slider.
  29. min_: Optional[Var[int]] = None
  30. # The maximum value of the slider.
  31. max_: Optional[Var[int]] = None
  32. # The step in which increments/decrements have to be made
  33. step: Optional[Var[int]] = None
  34. # The minimum distance between slider thumbs. Useful for preventing the thumbs from being too close together.
  35. min_steps_between_thumbs: Optional[Var[int]] = None
  36. # Oreintation of the slider vertical | horizontal.
  37. orientation: Optional[Var[LiteralLayout]] = None
  38. # Minimum height of the slider.
  39. min_h: Optional[Var[str]] = None
  40. # Minimum width of the slider.
  41. min_w: Optional[Var[str]] = None
  42. # Maximum height of the slider.
  43. max_h: Optional[Var[str]] = None
  44. # Maximum width of the slider.
  45. max_w: Optional[Var[str]] = None
  46. # The name of the form field
  47. name: Optional[Var[str]] = None
  48. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  49. """Get the event triggers that pass the component's value to the handler.
  50. Returns:
  51. A dict mapping the event trigger to the var that is passed to the handler.
  52. """
  53. return {
  54. **super().get_event_triggers(),
  55. EventTriggers.ON_CHANGE: lambda e0: [e0],
  56. EventTriggers.ON_CHANGE_END: lambda e0: [e0],
  57. EventTriggers.ON_CHANGE_START: lambda e0: [e0],
  58. } # type: ignore
  59. @classmethod
  60. def create(cls, *children, **props) -> Component:
  61. """Create a slider component.
  62. If no children are provided, a default slider will be created.
  63. Args:
  64. *children: The children of the component.
  65. **props: The properties of the component.
  66. Returns:
  67. The slider component.
  68. """
  69. if len(children) == 0:
  70. children = [
  71. SliderTrack.create(
  72. SliderFilledTrack.create(),
  73. ),
  74. SliderThumb.create(),
  75. ]
  76. return super().create(*children, **props)
  77. class SliderTrack(ChakraComponent):
  78. """The empty part of the slider that shows the track."""
  79. tag = "SliderTrack"
  80. class SliderFilledTrack(ChakraComponent):
  81. """The filled part of the slider."""
  82. tag = "SliderFilledTrack"
  83. class SliderThumb(ChakraComponent):
  84. """The handle that's used to change the slider value."""
  85. tag = "SliderThumb"
  86. # The size of the thumb.
  87. box_size: Optional[Var[str]] = None
  88. class SliderMark(ChakraComponent):
  89. """The label or mark that shows names for specific slider values."""
  90. tag = "SliderMark"