Skip to content

Commit

Permalink
show saved title in layer panel header
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Mar 3, 2023
1 parent 568b89c commit 10bb53d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

import { FramePublicAPI } from '../../../types';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { LayerHeader } from './layer_header';
import {
XYByReferenceAnnotationLayerConfig,
XYByValueAnnotationLayerConfig,
XYLayerConfig,
XYState,
} from '../types';

describe('layer header', () => {
describe('annotation layer header', () => {
it('should inject annotation title for by-reference layer', () => {
const byRefGroupTitle = 'My group title!';

const byRefLayer: XYByReferenceAnnotationLayerConfig = {
layerId: 'layer-123',
layerType: 'annotations',
annotationGroupId: 'some-group',
annotations: [],
indexPatternId: '',
ignoreGlobalFilters: false,
__lastSaved: {
title: byRefGroupTitle,
annotations: [],
indexPatternId: '',
},
};

const byValueLayer: XYByValueAnnotationLayerConfig = {
layerId: 'layer-123',
layerType: 'annotations',
annotations: [],
indexPatternId: '',
ignoreGlobalFilters: false,
};

const getStateWithLayers = (layers: XYLayerConfig[]): XYState => ({
preferredSeriesType: 'area',
legend: { isVisible: false, position: 'left' },
layers,
});

const props: Omit<Parameters<typeof LayerHeader>[0], 'state'> = {
layerId: 'layer-123',
frame: {} as FramePublicAPI,
onChangeIndexPattern: () => {},
setState: () => {},
};

expect(
mountWithIntl(<LayerHeader {...props} state={getStateWithLayers([byValueLayer])} />)
.text()
.trim()
).toBe('Annotations');

expect(
mountWithIntl(<LayerHeader {...props} state={getStateWithLayers([byRefLayer])} />)
.text()
.trim()
).toBe(byRefGroupTitle);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { State, visualizationTypes, SeriesType, XYAnnotationLayerConfig } from '
import { isHorizontalChart, isHorizontalSeries } from '../state_helpers';
import { ChangeIndexPattern, StaticHeader } from '../../../shared_components';
import { updateLayer } from '.';
import { isAnnotationsLayer, isDataLayer, isReferenceLayer } from '../visualization_helpers';
import {
isAnnotationsLayer,
isByReferenceAnnotationsLayer,
isDataLayer,
isReferenceLayer,
} from '../visualization_helpers';

export function LayerHeader(props: VisualizationLayerWidgetProps<State>) {
const layer = props.state.layers.find((l) => l.layerId === props.layerId);
Expand All @@ -30,7 +35,11 @@ export function LayerHeader(props: VisualizationLayerWidgetProps<State>) {
return <ReferenceLayerHeader />;
}
if (isAnnotationsLayer(layer)) {
return <AnnotationsLayerHeader />;
return (
<AnnotationsLayerHeader
title={isByReferenceAnnotationsLayer(layer) ? layer.__lastSaved.title : undefined}
/>
);
}
return <DataLayerHeader {...props} />;
}
Expand All @@ -54,13 +63,16 @@ function ReferenceLayerHeader() {
);
}

function AnnotationsLayerHeader() {
function AnnotationsLayerHeader({ title }: { title: string | undefined }) {
return (
<StaticHeader
icon={IconChartBarAnnotations}
label={i18n.translate('xpack.lens.xyChart.layerAnnotationsLabel', {
defaultMessage: 'Annotations',
})}
label={
title ||
i18n.translate('xpack.lens.xyChart.layerAnnotationsLabel', {
defaultMessage: 'Annotations',
})
}
/>
);
}
Expand Down

0 comments on commit 10bb53d

Please sign in to comment.