tabs_documentation.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class TabsDocumentation(UiElementDocumentation, element=ui.tabs):
  4. def main_demo(self) -> None:
  5. """Tabs
  6. The elements `ui.tabs`, `ui.tab`, `ui.tab_panels`, and `ui.tab_panel` resemble
  7. `Quasar's tabs <https://quasar.dev/vue-components/tabs>`_
  8. and `tab panels <https://quasar.dev/vue-components/tab-panels>`_ API.
  9. `ui.tabs` creates a container for the tabs. This could be placed in a `ui.header` for example.
  10. `ui.tab_panels` creates a container for the tab panels with the actual content.
  11. Each `ui.tab_panel` is associated with a `ui.tab` element.
  12. """
  13. with ui.tabs().classes('w-full') as tabs:
  14. one = ui.tab('One')
  15. two = ui.tab('Two')
  16. with ui.tab_panels(tabs, value=two).classes('w-full'):
  17. with ui.tab_panel(one):
  18. ui.label('First tab')
  19. with ui.tab_panel(two):
  20. ui.label('Second tab')
  21. def more(self) -> None:
  22. @self.demo('Name, label, icon', '''
  23. The `ui.tab` element has a `label` property that can be used to display a different text than the `name`.
  24. The `name` can also be used instead of the `ui.tab` objects to associate a `ui.tab` with a `ui.tab_panel`.
  25. Additionally each tab can have an `icon`.
  26. ''')
  27. def name_and_label():
  28. with ui.tabs() as tabs:
  29. ui.tab('h', label='Home', icon='home')
  30. ui.tab('a', label='About', icon='info')
  31. with ui.tab_panels(tabs, value='h').classes('w-full'):
  32. with ui.tab_panel('h'):
  33. ui.label('Main Content')
  34. with ui.tab_panel('a'):
  35. ui.label('Infos')
  36. @self.demo('Switch tabs programmatically', '''
  37. The `ui.tabs` and `ui.tab_panels` elements are derived from ValueElement which has a `set_value` method.
  38. That can be used to switch tabs programmatically.
  39. ''')
  40. def switch_tabs():
  41. content = {'Tab 1': 'Content 1', 'Tab 2': 'Content 2', 'Tab 3': 'Content 3'}
  42. with ui.tabs() as tabs:
  43. for title in content:
  44. ui.tab(title)
  45. with ui.tab_panels(tabs).classes('w-full') as panels:
  46. for title, text in content.items():
  47. with ui.tab_panel(title):
  48. ui.label(text)
  49. ui.button('GoTo 1', on_click=lambda: panels.set_value('Tab 1'))
  50. ui.button('GoTo 2', on_click=lambda: tabs.set_value('Tab 2'))