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 3 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` prop to `EuiStat` ([#3693](https://github.com/elastic/eui/pull/3693))
- Added `background.color` to `EUI_CHARTS_THEME_LIGHT/DARK.theme` ([#3669](https://github.com/elastic/eui/pull/3669))
- Added `gutterSize` prop to `EuiFacetGroup` ([#3639](https://github.com/elastic/eui/pull/3639))
- Updated props of `EuiCode` and `EuiCodeBlock` to reflect only functional props ([#3647](https://github.com/elastic/eui/pull/3647))
Expand Down
59 changes: 59 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,65 @@ 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>
<p
class="euiScreenReaderOnly"
>
[object Object] 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>
<p
class="euiScreenReaderOnly"
>
description [object Object]
</p>
</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
32 changes: 27 additions & 5 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);
ashikmeerankutty marked this conversation as resolved.
Show resolved Hide resolved

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 Down