Commit 78a6d39e by Tobias Skarhed Committed by GitHub

Forms migration: LayoutSelector (#23790)

parent 83608baf
...@@ -3,6 +3,7 @@ import { css } from 'emotion'; ...@@ -3,6 +3,7 @@ import { css } from 'emotion';
import uniqueId from 'lodash/uniqueId'; import uniqueId from 'lodash/uniqueId';
import { SelectableValue } from '@grafana/data'; import { SelectableValue } from '@grafana/data';
import { RadioButtonSize, RadioButton } from './RadioButton'; import { RadioButtonSize, RadioButton } from './RadioButton';
import { Icon } from '../../Icon/Icon';
const getRadioButtonGroupStyles = () => { const getRadioButtonGroupStyles = () => {
return { return {
...@@ -67,19 +68,20 @@ export function RadioButtonGroup<T>({ ...@@ -67,19 +68,20 @@ export function RadioButtonGroup<T>({
return ( return (
<div className={styles.radioGroup}> <div className={styles.radioGroup}>
{options.map(o => { {options.map((o, i) => {
const isItemDisabled = disabledOptions && o.value && disabledOptions.includes(o.value); const isItemDisabled = disabledOptions && o.value && disabledOptions.includes(o.value);
return ( return (
<RadioButton <RadioButton
size={size} size={size}
disabled={isItemDisabled || disabled} disabled={isItemDisabled || disabled}
active={value === o.value} active={value === o.value}
key={o.label} key={`o.label-${i}`}
onChange={handleOnChange(o)} onChange={handleOnChange(o)}
id={`option-${o.value}-${id}`} id={`option-${o.value}-${id}`}
name={groupName.current} name={groupName.current}
fullWidth={fullWidth} fullWidth={fullWidth}
> >
{o.icon && <Icon name={o.icon} />}
{o.label} {o.label}
</RadioButton> </RadioButton>
); );
......
import React, { FC } from 'react'; import React, { FC } from 'react';
import { Icon } from '@grafana/ui'; import { RadioButtonGroup } from '@grafana/ui';
export type LayoutMode = LayoutModes.Grid | LayoutModes.List; export type LayoutMode = LayoutModes.Grid | LayoutModes.List;
...@@ -13,28 +13,15 @@ interface Props { ...@@ -13,28 +13,15 @@ interface Props {
onLayoutModeChanged: (mode: LayoutMode) => {}; onLayoutModeChanged: (mode: LayoutMode) => {};
} }
const LayoutSelector: FC<Props> = props => { const options = [
const { mode, onLayoutModeChanged } = props; { icon: 'table', value: LayoutModes.Grid },
return ( { icon: 'list-ul', value: LayoutModes.List },
];
const LayoutSelector: FC<Props> = ({ mode, onLayoutModeChanged }) => (
<div className="layout-selector"> <div className="layout-selector">
<button <RadioButtonGroup value={mode} options={options} onChange={onLayoutModeChanged} />
onClick={() => {
onLayoutModeChanged(LayoutModes.List);
}}
className={mode === LayoutModes.List ? 'active' : ''}
>
<Icon name="list-ul" />
</button>
<button
onClick={() => {
onLayoutModeChanged(LayoutModes.Grid);
}}
className={mode === LayoutModes.Grid ? 'active' : ''}
>
<Icon name="table" />
</button>
</div> </div>
); );
};
export default LayoutSelector; export default LayoutSelector;
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import LayoutSelector, { LayoutMode } from '../LayoutSelector/LayoutSelector'; import LayoutSelector, { LayoutMode } from '../LayoutSelector/LayoutSelector';
import { FilterInput } from '../FilterInput/FilterInput'; import { FilterInput } from '../FilterInput/FilterInput';
import { LinkButton } from '@grafana/ui';
export interface Props { export interface Props {
searchQuery: string; searchQuery: string;
...@@ -33,9 +34,7 @@ export default class OrgActionBar extends PureComponent<Props> { ...@@ -33,9 +34,7 @@ export default class OrgActionBar extends PureComponent<Props> {
<LayoutSelector mode={layoutMode} onLayoutModeChanged={(mode: LayoutMode) => onSetLayoutMode(mode)} /> <LayoutSelector mode={layoutMode} onLayoutModeChanged={(mode: LayoutMode) => onSetLayoutMode(mode)} />
</div> </div>
<div className="page-action-bar__spacer" /> <div className="page-action-bar__spacer" />
<a className="btn btn-primary" {...linkProps}> <LinkButton {...linkProps}>{linkButton.title}</LinkButton>
{linkButton.title}
</a>
</div> </div>
); );
} }
......
...@@ -21,12 +21,11 @@ exports[`Render should render component 1`] = ` ...@@ -21,12 +21,11 @@ exports[`Render should render component 1`] = `
<div <div
className="page-action-bar__spacer" className="page-action-bar__spacer"
/> />
<a <LinkButton
className="btn btn-primary"
href="some/url" href="some/url"
target="_blank" target="_blank"
> >
test test
</a> </LinkButton>
</div> </div>
`; `;
import store from 'app/core/store';
import coreModule from 'app/core/core_module';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
import { CoreEvents } from 'app/types';
const template = `
<div class="layout-selector">
<button ng-click="ctrl.listView()" ng-class="{active: ctrl.mode === 'list'}">
<i class="fa fa-list"></i>
</button>
<button ng-click="ctrl.gridView()" ng-class="{active: ctrl.mode === 'grid'}">
<icon name="table"></i>
</button>
</div>
`;
export class LayoutSelectorCtrl {
mode: string;
/** @ngInject */
constructor(private $rootScope: GrafanaRootScope) {
this.mode = store.get('grafana.list.layout.mode') || 'grid';
}
listView() {
this.mode = 'list';
store.set('grafana.list.layout.mode', 'list');
this.$rootScope.appEvent(CoreEvents.layoutModeChanged, 'list');
}
gridView() {
this.mode = 'grid';
store.set('grafana.list.layout.mode', 'grid');
this.$rootScope.appEvent(CoreEvents.layoutModeChanged, 'grid');
}
}
/** @ngInject */
export function layoutSelector() {
return {
restrict: 'E',
controller: LayoutSelectorCtrl,
bindToController: true,
controllerAs: 'ctrl',
scope: {},
template: template,
};
}
/** @ngInject */
export function layoutMode($rootScope: GrafanaRootScope) {
return {
restrict: 'A',
scope: {},
link: (scope: any, elem: any) => {
const layout = store.get('grafana.list.layout.mode') || 'grid';
let className = 'card-list-layout-' + layout;
elem.addClass(className);
$rootScope.onAppEvent(
CoreEvents.layoutModeChanged,
(evt: any, newLayout: any) => {
elem.removeClass(className);
className = 'card-list-layout-' + newLayout;
elem.addClass(className);
},
scope
);
},
};
}
coreModule.directive('layoutSelector', layoutSelector);
coreModule.directive('layoutMode', layoutMode);
...@@ -23,7 +23,6 @@ import { infoPopover } from './components/info_popover'; ...@@ -23,7 +23,6 @@ import { infoPopover } from './components/info_popover';
import { arrayJoin } from './directives/array_join'; import { arrayJoin } from './directives/array_join';
import { liveSrv } from './live/live_srv'; import { liveSrv } from './live/live_srv';
import { Emitter } from './utils/emitter'; import { Emitter } from './utils/emitter';
import { layoutSelector } from './components/layout_selector/layout_selector';
import { switchDirective } from './components/switch'; import { switchDirective } from './components/switch';
import { dashboardSelector } from './components/dashboard_selector'; import { dashboardSelector } from './components/dashboard_selector';
import { queryPartEditorDirective } from './components/query_part/query_part_editor'; import { queryPartEditorDirective } from './components/query_part/query_part_editor';
...@@ -54,7 +53,6 @@ export { ...@@ -54,7 +53,6 @@ export {
coreModule, coreModule,
searchDirective, searchDirective,
liveSrv, liveSrv,
layoutSelector,
switchDirective, switchDirective,
infoPopover, infoPopover,
Emitter, Emitter,
......
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