Commit 02a3951f by Johannes Schill Committed by Daniel Lee

dashfolders: Remove those 2-line-components from PageHeader to make it easier to…

dashfolders: Remove those 2-line-components from PageHeader to make it easier to read and make sure components listening to the mobx state are wrapped with observer() #10275
parent 766ab691
import React from 'react';
import { observer } from 'mobx-react';
import { NavModel, NavModelItem } from '../../nav_model_srv';
import classNames from 'classnames';
import appEvents from 'app/core/app_events';
......@@ -7,12 +8,52 @@ export interface IProps {
model: NavModel;
}
function TabItem(tab: NavModelItem) {
const SelectNav = observer(({ main, customCss }: { main: NavModelItem; customCss: string }) => {
const defaultSelectedItem = main.children.find(navItem => {
return navItem.active === true;
});
const gotoUrl = evt => {
var element = evt.target;
var url = element.options[element.selectedIndex].value;
appEvents.emit('location-change', { href: url });
};
return (
<div className={`gf-form-select-wrapper width-20 ${customCss}`}>
<label className={`gf-form-select-icon ${defaultSelectedItem.icon}`} htmlFor="page-header-select-nav" />
{/* Label to make it clickable */}
<select
className="gf-select-nav gf-form-input"
value={defaultSelectedItem.url}
onChange={gotoUrl}
id="page-header-select-nav"
>
{main.children.map((navItem: NavModelItem) => {
if (navItem.hideFromTabs) {
// TODO: Rename hideFromTabs => hideFromNav
return null;
}
return (
<option key={navItem.url} value={navItem.url}>
{navItem.text}
</option>
);
})}
</select>
</div>
);
});
const Tabs = observer(({ main, customCss }: { main: NavModelItem; customCss: string }) => {
return (
<ul className={`gf-tabs ${customCss}`}>
{main.children.map((tab, idx) => {
if (tab.hideFromTabs) {
return null;
}
let tabClasses = classNames({
const tabClasses = classNames({
'gf-tabs-link': true,
active: tab.active,
});
......@@ -25,61 +66,21 @@ function TabItem(tab: NavModelItem) {
</a>
</li>
);
}
function SelectOption(navItem: NavModelItem) {
if (navItem.hideFromTabs) {
// TODO: Rename hideFromTabs => hideFromNav
return null;
}
return (
<option key={navItem.url} value={navItem.url}>
{navItem.text}
</option>
})}
</ul>
);
}
});
function Navigation({ main }: { main: NavModelItem }) {
const Navigation = ({ main }: { main: NavModelItem }) => {
return (
<nav>
<SelectNav customCss="page-header__select-nav" main={main} />
<Tabs customCss="page-header__tabs" main={main} />
</nav>
);
}
function SelectNav({ main, customCss }: { main: NavModelItem; customCss: string }) {
const defaultSelectedItem = main.children.find(navItem => {
return navItem.active === true;
});
const gotoUrl = evt => {
var element = evt.target;
var url = element.options[element.selectedIndex].value;
appEvents.emit('location-change', { href: url });
};
return (
<div className={`gf-form-select-wrapper width-20 ${customCss}`}>
<label className={`gf-form-select-icon ${defaultSelectedItem.icon}`} htmlFor="page-header-select-nav" />
{/* Label to make it clickable */}
<select
className="gf-select-nav gf-form-input"
defaultValue={defaultSelectedItem.url}
onChange={gotoUrl}
id="page-header-select-nav"
>
{main.children.map(SelectOption)}
</select>
</div>
);
}
function Tabs({ main, customCss }: { main: NavModelItem; customCss: string }) {
return <ul className={`gf-tabs ${customCss}`}>{main.children.map(TabItem)}</ul>;
}
};
@observer
export default class PageHeader extends React.Component<IProps, any> {
constructor(props) {
super(props);
......@@ -140,7 +141,6 @@ export default class PageHeader extends React.Component<IProps, any> {
if (!model) {
return null;
}
return (
<div className="page-header-canvas">
<div className="page-container">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment