Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiStat] Allow customizing the render of the title and description HTML tags #3693

Merged
merged 14 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `titleElement` and `descriptionElement` props to `EuiStat` ([#3693](https://github.com/elastic/eui/pull/3693))
- Updated `securityAnalyticsApp` app icon ([#3720](https://github.com/elastic/eui/pull/3720))
- Removed `src/test` and `@types/enzyme` references from `eui.d.ts` ([#3715](https://github.com/elastic/eui/pull/3715))
- Added `index.d.ts` file to `lib/test` and `es/test` ([#3715](https://github.com/elastic/eui/pull/3715))
Expand Down
49 changes: 49 additions & 0 deletions src/components/stat/__snapshots__/stat.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,55 @@ exports[`EuiStat props primary is rendered 1`] = `
</div>
`;

exports[`EuiStat props render with custom description element 1`] = `
<div
class="euiStat euiStat--leftAligned"
>
<div
class="euiText euiText--small euiStat__description"
>
<div
aria-hidden="true"
>
<div>
description
</div>
</div>
</div>
<p
aria-hidden="true"
class="euiTitle euiTitle--large euiStat__title"
style="color:#EB1919"
>
title
</p>
</div>
`;

exports[`EuiStat props render with custom title element 1`] = `
<div
class="euiStat euiStat--leftAligned"
>
<div
class="euiText euiText--small euiStat__description"
>
<p
aria-hidden="true"
>
description
</p>
</div>
<div
aria-hidden="true"
class="euiTitle euiTitle--large euiStat__title"
>
<div>
title
</div>
</div>
</div>
`;

exports[`EuiStat props right is rendered 1`] = `
<div
class="euiStat euiStat--rightAligned"
Expand Down
25 changes: 25 additions & 0 deletions src/components/stat/stat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ describe('EuiStat', () => {
expect(component).toMatchSnapshot();
});

test('render with custom description element', () => {
const component = render(
<EuiStat
title="title"
description={<div>description</div>}
descriptionElement="div"
titleColor="#EB1919"
/>
);

expect(component).toMatchSnapshot();
});

test('render with custom title element', () => {
const component = render(
<EuiStat
title={<div>title</div>}
titleElement="div"
description="description"
/>
);

expect(component).toMatchSnapshot();
});

TITLE_SIZES.forEach(size => {
test(`${size} is rendered`, () => {
const component = render(
Expand Down
36 changes: 30 additions & 6 deletions src/components/stat/stat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, {
HTMLAttributes,
FunctionComponent,
ReactNode,
createElement,
} from 'react';
import { CommonProps, keysOf } from '../common';
import classNames from 'classnames';
Expand Down Expand Up @@ -82,6 +83,14 @@ export interface EuiStatProps {
* Size of the title. See EuiTitle for options ('s', 'm', 'l'... etc)
*/
titleSize?: EuiTitleSize;
/**
* HTML Element to be used for title
*/
titleElement?: string;
/**
* HTML Element to be used for description
*/
descriptionElement?: string;
}

export const EuiStat: FunctionComponent<
Expand All @@ -96,6 +105,8 @@ export const EuiStat: FunctionComponent<
title,
titleColor = 'default',
titleSize = 'l',
titleElement = 'p',
descriptionElement = 'p',
...rest
}) => {
const classes = classNames(
Expand All @@ -112,21 +123,32 @@ export const EuiStat: FunctionComponent<
}
);

const commonProps = {
'aria-hidden': true,
};

const descriptionDisplay = (
<EuiText size="s" className="euiStat__description">
<p aria-hidden="true">{description}</p>
{createElement(descriptionElement, commonProps, description)}
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
</EuiText>
);

const titlePropsWithColor = {
'aria-hidden': true,
style: {
color: `${titleColor}`,
},
};

const titleChildren = isLoading ? '--' : title;

const titleDisplay = isColorClass(titleColor) ? (
<EuiTitle size={titleSize} className={titleClasses}>
<p aria-hidden="true">{isLoading ? '--' : title}</p>
{createElement(titleElement, commonProps, titleChildren)}
</EuiTitle>
) : (
<EuiTitle size={titleSize} className={titleClasses}>
<p aria-hidden="true" style={{ color: `${titleColor}` }}>
{isLoading ? '--' : title}
</p>
{createElement(titleElement, titlePropsWithColor, titleChildren)}
</EuiTitle>
);

Expand All @@ -149,7 +171,9 @@ export const EuiStat: FunctionComponent<
{!reverse && descriptionDisplay}
{titleDisplay}
{reverse && descriptionDisplay}
{screenReader}
{typeof title === 'string' &&
typeof description === 'string' &&
screenReader}
</Fragment>
);

Expand Down