stepper.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import annotations
  2. from typing import Any, Callable, Optional, Union, cast
  3. from .. import globals # pylint: disable=redefined-builtin
  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. keep_alive: bool = True,
  12. ) -> None:
  13. """Stepper
  14. This element represents `Quasar's QStepper <https://quasar.dev/vue-components/stepper#qstepper-api>`_ component.
  15. It contains individual steps.
  16. To avoid issues with dynamic elements when switching steps,
  17. this element uses Vue's `keep-alive <https://vuejs.org/guide/built-ins/keep-alive.html>`_ component.
  18. If client-side performance is an issue, you can disable this feature.
  19. :param value: `ui.step` or name of the step to be initially selected (default: `None` meaning the first step)
  20. :param on_value_change: callback to be executed when the selected step changes
  21. :param keep_alive: whether to use Vue's keep-alive component on the content (default: `True`)
  22. """
  23. super().__init__(tag='q-stepper', value=value, on_value_change=on_value_change)
  24. self._props['keep-alive'] = keep_alive
  25. self._classes = ['nicegui-stepper']
  26. def _value_to_model_value(self, value: Any) -> Any:
  27. return value._props['name'] if isinstance(value, Step) else value # pylint: disable=protected-access
  28. def _handle_value_change(self, value: Any) -> None:
  29. super()._handle_value_change(value)
  30. names = [step._props['name'] for step in self] # pylint: disable=protected-access
  31. for i, step in enumerate(self):
  32. done = i < names.index(value) if value in names else False
  33. step.props(f':done={done}')
  34. def next(self) -> None:
  35. """Show the next step."""
  36. self.run_method('next')
  37. def previous(self) -> None:
  38. """Show the previous step."""
  39. self.run_method('previous')
  40. class Step(DisableableElement):
  41. def __init__(self, name: str, title: Optional[str] = None, icon: Optional[str] = None) -> None:
  42. """Step
  43. This element represents `Quasar's QStep <https://quasar.dev/vue-components/stepper#qstep-api>`_ component.
  44. It is a child of a `ui.stepper` element.
  45. :param name: name of the step (will be the value of the `ui.stepper` element)
  46. :param title: title of the step (default: `None`, meaning the same as `name`)
  47. :param icon: icon of the step (default: `None`)
  48. """
  49. super().__init__(tag='q-step')
  50. self._props['name'] = name
  51. self._props['title'] = title if title is not None else name
  52. self._classes = ['nicegui-step']
  53. if icon:
  54. self._props['icon'] = icon
  55. self.stepper = cast(ValueElement, globals.get_slot().parent)
  56. if self.stepper.value is None:
  57. self.stepper.value = name
  58. class StepperNavigation(Element):
  59. def __init__(self) -> None:
  60. """Stepper Navigation
  61. This element represents `Quasar's QStepperNavigation https://quasar.dev/vue-components/stepper#qsteppernavigation-api>`_ component.
  62. """
  63. super().__init__('q-stepper-navigation')