Skip to content

Commit

Permalink
[EuiStat] Allow customizing the render of the title and description H…
Browse files Browse the repository at this point in the history
…TML tags (#3693)

* Add titleElement and descriptionElement to EuiStat

* Updated snapshots

* Updated changelog

* Fixed issue with screenreader

* Updated snapshots

* Use screen reader only if title and description is a string

* updated snapshots

* Merge remote-tracking branch 'upstream/master' into fix/stat

* Fixed typo in changelod

* removed titleChildren

* titlechildren as variable

* Update CHANGELOG.md

Remove an extra line left over from a merge resolution

Co-authored-by: Chandler Prall <chandler.prall@gmail.com>
  • Loading branch information
ashikmeerankutty and chandlerprall authored Jul 14, 2020
1 parent f7d5843 commit c09ece9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
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)}
</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

0 comments on commit c09ece9

Please sign in to comment.