Skip to content

Commit

Permalink
[Canvas] Added KibanaThemeProvider to expression_metric. (#120078)
Browse files Browse the repository at this point in the history
* Added KibanaThemeProvider to metric and changed exports.

* Added kibanaReact bundle.

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Kuznietsov and kibanamachine authored Dec 3, 2021
1 parent cc46feb commit 3fed3a4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/plugins/expression_metric/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"server": true,
"ui": true,
"requiredPlugins": ["expressions", "presentationUtil"],
"optionalPlugins": []
"optionalPlugins": [],
"requiredBundles": ["kibanaReact"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React, { CSSProperties } from 'react';
import { storiesOf } from '@storybook/react';
import { Style } from 'src/plugins/expressions';
import { metricRenderer } from '../metric_renderer';
import { getMetricRenderer } from '../metric_renderer';
import { Render } from '../../../../presentation_util/public/__stories__';
import { MetricRendererConfig } from '../../../common';

Expand Down Expand Up @@ -45,7 +45,7 @@ storiesOf('renderers/Metric', module)
label: '',
metricFormat: '',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with number metric', () => {
const config: MetricRendererConfig = {
Expand All @@ -55,7 +55,7 @@ storiesOf('renderers/Metric', module)
label: '',
metricFormat: '',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with string metric', () => {
const config: MetricRendererConfig = {
Expand All @@ -65,7 +65,7 @@ storiesOf('renderers/Metric', module)
label: '',
metricFormat: '',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with label', () => {
const config: MetricRendererConfig = {
Expand All @@ -75,7 +75,7 @@ storiesOf('renderers/Metric', module)
label: 'Average price',
metricFormat: '',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with number metric and a specified format', () => {
const config: MetricRendererConfig = {
Expand All @@ -85,7 +85,7 @@ storiesOf('renderers/Metric', module)
label: 'Average price',
metricFormat: '0.00%',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with formatted string metric and a specified format', () => {
const config: MetricRendererConfig = {
Expand All @@ -95,7 +95,7 @@ storiesOf('renderers/Metric', module)
label: 'Total Revenue',
metricFormat: '$0a',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
})
.add('with invalid metricFont', () => {
const config: MetricRendererConfig = {
Expand All @@ -105,5 +105,5 @@ storiesOf('renderers/Metric', module)
label: 'Total Revenue',
metricFormat: '$0a',
};
return <Render renderer={metricRenderer} config={config} />;
return <Render renderer={getMetricRenderer()} config={config} />;
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@
* Side Public License, v 1.
*/

import { metricRenderer } from './metric_renderer';

export const renderers = [metricRenderer];

export { metricRenderer };
export { metricRendererFactory, getMetricRenderer } from './metric_renderer';
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
* Side Public License, v 1.
*/
import React, { CSSProperties, lazy } from 'react';
import { Observable } from 'rxjs';
import { CoreTheme } from 'kibana/public';
import { render, unmountComponentAtNode } from 'react-dom';
import { ExpressionRenderDefinition, IInterpreterRenderHandlers } from 'src/plugins/expressions';
import { i18n } from '@kbn/i18n';
import { withSuspense } from '../../../presentation_util/public';
import { CoreSetup } from '../../../../core/public';
import { KibanaThemeProvider } from '../../../kibana_react/public';
import { withSuspense, defaultTheme$ } from '../../../presentation_util/public';
import { MetricRendererConfig } from '../../common/types';

const strings = {
Expand All @@ -26,30 +30,36 @@ const strings = {
const LazyMetricComponent = lazy(() => import('../components/metric_component'));
const MetricComponent = withSuspense(LazyMetricComponent);

export const metricRenderer = (): ExpressionRenderDefinition<MetricRendererConfig> => ({
name: 'metric',
displayName: strings.getDisplayName(),
help: strings.getHelpDescription(),
reuseDomNode: true,
render: async (
domNode: HTMLElement,
config: MetricRendererConfig,
handlers: IInterpreterRenderHandlers
) => {
handlers.onDestroy(() => {
unmountComponentAtNode(domNode);
});
export const getMetricRenderer =
(theme$: Observable<CoreTheme> = defaultTheme$) =>
(): ExpressionRenderDefinition<MetricRendererConfig> => ({
name: 'metric',
displayName: strings.getDisplayName(),
help: strings.getHelpDescription(),
reuseDomNode: true,
render: async (
domNode: HTMLElement,
config: MetricRendererConfig,
handlers: IInterpreterRenderHandlers
) => {
handlers.onDestroy(() => {
unmountComponentAtNode(domNode);
});

render(
<MetricComponent
label={config.label}
labelFont={config.labelFont ? (config.labelFont.spec as CSSProperties) : {}}
metric={config.metric}
metricFont={config.metricFont ? (config.metricFont.spec as CSSProperties) : {}}
metricFormat={config.metricFormat}
/>,
domNode,
() => handlers.done()
);
},
});
render(
<KibanaThemeProvider theme$={theme$}>
<MetricComponent
label={config.label}
labelFont={config.labelFont ? (config.labelFont.spec as CSSProperties) : {}}
metric={config.metric}
metricFont={config.metricFont ? (config.metricFont.spec as CSSProperties) : {}}
metricFormat={config.metricFormat}
/>
</KibanaThemeProvider>,
domNode,
() => handlers.done()
);
},
});

export const metricRendererFactory = (core: CoreSetup) => getMetricRenderer(core.theme.theme$);
5 changes: 1 addition & 4 deletions src/plugins/expression_metric/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
* Side Public License, v 1.
*/

// TODO: https://github.com/elastic/kibana/issues/110893
/* eslint-disable @kbn/eslint/no_export_all */

import { ExpressionMetricPlugin } from './plugin';

export type { ExpressionMetricPluginSetup, ExpressionMetricPluginStart } from './plugin';
Expand All @@ -17,4 +14,4 @@ export function plugin() {
return new ExpressionMetricPlugin();
}

export * from './expression_renderers';
export { metricRendererFactory, getMetricRenderer } from './expression_renderers';
4 changes: 2 additions & 2 deletions src/plugins/expression_metric/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { CoreSetup, CoreStart, Plugin } from '../../../core/public';
import { ExpressionsStart, ExpressionsSetup } from '../../expressions/public';
import { metricFunction } from '../common/expression_functions';
import { metricRenderer } from './expression_renderers';
import { metricRendererFactory } from './expression_renderers';

interface SetupDeps {
expressions: ExpressionsSetup;
Expand All @@ -27,7 +27,7 @@ export class ExpressionMetricPlugin
{
public setup(core: CoreSetup, { expressions }: SetupDeps): ExpressionMetricPluginSetup {
expressions.registerFunction(metricFunction);
expressions.registerRenderer(metricRenderer);
expressions.registerRenderer(metricRendererFactory(core));
}

public start(core: CoreStart): ExpressionMetricPluginStart {}
Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/canvas/canvas_plugin_src/renderers/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { imageRenderer } from '../../../../../src/plugins/expression_image/public';
import { metricRenderer } from '../../../../../src/plugins/expression_metric/public';
import { metricRendererFactory } from '../../../../../src/plugins/expression_metric/public';
import {
errorRendererFactory,
debugRendererFactory,
Expand All @@ -20,11 +20,14 @@ import {

export const renderFunctions = [
imageRenderer,
metricRenderer,
revealImageRenderer,
shapeRenderer,
repeatImageRenderer,
progressRenderer,
];

export const renderFunctionFactories = [debugRendererFactory, errorRendererFactory];
export const renderFunctionFactories = [
debugRendererFactory,
errorRendererFactory,
metricRendererFactory,
];
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
shapeRenderer as shape,
progressRenderer as progress,
} from '../../../../src/plugins/expression_shape/public';
import { metricRenderer as metric } from '../../../../src/plugins/expression_metric/public';
import { getMetricRenderer } from '../../../../src/plugins/expression_metric/public';

const unboxFactory = (factory) => factory();

Expand All @@ -31,6 +31,7 @@ const renderFunctionsFactories = [
getTableRenderer,
getErrorRenderer,
getDebugRenderer,
getMetricRenderer,
];

/**
Expand All @@ -42,7 +43,6 @@ export const renderFunctions = [
image,
repeatImage,
revealImage,
metric,
pie,
plot,
progress,
Expand Down

0 comments on commit 3fed3a4

Please sign in to comment.