Skip to content

Commit

Permalink
Merge branch 'main' into kbn-104081-split-multiple-indices-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila authored Apr 4, 2023
2 parents dfc8045 + 606eb9c commit 279a2ed
Show file tree
Hide file tree
Showing 174 changed files with 6,242 additions and 1,976 deletions.
3 changes: 2 additions & 1 deletion src/plugins/chart_expressions/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* Side Public License, v 1.
*/

export { extractContainerType, extractVisualizationType } from './utils';
export { extractContainerType, extractVisualizationType, getOverridesFor } from './utils';
export type { Simplify, MakeOverridesSerializable } from './types';
28 changes: 28 additions & 0 deletions src/plugins/chart_expressions/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';

export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};

// Overrides should not expose Functions, React nodes and children props
// So filter out any type which is not serializable
export type MakeOverridesSerializable<T> = {
[KeyType in keyof T]: NonNullable<T[KeyType]> extends Function
? // cannot use boolean here as it would be challenging to distinguish
// between a "native" boolean props and a disabled callback
// so use a specific keyword
'ignore'
: // be careful here to not filter out string/number types
NonNullable<T[KeyType]> extends React.ReactChildren | React.ReactElement
? never
: // make it recursive
NonNullable<T[KeyType]> extends object
? MakeOverridesSerializable<T[KeyType]>
: NonNullable<T[KeyType]>;
};
33 changes: 33 additions & 0 deletions src/plugins/chart_expressions/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { getOverridesFor } from './utils';

describe('Overrides utilities', () => {
describe('getOverridesFor', () => {
it('should return an empty object for undefined values', () => {
expect(getOverridesFor(undefined, 'settings')).toEqual({});
// @ts-expect-error
expect(getOverridesFor({}, 'settings')).toEqual({});
// @ts-expect-error
expect(getOverridesFor({ otherOverride: {} }, 'settings')).toEqual({});
});

it('should return only the component specific overrides', () => {
expect(
getOverridesFor({ otherOverride: { a: 15 }, settings: { b: 10 } }, 'settings')
).toEqual({ b: 10 });
});

it('should swap any "ignore" value into undefined value', () => {
expect(
getOverridesFor({ otherOverride: { a: 15 }, settings: { b: 10, c: 'ignore' } }, 'settings')
).toEqual({ b: 10, c: undefined });
});
});
});
28 changes: 27 additions & 1 deletion src/plugins/chart_expressions/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';

export const extractContainerType = (context?: KibanaExecutionContext): string | undefined => {
Expand Down Expand Up @@ -33,3 +32,30 @@ export const extractVisualizationType = (context?: KibanaExecutionContext): stri
return recursiveGet(context)?.type;
}
};

