Skip to content

Commit

Permalink
Canvas: Add standard text config options to button element (grafana#7…
Browse files Browse the repository at this point in the history
…6160)

Co-authored-by: Adela Almasan <[email protected]>
  • Loading branch information
nmarrs and adela-almasan authored Oct 10, 2023
1 parent c26e3d8 commit 6184bf2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 31 deletions.
4 changes: 0 additions & 4 deletions .betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -2907,10 +2907,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Styles should be written using objects.", "1"],
[0, 0, 0, "Styles should be written using objects.", "2"]
],
"public/app/features/canvas/elements/rectangle.tsx:5381": [
[0, 0, 0, "Styles should be written using objects.", "0"],
[0, 0, 0, "Styles should be written using objects.", "1"]
],
"public/app/features/canvas/elements/server/server.tsx:5381": [
[0, 0, 0, "Styles should be written using objects.", "0"],
[0, 0, 0, "Styles should be written using objects.", "1"],
Expand Down
3 changes: 2 additions & 1 deletion public/app/features/canvas/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ export interface CanvasElementItem<TConfig = any, TData = any> extends RegistryI

export const defaultBgColor = '#D9D9D9';
export const defaultTextColor = '#000000';
export const defaultThemeTextColor = config.theme2.colors.text.primary;
export const defaultLightTextColor = '#F0F4FD';
export const defaultThemeTextColor = config.theme2.colors.background.primary;
74 changes: 61 additions & 13 deletions public/app/features/canvas/elements/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import React, { PureComponent } from 'react';

import { GrafanaTheme2 } from '@grafana/data';
import { PluginState } from '@grafana/data/src';
import { TextDimensionConfig, TextDimensionMode } from '@grafana/schema';
import { TextDimensionMode } from '@grafana/schema';
import { Button, stylesFactory } from '@grafana/ui';
import { config } from 'app/core/config';
import { DimensionContext } from 'app/features/dimensions/context';
import { ColorDimensionEditor } from 'app/features/dimensions/editors';
import { TextDimensionEditor } from 'app/features/dimensions/editors/TextDimensionEditor';
import { APIEditor, APIEditorConfig } from 'app/plugins/panel/canvas/editor/element/APIEditor';
import { ButtonStyleConfig, ButtonStyleEditor } from 'app/plugins/panel/canvas/editor/element/ButtonStyleEditor';
import { callApi } from 'app/plugins/panel/canvas/editor/element/utils';
import { HttpRequestMethod } from 'app/plugins/panel/canvas/panelcfg.gen';

import { CanvasElementItem, CanvasElementProps } from '../element';
import { CanvasElementItem, CanvasElementProps, defaultLightTextColor } from '../element';
import { Align, TextConfig, TextData } from '../types';

interface ButtonData {
text?: string;
interface ButtonData extends Omit<TextData, 'valign'> {
api?: APIEditorConfig;
style?: ButtonStyleConfig;
}

interface ButtonConfig {
text?: TextDimensionConfig;
interface ButtonConfig extends Omit<TextConfig, 'valign'> {
api?: APIEditorConfig;
style?: ButtonStyleConfig;
}
Expand Down Expand Up @@ -61,9 +61,16 @@ class ButtonDisplay extends PureComponent<CanvasElementProps<ButtonConfig, Butto

const getStyles = stylesFactory((theme: GrafanaTheme2, data: ButtonData | undefined) => ({
button: css({
position: 'absolute',
height: '100%',
width: '100%',
display: 'grid',

'> span': {
display: 'inline-grid',
textAlign: data?.align,
fontSize: `${data?.size}px`,
color: data?.color,
},
}),
}));

Expand All @@ -80,8 +87,8 @@ export const buttonItem: CanvasElementItem<ButtonConfig, ButtonData> = {
display: ButtonDisplay,

defaultSize: {
width: 78,
height: 32,
width: 150,
height: 45,
},

getNewOptions: (options) => ({
Expand All @@ -91,6 +98,11 @@ export const buttonItem: CanvasElementItem<ButtonConfig, ButtonData> = {
mode: TextDimensionMode.Fixed,
fixed: 'Button',
},
align: Align.Center,
color: {
fixed: defaultLightTextColor,
},
size: 14,
api: defaultApiConfig,
style: defaultStyleConfig,
},
Expand Down Expand Up @@ -124,17 +136,30 @@ export const buttonItem: CanvasElementItem<ButtonConfig, ButtonData> = {

const data: ButtonData = {
text: cfg?.text ? ctx.getText(cfg.text).value() : '',
align: cfg.align ?? Align.Center,
size: cfg.size,
api: getCfgApi(),
style: cfg?.style ?? defaultStyleConfig,
};

if (cfg.color) {
data.color = ctx.getColor(cfg.color).value();
}

return data;
},

// Heatmap overlay options
registerOptionsUI: (builder) => {
const category = ['Button'];
builder
.addCustomEditor({
category,
id: 'styleSelector',
path: 'config.style',
name: 'Style',
editor: ButtonStyleEditor,
})
.addCustomEditor({
category,
id: 'textSelector',
Expand All @@ -144,10 +169,33 @@ export const buttonItem: CanvasElementItem<ButtonConfig, ButtonData> = {
})
.addCustomEditor({
category,
id: 'styleSelector',
path: 'config.style',
name: 'Style',
editor: ButtonStyleEditor,
id: 'config.color',
path: 'config.color',
name: 'Text color',
editor: ColorDimensionEditor,
settings: {},
defaultValue: {},
})
.addRadio({
category,
path: 'config.align',
name: 'Align text',
settings: {
options: [
{ value: Align.Left, label: 'Left' },
{ value: Align.Center, label: 'Center' },
{ value: Align.Right, label: 'Right' },
],
},
defaultValue: Align.Left,
})
.addNumberInput({
category,
path: 'config.size',
name: 'Text size',
settings: {
placeholder: 'Auto',
},
})
.addCustomEditor({
category,
Expand Down
29 changes: 16 additions & 13 deletions public/app/features/canvas/elements/rectangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@ class RectangleDisplay extends PureComponent<CanvasElementProps<TextConfig, Text
render() {
const { data } = this.props;
const styles = getStyles(config.theme2, data);

return (
<div className={styles.container}>
<span className={styles.span}>{data?.text}</span>
</div>
);
}
}

const getStyles = stylesFactory((theme: GrafanaTheme2, data) => ({
container: css`
position: absolute;
height: 100%;
width: 100%;
display: table;
`,
span: css`
display: table-cell;
vertical-align: ${data?.valign};
text-align: ${data?.align};
font-size: ${data?.size}px;
color: ${data?.color};
`,
container: css({
position: 'absolute',
height: '100%',
width: '100%',
display: 'table',
}),
span: css({
display: 'table-cell',
verticalAlign: data?.valign,
textAlign: data?.align,
fontSize: `${data?.size}px`,
color: data?.color,
}),
}));

export const rectangleItem: CanvasElementItem<TextConfig, TextData> = {
id: 'rectangle',
name: 'Rectangle',
Expand Down

0 comments on commit 6184bf2

Please sign in to comment.