tabs_documentation.py 2.3 KB

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