/**
* Get an override specification and returns a props object to use directly with the Component
* @param overrides Overrides object
* @param componentName name of the Component to look for (i.e. "settings", "axisX")
* @returns an props object to use directly with the component
*/
export function getOverridesFor<
// Component props
P extends Record<string, unknown>,
// Overrides
O extends Record<string, P>,
// Overrides Component names
K extends keyof O
>(overrides: O | undefined, componentName: K) {
if (!overrides || !overrides[componentName]) {
return {};
}
return Object.fromEntries(
Object.entries(overrides[componentName]).map(([key, value]) => {
if (value === 'ignore') {
return [key, undefined];
}
return [key, value];
})
);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GaugeArguments, GaugeShapes } from '..';
import { functionWrapper } from '@kbn/expressions-plugin/common/expression_functions/specs/tests/utils';
import { Datatable } from '@kbn/expressions-plugin/common/expression_types/specs';
import {
EXPRESSION_GAUGE_NAME,
GaugeCentralMajorModes,
GaugeColorModes,
GaugeLabelMajorModes,
Expand Down Expand Up @@ -110,4 +111,23 @@ describe('interpreter/functions#gauge', () => {

expect(loggedTable!).toMatchSnapshot();
});

it('should pass over overrides from variables', async () => {
const overrides = {
settings: {
onBrushEnd: 'ignore',
},
};
const handlers = {
variables: { overrides },
getExecutionContext: jest.fn(),
} as unknown as ExecutionContext;
const result = await fn(context, args, handlers);

expect(result).toEqual({
type: 'render',
as: EXPRESSION_GAUGE_NAME,
value: expect.objectContaining({ overrides }),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { i18n } from '@kbn/i18n';
import { prepareLogTable, validateAccessor } from '@kbn/visualizations-plugin/common/utils';
import { GaugeExpressionFunctionDefinition } from '../types';
import { GaugeExpressionFunctionDefinition, GaugeRenderProps } from '../types';
import {
EXPRESSION_GAUGE_NAME,
GaugeCentralMajorModes,
Expand Down Expand Up @@ -232,6 +232,7 @@ export const gaugeFunction = (): GaugeExpressionFunctionDefinition => ({
handlers.getExecutionContext?.()?.description,
},
canNavigateToLens: Boolean(handlers?.variables?.canNavigateToLens),
overrides: handlers.variables?.overrides as GaugeRenderProps['overrides'],
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const PLUGIN_ID = 'expressionGauge';
export const PLUGIN_NAME = 'expressionGauge';

export type {
AllowedGaugeOverrides,
GaugeExpressionFunctionDefinition,
GaugeExpressionProps,
FormatFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '@kbn/expressions-plugin/common';
import { ExpressionValueVisDimension } from '@kbn/visualizations-plugin/common';
import { CustomPaletteState } from '@kbn/charts-plugin/common';
import type { MakeOverridesSerializable, Simplify } from '@kbn/chart-expressions-common/types';
import type { GoalProps } from '@elastic/charts';
import {
EXPRESSION_GAUGE_NAME,
GAUGE_FUNCTION_RENDERER_NAME,
Expand Down Expand Up @@ -84,3 +86,7 @@ export interface Accessors {
metric?: string;
goal?: string;
}

export type AllowedGaugeOverrides = Partial<
Record<'gauge', Simplify<MakeOverridesSerializable<GoalProps>>>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type { PaletteRegistry } from '@kbn/coloring';
import type { PersistedState } from '@kbn/visualizations-plugin/public';
import type { ChartsPluginSetup } from '@kbn/charts-plugin/public';
import type { IFieldFormat, SerializedFieldFormat } from '@kbn/field-formats-plugin/common';
import type { GaugeExpressionProps } from './expression_functions';
import type { AllowedSettingsOverrides } from '@kbn/charts-plugin/common';
import type { AllowedGaugeOverrides, GaugeExpressionProps } from './expression_functions';

export type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat;

Expand All @@ -20,4 +21,5 @@ export type GaugeRenderProps = GaugeExpressionProps & {
paletteService: PaletteRegistry;
renderComplete: () => void;
uiState: PersistedState;
overrides?: AllowedGaugeOverrides & AllowedSettingsOverrides;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
GaugeColorModes,
} from '../../common';
import GaugeComponent from './gauge_component';
import { Chart, Goal } from '@elastic/charts';
import { Chart, Goal, Settings } from '@elastic/charts';

jest.mock('@elastic/charts', () => {
const original = jest.requireActual('@elastic/charts');
Expand Down Expand Up @@ -405,4 +405,19 @@ describe('GaugeComponent', function () {
expect(goal.prop('bands')).toEqual([0, 2, 6, 8, 10]);
});
});

describe('overrides', () => {
it('should apply overrides to the settings component', () => {
const component = shallowWithIntl(
<GaugeComponent
{...wrapperProps}
overrides={{ settings: { onBrushEnd: 'ignore', ariaUseDefaultSummary: true } }}
/>
);

const settingsComponent = component.find(Settings);
expect(settingsComponent.prop('onBrushEnd')).toBeUndefined();
expect(settingsComponent.prop('ariaUseDefaultSummary')).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { PaletteOutput } from '@kbn/coloring';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import type { CustomPaletteState } from '@kbn/charts-plugin/public';
import { EmptyPlaceholder } from '@kbn/charts-plugin/public';
import { getOverridesFor } from '@kbn/chart-expressions-common';
import { isVisDimension } from '@kbn/visualizations-plugin/common/utils';
import {
GaugeRenderProps,
Expand Down Expand Up @@ -167,7 +168,16 @@ function getTicks(
}

export const GaugeComponent: FC<GaugeRenderProps> = memo(
({ data, args, uiState, formatFactory, paletteService, chartsThemeService, renderComplete }) => {
({
data,
args,
uiState,
formatFactory,
paletteService,
chartsThemeService,
renderComplete,
overrides,
}) => {
const {
shape: gaugeType,
palette,
Expand Down Expand Up @@ -360,6 +370,7 @@ export const GaugeComponent: FC<GaugeRenderProps> = memo(
ariaLabel={args.ariaLabel}
ariaUseDefaultSummary={!args.ariaLabel}
onRenderChange={onRenderChange}
{...getOverridesFor(overrides, 'settings')}
/>
<Goal
id="goal"
Expand Down Expand Up @@ -396,6 +407,7 @@ export const GaugeComponent: FC<GaugeRenderProps> = memo(
labelMinor={labelMinor ? `${labelMinor}${minorExtraSpaces}` : ''}
{...extraTitles}
{...goalConfig}
{...getOverridesFor(overrides, 'gauge')}
/>
</Chart>
{commonLabel && <div className="gauge__label">{commonLabel}</div>}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 279a2ed

Please sign in to comment.