carousel.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import annotations
  2. from typing import Any, Optional, Union, cast
  3. from ..context import context
  4. from ..events import Handler, ValueChangeEventArguments
  5. from .mixins.disableable_element import DisableableElement
  6. from .mixins.value_element import ValueElement
  7. class Carousel(ValueElement):
  8. def __init__(self, *,
  9. value: Union[str, CarouselSlide, None] = None,
  10. on_value_change: Optional[Handler[ValueChangeEventArguments]] = None,
  11. animated: bool = False,
  12. arrows: bool = False,
  13. navigation: bool = False,
  14. ) -> None:
  15. """Carousel
  16. This element represents `Quasar's QCarousel <https://quasar.dev/vue-components/carousel#qcarousel-api>`_ component.
  17. It contains individual carousel slides.
  18. :param value: `ui.carousel_slide` or name of the slide to be initially selected (default: `None` meaning the first slide)
  19. :param on_value_change: callback to be executed when the selected slide changes
  20. :param animated: whether to animate slide transitions (default: `False`)
  21. :param arrows: whether to show arrows for manual slide navigation (default: `False`)
  22. :param navigation: whether to show navigation dots for manual slide navigation (default: `False`)
  23. """
  24. super().__init__(tag='q-carousel', value=value, on_value_change=on_value_change)
  25. self._props['animated'] = animated
  26. self._props['arrows'] = arrows
  27. self._props['navigation'] = navigation
  28. def _value_to_model_value(self, value: Any) -> Any:
  29. return value.props['name'] if isinstance(value, CarouselSlide) else value
  30. def _handle_value_change(self, value: Any) -> None:
  31. super()._handle_value_change(value)
  32. names = [slide.props['name'] for slide in self.default_slot]
  33. for i, slide in enumerate(self):
  34. done = i < names.index(value) if value in names else False
  35. slide.props(f':done={done}')
  36. def next(self) -> None:
  37. """Show the next slide."""
  38. self.run_method('next')
  39. def previous(self) -> None:
  40. """Show the previous slide."""
  41. self.run_method('previous')
  42. class CarouselSlide(DisableableElement):
  43. def __init__(self, name: Optional[str] = None) -> None:
  44. """Carousel Slide
  45. This element represents `Quasar's QCarouselSlide <https://quasar.dev/vue-components/carousel#qcarouselslide-api>`_ component.
  46. It is a child of a `ui.carousel` element.
  47. :param name: name of the slide (will be the value of the `ui.carousel` element, auto-generated if `None`)
  48. """
  49. super().__init__(tag='q-carousel-slide')
  50. self.carousel = cast(ValueElement, context.slot.parent)
  51. name = name or f'slide_{len(self.carousel.default_slot.children)}'
  52. self._props['name'] = name
  53. self._classes.append('nicegui-carousel-slide')
  54. if self.carousel.value is None:
  55. self.carousel.value = name