stepper.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from __future__ import annotations
  2. from typing import Any, Callable, Optional, Union, cast
  3. from .. import globals
  4. from ..element import Element
  5. from .mixins.disableable_element import DisableableElement
  6. from .mixins.value_element import ValueElement
  7. class Stepper(ValueElement):
  8. def __init__(self, *,
  9. value: Union[str, Step, None] = None,
  10. on_value_change: Optional[Callable[..., Any]] = None,
  11. ) -> None:
  12. """Stepper
  13. This element represents `Quasar's QStepper <https://quasar.dev/vue-components/stepper#qstepper-api>`_ component.
  14. It contains individual steps.
  15. :param value: `ui.step` or name of the step to be initially selected (default: `None` meaning the first step)
  16. :param on_value_change: callback to be executed when the selected step changes
  17. """
  18. super().__init__(tag='q-stepper', value=value, on_value_change=on_value_change)
  19. def _value_to_model_value(self, value: Any) -> Any:
  20. return value._props['name'] if isinstance(value, Step) else value
  21. def on_value_change(self, value: Any) -> None:
  22. super().on_value_change(value)
  23. names = [step._props['name'] for step in self]
  24. for i, step in enumerate(self):
  25. done = i < names.index(value) if value in names else False
  26. step.props(f':done={done}')
  27. def next(self) -> None:
  28. self.run_method('next')
  29. def previous(self) -> None:
  30. self.run_method('previous')
  31. class Step(DisableableElement):
  32. def __init__(self, name: str, title: Optional[str] = None, icon: Optional[str] = None) -> None:
  33. """Step
  34. This element represents `Quasar's QStep <https://quasar.dev/vue-components/stepper#qstep-api>`_ component.
  35. It is a child of a `ui.stepper` element.
  36. :param name: name of the step (will be the value of the `ui.stepper` element)
  37. :param title: title of the step (default: `None`, meaning the same as `name`)
  38. :param icon: icon of the step (default: `None`)
  39. """
  40. super().__init__(tag='q-step')
  41. self._props['name'] = name
  42. self._props['title'] = title if title is not None else name
  43. if icon:
  44. self._props['icon'] = icon
  45. self.stepper = cast(ValueElement, globals.get_slot().parent)
  46. if self.stepper.value is None:
  47. self.stepper.value = name
  48. class StepperNavigation(Element):
  49. def __init__(self) -> None:
  50. super().__init__('q-stepper-navigation')