Skip to content

Commit

Permalink
♻️ Refactor badge component into single codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Apr 21, 2023
1 parent 10fe021 commit a372d04
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 123 deletions.
75 changes: 10 additions & 65 deletions x-pack/plugins/lens/public/datasources/form_based/info_badges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@
* 2.0.
*/

import {
EuiColorPaletteDisplay,
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiText,
useEuiTheme,
} from '@elastic/eui';
import { css } from '@emotion/react';
import { EuiText } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { FormBasedLayer } from '../..';
import { InfoBadge } from '../../shared_components/info_badges/info_badge';
import { FramePublicAPI, VisualizationInfo } from '../../types';
import { getSamplingValue } from './utils';

Expand All @@ -29,7 +22,6 @@ export function ReducedSamplingSectionEntries({
visualizationInfo: VisualizationInfo;
dataViews: FramePublicAPI['dataViews'];
}) {
const { euiTheme } = useEuiTheme();
return (
<>
{layers.map(([id, layer], layerIndex) => {
Expand All @@ -42,62 +34,15 @@ export function ReducedSamplingSectionEntries({
});
const layerPalette = layerInfo?.palette;
return (
<li
key={`${layerTitle}-${dataView}-${layerIndex}`}
data-test-subj={`lns-feature-badges-reducedSampling-${layerIndex}`}
css={css`
margin: ${euiTheme.size.base} 0 0;
`}
<InfoBadge
title={layerTitle}
index={layerIndex}
dataView={dataView.id}
palette={layerPalette}
data-test-subj-prefix="lns-feature-badges-reducedSampling"
>
<EuiFlexGroup justifyContent={layerPalette ? 'center' : 'spaceBetween'} gutterSize="s">
{layerPalette ? (
<EuiFlexItem grow={false}>
{layerPalette.length === 1 ? (
<EuiIcon
color={layerPalette[0]}
type="stopFilled"
css={css`
margin-top: 2px;
`}
/>
) : (
<EuiIcon
type="color"
css={css`
margin-top: 2px;
`}
/>
)}
</EuiFlexItem>
) : null}
<EuiFlexItem
grow={Boolean(layerPalette)}
css={css`
width: 150px;
`}
>
<EuiText size="s">{layerTitle}</EuiText>
{layerPalette && layerPalette.length > 1 ? (
<EuiColorPaletteDisplay
size="xs"
palette={layerPalette}
css={css`
margin-top: 4px;
width: 150px;
`}
/>
) : null}
</EuiFlexItem>
<EuiFlexItem
grow={false}
css={css`
padding-right: 0;
`}
>
<EuiText size="s">{`${Number(getSamplingValue(layer)) * 100}%`}</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
</li>
<EuiText size="s">{`${Number(getSamplingValue(layer)) * 100}%`}</EuiText>
</InfoBadge>
);
})}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 { render } from '@testing-library/react';
import { InfoBadge } from './info_badge';

describe('Info badge', () => {
it('should render no icon if no palette is passed', () => {
const res = render(
<InfoBadge title="my Title" dataView="dataView" index={0} data-test-subj-prefix="prefix" />
);

expect(res.queryByTestId('prefix-0-icon')).not.toBeInTheDocument();
expect(res.queryByTestId('prefix-0-palette')).not.toBeInTheDocument();
});

it('should render an icon if a single palette color is passed over', () => {
const res = render(
<InfoBadge
title="my Title"
dataView="dataView"
index={0}
data-test-subj-prefix="prefix"
palette={['red']}
/>
);

expect(res.queryByTestId('prefix-0-icon')).toBeInTheDocument();
expect(res.queryByTestId('prefix-0-palette')).not.toBeInTheDocument();
});

it('should render both an icon an a palette indicator if multiple colors are passed over', () => {
const res = render(
<InfoBadge
title="my Title"
dataView="dataView"
index={0}
data-test-subj-prefix="prefix"
palette={['red', 'blue']}
/>
);

expect(res.queryByTestId('prefix-0-icon')).toBeInTheDocument();
expect(res.queryByTestId('prefix-0-palette')).toBeInTheDocument();
});

it('should render children as value when passed', () => {
const res = render(
<InfoBadge title="my Title" dataView="dataView" index={0} data-test-subj-prefix="prefix">
<div>100%</div>
</InfoBadge>
);
expect(res.getByText('100%')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 {
EuiColorPaletteDisplay,
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiText,
useEuiTheme,
} from '@elastic/eui';
import { css } from '@emotion/react';
import React, { type ReactChildren, type ReactChild } from 'react';

export function InfoBadge({
title,
dataView,
index,
palette,
children,
'data-test-subj-prefix': dataTestSubjPrefix,
}: {
title: string;
dataView: string;
index: number;
palette?: string[];
children?: ReactChild | ReactChildren;
'data-test-subj-prefix': string;
}) {
const { euiTheme } = useEuiTheme();
const hasColor = Boolean(palette);
const hasSingleColor = palette && palette.length === 1;
const hasMultipleColors = palette && palette.length > 1;
const iconType = hasSingleColor ? 'stopFilled' : 'color';
return (
<li
key={`${title}-${dataView}-${index}`}
data-test-subj={`${dataTestSubjPrefix}-${index}`}
css={css`
margin: ${euiTheme.size.base} 0 0;
`}
>
<EuiFlexGroup justifyContent={hasColor ? 'center' : 'spaceBetween'} gutterSize="s">
{hasColor ? (
<EuiFlexItem grow={false}>
<EuiIcon
color={hasSingleColor ? palette[0] : undefined}
type={iconType}
css={css`
margin-top: ${euiTheme.size.xxs};
`}
data-test-subj={`${dataTestSubjPrefix}-${index}-icon`}
/>
</EuiFlexItem>
) : null}
<EuiFlexItem grow={Boolean(palette)}>
<EuiText size="s" data-test-subj={`${dataTestSubjPrefix}-${index}-title`}>
{title}
</EuiText>
</EuiFlexItem>
<EuiFlexItem
grow={false}
css={css`
padding-right: 0;
`}
>
{children}
</EuiFlexItem>
</EuiFlexGroup>
{hasMultipleColors ? (
<EuiFlexGroup
justifyContent={'center'}
gutterSize="s"
css={css`
margin-top: ${euiTheme.size.xs};
overflow-y: hidden;
`}
>
<EuiFlexItem
grow={false}
css={css`
height: ${euiTheme.size.xs};
`}
>
<EuiIcon type="empty" />
</EuiFlexItem>
<EuiFlexItem
grow={Boolean(palette)}
css={css`
height: ${euiTheme.size.xs};
`}
>
<EuiColorPaletteDisplay
size="xs"
palette={palette}
data-test-subj={`${dataTestSubjPrefix}-${index}-palette`}
/>
</EuiFlexItem>
</EuiFlexGroup>
) : null}
</li>
);
}
66 changes: 8 additions & 58 deletions x-pack/plugins/lens/public/visualizations/xy/info_badges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@
* 2.0.
*/

import {
EuiColorPaletteDisplay,
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiText,
useEuiTheme,
} from '@elastic/eui';
import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { InfoBadge } from '../../shared_components/info_badges/info_badge';
import { FramePublicAPI, VisualizationInfo } from '../../types';
import { XYAnnotationLayerConfig } from './types';

Expand All @@ -28,7 +20,6 @@ export function IgnoredGlobalFiltersEntries({
visualizationInfo: VisualizationInfo;
dataViews: FramePublicAPI['dataViews'];
}) {
const { euiTheme } = useEuiTheme();
return (
<>
{layers.map((layer, layerIndex) => {
Expand All @@ -41,54 +32,13 @@ export function IgnoredGlobalFiltersEntries({
});
const layerPalette = layerInfo?.palette;
return (
<li
key={`${layerTitle}-${dataView}-${layerIndex}`}
data-test-subj={`lns-feature-badges-ignoreGlobalFilters-${layerIndex}`}
css={css`
margin: ${euiTheme.size.base} 0 0;
`}
>
<EuiFlexGroup justifyContent={layerPalette ? 'center' : 'spaceBetween'} gutterSize="s">
{layerPalette ? (
<EuiFlexItem grow={false}>
{layerPalette.length === 1 ? (
<EuiIcon
color={layerPalette[0]}
type="stopFilled"
css={css`
margin-top: 2px;
`}
/>
) : (
<EuiIcon
type="color"
css={css`
margin-top: 2px;
`}
/>
)}
</EuiFlexItem>
) : null}
<EuiFlexItem
grow={Boolean(layerPalette)}
css={css`
width: 150px;
`}
>
<EuiText size="s">{layerTitle}</EuiText>
{layerPalette && layerPalette.length > 1 ? (
<EuiColorPaletteDisplay
size="xs"
palette={layerPalette}
css={css`
margin-top: 4px;
width: 150px;
`}
/>
) : null}
</EuiFlexItem>
</EuiFlexGroup>
</li>
<InfoBadge
title={layerTitle}
index={layerIndex}
dataView={dataView.id}
palette={layerPalette}
data-test-subj-prefix="lns-feature-badges-ignoreGlobalFilters"
/>
);
})}
</>
Expand Down

0 comments on commit a372d04

Please sign in to comment.