-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor badge component into single codebase
- Loading branch information
Showing
4 changed files
with
185 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
x-pack/plugins/lens/public/shared_components/info_badges/info_badge.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
107 changes: 107 additions & 0 deletions
107
x-pack/plugins/lens/public/shared_components/info_badges/info_badge.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters