slider.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """A slider component."""
  2. from typing import Set
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.var import Var
  6. class Slider(ChakraComponent):
  7. """The wrapper that provides context and functionality for all children."""
  8. tag = "Slider"
  9. # State var to bind the the input.
  10. value: Var[int]
  11. # The placeholder text.
  12. default_value: Var[int]
  13. # The writing mode ("ltr" | "rtl")
  14. direction: Var[str]
  15. # If false, the slider handle will not capture focus when value changes.
  16. focus_thumb_on_change: Var[bool]
  17. # If true, the slider will be disabled
  18. is_disabled: Var[bool]
  19. # If true, the slider will be in `read-only` state.
  20. is_read_only: Var[bool]
  21. # If true, the value will be incremented or decremented in reverse.
  22. is_reversed: Var[bool]
  23. # The minimum value of the slider.
  24. min_: Var[int]
  25. # The maximum value of the slider.
  26. max_: Var[int]
  27. # The minimum distance between slider thumbs. Useful for preventing the thumbs from being too close together.
  28. min_steps_between_thumbs: Var[int]
  29. @classmethod
  30. def get_controlled_triggers(cls) -> Set[str]:
  31. """Get the event triggers that pass the component's value to the handler.
  32. Returns:
  33. The controlled event triggers.
  34. """
  35. return {
  36. "on_change",
  37. "on_change_end",
  38. "on_change_start",
  39. }
  40. @classmethod
  41. def create(cls, *children, **props) -> Component:
  42. """Create a slider component.
  43. If no children are provided, a default slider will be created.
  44. Args:
  45. children: The children of the component.
  46. props: The properties of the component.
  47. Returns:
  48. The slider component.
  49. """
  50. if len(children) == 0:
  51. children = [
  52. SliderTrack.create(
  53. SliderFilledTrack.create(),
  54. ),
  55. SliderThumb.create(),
  56. ]
  57. return super().create(*children, **props)
  58. class SliderTrack(ChakraComponent):
  59. """The empty part of the slider that shows the track."""
  60. tag = "SliderTrack"
  61. class SliderFilledTrack(ChakraComponent):
  62. """The filled part of the slider."""
  63. tag = "SliderFilledTrack"
  64. class SliderThumb(ChakraComponent):
  65. """The handle that's used to change the slider value."""
  66. tag = "SliderThumb"
  67. class SliderMark(ChakraComponent):
  68. """The label or mark that shows names for specific slider values."""
  69. tag = "SliderMark"