slider.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """A slider component."""
  2. from __future__ import annotations
  3. from typing import Any, Union
  4. from reflex.components.component import Component
  5. from reflex.components.graphing.recharts.recharts import LiteralLayout
  6. from reflex.components.libs.chakra import ChakraComponent, LiteralChakraDirection
  7. from reflex.constants import EventTriggers
  8. from reflex.vars import Var
  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: Var[int]
  14. # The color scheme.
  15. color_scheme: Var[str]
  16. # The placeholder text.
  17. default_value: Var[int]
  18. # The writing mode ("ltr" | "rtl")
  19. direction: Var[LiteralChakraDirection]
  20. # If false, the slider handle will not capture focus when value changes.
  21. focus_thumb_on_change: Var[bool]
  22. # If true, the slider will be disabled
  23. is_disabled: Var[bool]
  24. # If true, the slider will be in `read-only` state.
  25. is_read_only: Var[bool]
  26. # If true, the value will be incremented or decremented in reverse.
  27. is_reversed: Var[bool]
  28. # The minimum value of the slider.
  29. min_: Var[int]
  30. # The maximum value of the slider.
  31. max_: Var[int]
  32. # The step in which increments/decrements have to be made
  33. step: Var[int]
  34. # The minimum distance between slider thumbs. Useful for preventing the thumbs from being too close together.
  35. min_steps_between_thumbs: Var[int]
  36. # Oreintation of the slider vertical | horizontal.
  37. orientation: Var[LiteralLayout]
  38. # Minimum height of the slider.
  39. min_h: Var[str]
  40. # Minimum width of the slider.
  41. min_w: Var[str]
  42. # Maximum height of the slider.
  43. max_h: Var[str]
  44. # Maximum width of the slider.
  45. max_w: Var[str]
  46. # The name of the form field
  47. name: Var[str]
  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: Var[str]
  88. class SliderMark(ChakraComponent):
  89. """The label or mark that shows names for specific slider values."""
  90. tag = "SliderMark"