rangeslider.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """A range slider component."""
  2. from typing import Dict, List
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.event import EVENT_ARG
  6. from pynecone.vars import Var
  7. class RangeSlider(ChakraComponent):
  8. """The RangeSlider is a multi thumb slider used to select a range of related values. A common use-case of this component is a price range picker that allows a user to set the minimum and maximum price."""
  9. tag = "RangeSlider"
  10. # State var to bind the the input.
  11. value: Var[List[int]]
  12. # The default values.
  13. default_value: Var[List[int]]
  14. # The writing mode ("ltr" | "rtl")
  15. direction: Var[str]
  16. # If false, the slider handle will not capture focus when value changes.
  17. focus_thumb_on_change: Var[bool]
  18. # If true, the slider will be disabled
  19. is_disabled: Var[bool]
  20. # If true, the slider will be in `read-only` state.
  21. is_read_only: Var[bool]
  22. # If true, the value will be incremented or decremented in reverse.
  23. is_reversed: Var[bool]
  24. # The minimum value of the slider.
  25. min_: Var[int]
  26. # The maximum value of the slider.
  27. max_: Var[int]
  28. # The minimum distance between slider thumbs. Useful for preventing the thumbs from being too close together.
  29. min_steps_between_thumbs: Var[int]
  30. def get_controlled_triggers(self) -> Dict[str, Var]:
  31. """Get the event triggers that pass the component's value to the handler.
  32. Returns:
  33. A dict mapping the event trigger to the var that is passed to the handler.
  34. """
  35. return {
  36. "on_change": EVENT_ARG,
  37. "on_change_end": EVENT_ARG,
  38. "on_change_start": EVENT_ARG,
  39. }
  40. @classmethod
  41. def create(cls, *children, **props) -> Component:
  42. """Create a RangeSlider component.
  43. If no children are provided, a default RangeSlider will be created.
  44. Args:
  45. children: The children of the component.
  46. props: The properties of the component.
  47. Returns:
  48. The RangeSlider component.
  49. """
  50. if len(children) == 0:
  51. children = [
  52. RangeSliderTrack.create(
  53. RangeSliderFilledTrack.create(),
  54. ),
  55. RangeSliderThumb.create(index=0),
  56. RangeSliderThumb.create(index=1),
  57. ]
  58. return super().create(*children, **props)
  59. class RangeSliderTrack(ChakraComponent):
  60. """A range slider track."""
  61. tag = "RangeSliderTrack"
  62. class RangeSliderFilledTrack(ChakraComponent):
  63. """A filled range slider track."""
  64. tag = "RangeSliderFilledTrack"
  65. class RangeSliderThumb(ChakraComponent):
  66. """A range slider thumb."""
  67. tag = "RangeSliderThumb"
  68. # The position of the thumb.
  69. index: Var[int]