Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CSS class for the Panel instead of a Row #272

Merged
merged 11 commits into from
Mar 6, 2024
3 changes: 2 additions & 1 deletion src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
styles.frame,
css`
${options.styles ? replaceVariables(options.styles) : ''}
mikhail-vl marked this conversation as resolved.
Show resolved Hide resolved
`
`,
'dt-row'
);

/**
Expand Down Expand Up @@ -187,7 +188,7 @@
const templateData = frames.map((frame) =>
frame.fields.reduce(
(acc, { config, name, values, display }) => {
values.toArray().forEach((value, i) => {

Check warning on line 191 in src/components/Text/Text.tsx

View workflow job for this annotation

GitHub Actions / tests

'toArray' is deprecated. this is not necessary. This only exists to help migrate Vector to Array

Check warning on line 191 in src/components/Text/Text.tsx

View workflow job for this annotation

GitHub Actions / build

'toArray' is deprecated. this is not necessary. This only exists to help migrate Vector to Array

Check warning on line 191 in src/components/Text/Text.tsx

View workflow job for this annotation

GitHub Actions / build

'toArray' is deprecated. this is not necessary. This only exists to help migrate Vector to Array
/**
* Status Color
*/
Expand Down
43 changes: 43 additions & 0 deletions src/components/TextPanel/TextPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React from 'react';
import { CodeLanguage, Format, TEST_IDS } from '../../constants';
import { PanelOptions, RenderMode } from '../../types';
import { TextPanel } from './TextPanel';
import { injectGlobal } from '@emotion/css';

/**
* Props
Expand All @@ -19,6 +20,14 @@ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
}));

/**
* Mock @emotion/css
*/
jest.mock('@emotion/css', () => ({
...jest.requireActual('@emotion/css'),
injectGlobal: jest.fn(),
}));

/**
* Mock @grafana/ui
*/
Expand Down Expand Up @@ -159,6 +168,40 @@ describe('Panel', () => {
expect(streamSubscribe).toHaveBeenCalled();
});

it('Should apply css for component', async () => {
jest.mocked(injectGlobal);

const streamSubscribe = jest.fn(() => ({
unsubscribe: jest.fn(),
}));

const eventBus = {
getStream: jest.fn(() => ({
subscribe: streamSubscribe,
})),
};

await act(async () =>
render(
getComponent({
options: {
...defaultOptions,
defaultContent: 'hello',
styles: 'styles-test',
},
replaceVariables: (str: string) => str,
data: { series: [] } as any,
eventBus: eventBus as any,
})
)
);

expect(screen.getByTestId(TEST_IDS.panel.root)).toBeInTheDocument();
expect(injectGlobal).toHaveBeenCalledTimes(2);
expect(injectGlobal).toHaveBeenCalledWith('');
expect(injectGlobal).toHaveBeenCalledWith('styles-test');
asimonok marked this conversation as resolved.
Show resolved Hide resolved
});

describe('Helpers execution', () => {
const helpers = `
const subscription = eventBus.subscribe('event', () => {});
Expand Down
13 changes: 11 additions & 2 deletions src/components/TextPanel/TextPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css, cx } from '@emotion/css';
import { css, cx, injectGlobal } from '@emotion/css';
import { PanelProps, SelectableValue } from '@grafana/data';
import { RefreshEvent } from '@grafana/runtime';
import { Select, useStyles2 } from '@grafana/ui';
Expand Down Expand Up @@ -26,6 +26,7 @@ export const TextPanel: React.FC<Props> = ({
timeRange,
timeZone,
eventBus,
id,
replaceVariables,
}) => {
/**
Expand All @@ -38,6 +39,12 @@ export const TextPanel: React.FC<Props> = ({
* Styles
*/
const styles = useStyles2(getStyles);
const panelStyles = options.styles ? replaceVariables(options.styles) : '';

/**
* Injects styles into the global scope (need for dt-row-___ classes)
*/
injectGlobal(panelStyles);

mikhail-vl marked this conversation as resolved.
Show resolved Hide resolved
/**
* Change Frame
Expand Down Expand Up @@ -121,7 +128,9 @@ export const TextPanel: React.FC<Props> = ({
css`
flex-grow: 1;
overflow: auto;
`
`,
'dt-row-container',
`dt-row-container-${id}`
mikhail-vl marked this conversation as resolved.
Show resolved Hide resolved
)}
>
<Text
Expand Down
Loading