ExampleRootPage.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Types
  4. import { AppRootProps, NavModelItem } from '@grafana/ui';
  5. interface Props extends AppRootProps {}
  6. const TAB_ID_A = 'A';
  7. const TAB_ID_B = 'B';
  8. const TAB_ID_C = 'C';
  9. export class ExampleRootPage<ExampleAppSettings> extends PureComponent<Props> {
  10. constructor(props: Props) {
  11. super(props);
  12. }
  13. componentDidMount() {
  14. this.updateNav();
  15. }
  16. componentDidUpdate(prevProps: Props) {
  17. if (this.props.query !== prevProps.query) {
  18. if (this.props.query.tab !== prevProps.query.tab) {
  19. this.updateNav();
  20. }
  21. }
  22. }
  23. updateNav() {
  24. const { path, onNavChanged, query, meta } = this.props;
  25. const tabs: NavModelItem[] = [];
  26. tabs.push({
  27. text: 'Tab A',
  28. icon: 'fa fa-fw fa-file-text-o',
  29. url: path + '?tab=' + TAB_ID_A,
  30. id: TAB_ID_A,
  31. });
  32. tabs.push({
  33. text: 'Tab B',
  34. icon: 'fa fa-fw fa-file-text-o',
  35. url: path + '?tab=' + TAB_ID_B,
  36. id: TAB_ID_B,
  37. });
  38. tabs.push({
  39. text: 'Tab C',
  40. icon: 'fa fa-fw fa-file-text-o',
  41. url: path + '?tab=' + TAB_ID_C,
  42. id: TAB_ID_C,
  43. });
  44. // Set the active tab
  45. let found = false;
  46. const selected = query.tab || TAB_ID_B;
  47. for (const tab of tabs) {
  48. tab.active = !found && selected === tab.id;
  49. if (tab.active) {
  50. found = true;
  51. }
  52. }
  53. if (!found) {
  54. tabs[0].active = true;
  55. }
  56. const node = {
  57. text: 'This is the Page title',
  58. img: meta.info.logos.large,
  59. subTitle: 'subtitle here',
  60. url: path,
  61. children: tabs,
  62. };
  63. // Update the page header
  64. onNavChanged({
  65. node: node,
  66. main: node,
  67. });
  68. }
  69. render() {
  70. const { path, query, meta } = this.props;
  71. return (
  72. <div>
  73. QUERY: <pre>{JSON.stringify(query)}</pre>
  74. <br />
  75. <ul>
  76. <li>
  77. <a href={path + '?x=1'}>111</a>
  78. </li>
  79. <li>
  80. <a href={path + '?x=AAA'}>AAA</a>
  81. </li>
  82. <li>
  83. <a href={path + '?x=1&y=2&y=3'}>ZZZ</a>
  84. </li>
  85. </ul>
  86. <pre>{JSON.stringify(meta.jsonData)}</pre>
  87. </div>
  88. );
  89. }
  90. }