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

[EuiBreadcrumbs] Fix the last breadcrumb in the array not respecting truncate overrides #6280

Merged
merged 5 commits into from
Sep 30, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -629,88 +629,31 @@ exports[`EuiBreadcrumbs props responsive is rendered with custom breakpoints 1`]
</nav>
`;

exports[`EuiBreadcrumbs props truncate as false is rendered 1`] = `
exports[`EuiBreadcrumbs truncation setting truncate on breadcrumbs parents cascades down to all children 1`] = `
<nav
aria-label="Breadcrumbs"
class="euiBreadcrumbs"
>
<ol
class="euiBreadcrumbs__list emotion-euiBreadcrumbs__list"
>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page"
>
<a
class="euiLink euiBreadcrumb__content customClass emotion-euiLink-primary-euiBreadcrumb__content-page"
data-test-subj="breadcrumbsAnimals"
href="#"
rel="noreferrer"
>
Animals
</a>
</li>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page"
>
<span
class="euiBreadcrumb__content emotion-euiBreadcrumb__content-page-euiTextColor-subdued"
>
Metazoans
A
</span>
</li>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page-isCollapsed"
>
<div
class="euiPopover emotion-euiPopover"
>
<div
class="euiPopover__anchor css-16vtueo-render"
>
<button
aria-label="See collapsed breadcrumbs"
class="euiLink euiBreadcrumb__content emotion-euiLink-subdued-euiBreadcrumb__content-page"
title="See collapsed breadcrumbs"
type="button"
>
<span
data-euiicon-type="arrowDown"
/>
</button>
</div>
</div>
</li>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page"
>
<button
class="euiLink euiBreadcrumb__content emotion-euiLink-subdued-euiBreadcrumb__content-page"
type="button"
>
Reptiles
</button>
</li>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page"
>
<a
class="euiLink euiBreadcrumb__content emotion-euiLink-subdued-euiBreadcrumb__content-page-isTruncated"
href="#"
rel="noreferrer"
title="Boa constrictor has an error"
>
Boa constrictor
</a>
</li>
<li
class="euiBreadcrumb emotion-euiBreadcrumb-page"
>
<span
aria-current="page"
class="euiBreadcrumb__content emotion-euiBreadcrumb__content-page-euiTextColor-default"
>
Edit
B
</span>
</li>
</ol>
Expand Down
6 changes: 4 additions & 2 deletions src/components/breadcrumbs/breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type _EuiBreadcrumbProps = {
isLastBreadcrumb?: boolean;
isOnlyBreadcrumb?: boolean;
highlightLastBreadcrumb?: boolean;
truncateLastBreadcrumb?: boolean;
} & Pick<EuiBreadcrumbProps, 'truncate'>;

export const EuiBreadcrumb: FunctionComponent<
Expand Down Expand Up @@ -100,6 +101,7 @@ export const EuiBreadcrumbContent: FunctionComponent<
isLastBreadcrumb,
isOnlyBreadcrumb,
highlightLastBreadcrumb,
truncateLastBreadcrumb,
...rest
}) => {
const classes = classNames('euiBreadcrumb__content', className);
Expand All @@ -109,8 +111,8 @@ export const EuiBreadcrumbContent: FunctionComponent<
const cssStyles = [
styles.euiBreadcrumb__content,
styles[type],
truncate &&
(isLastBreadcrumb ? styles.isTruncatedLast : styles.isTruncated),
truncate && !truncateLastBreadcrumb && styles.isTruncated,
truncateLastBreadcrumb && styles.isTruncatedLast,
];
if (type === 'application') {
if (isOnlyBreadcrumb) {
Expand Down
106 changes: 96 additions & 10 deletions src/components/breadcrumbs/breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../test';
import { requiredProps, replaceEmotionPrefix } from '../../test';

import { EuiBreadcrumbs, EuiBreadcrumb } from './';

Expand Down Expand Up @@ -74,6 +74,101 @@ describe('EuiBreadcrumbs', () => {
expect(component).toMatchSnapshot();
});

describe('truncation', () => {
test('setting truncate on breadcrumbs parents cascades down to all children', () => {
const component = render(
<EuiBreadcrumbs
breadcrumbs={[{ text: 'A' }, { text: 'B' }]}
truncate={false}
/>
);
expect(component).toMatchSnapshot();
});

const getBreadcrumbClass = (component: Cheerio, dataTestSubj: string) =>
replaceEmotionPrefix(
component.find(`[data-test-subj=${dataTestSubj}]`).attr('class')
);

test('child breadcrumbs can override truncate set on parent breadcrumbs', () => {
const component = render(
<>
<EuiBreadcrumbs
breadcrumbs={[
{ text: 'A', 'data-test-subj': 'A', truncate: false },
{ text: 'B', 'data-test-subj': 'B' },
]}
truncate
/>
<EuiBreadcrumbs
breadcrumbs={[
{ text: 'C', 'data-test-subj': 'C', truncate: true },
{ text: 'D', 'data-test-subj': 'D' },
]}
truncate={false}
/>
</>
);
expect(getBreadcrumbClass(component, 'A')).toEqual(
'emotion-euiBreadcrumb__content-page-euiTextColor-subdued'
);
expect(getBreadcrumbClass(component, 'C')).toEqual(
'emotion-euiBreadcrumb__content-page-isTruncated-euiTextColor-subdued'
);
});

describe('last breadcrumb', () => {
describe('if the parent truncate is true and the last breadcrumb does not have its own truncate property', () => {
it('sets a isTruncatedLast style that allows the last breadcrumb to occupy the remaining width of the breadcrumbs line', () => {
const component = render(
<EuiBreadcrumbs
breadcrumbs={[
{ text: 'A' },
{ text: 'B', 'data-test-subj': 'last' },
]}
truncate
/>
);
expect(getBreadcrumbClass(component, 'last')).toEqual(
'emotion-euiBreadcrumb__content-page-isTruncatedLast-euiTextColor-default'
);
});
});

describe('if the last breadcrumb has its own truncate property', () => {
it('uses the normal isTruncated if truncate is true', () => {
const component = render(
<EuiBreadcrumbs
breadcrumbs={[
{ text: 'A' },
{ text: 'B', 'data-test-subj': 'last', truncate: true },
]}
truncate
/>
);
expect(getBreadcrumbClass(component, 'last')).toEqual(
'emotion-euiBreadcrumb__content-page-isTruncated-euiTextColor-default'
);
});

it('does not set any truncation classes if truncate is false', () => {
const component = render(
<EuiBreadcrumbs
breadcrumbs={[
{ text: 'A' },
{ text: 'B', 'data-test-subj': 'last', truncate: false },
]}
truncate
/>
);
expect(getBreadcrumbClass(component, 'last')).toEqual(
'emotion-euiBreadcrumb__content-page-euiTextColor-default'
);
});
});
});
});

describe('props', () => {
describe('responsive', () => {
test('is rendered', () => {
Expand Down Expand Up @@ -101,15 +196,6 @@ describe('EuiBreadcrumbs', () => {
});
});

describe('truncate as false', () => {
test('is rendered', () => {
const component = render(
<EuiBreadcrumbs breadcrumbs={breadcrumbs} truncate={false} />
);
expect(component).toMatchSnapshot();
});
});

describe('max', () => {
test('renders 1 item', () => {
const component = render(
Expand Down
9 changes: 6 additions & 3 deletions src/components/breadcrumbs/breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const EuiBreadcrumbs: FunctionComponent<EuiBreadcrumbsProps> = ({
const isLastBreadcrumb = index === visibleBreadcrumbs.length - 1;
const isOnlyBreadcrumb = visibleBreadcrumbs.length === 1;

const sharedProps = { type, truncate };
const sharedProps = { type, truncate: breadcrumb.truncate ?? truncate };

return breadcrumb.isCollapsedButton ? (
<EuiBreadcrumbCollapsed
Expand All @@ -139,14 +139,17 @@ export const EuiBreadcrumbs: FunctionComponent<EuiBreadcrumbsProps> = ({
) : (
<EuiBreadcrumb key={index} {...sharedProps}>
<EuiBreadcrumbContent
{...breadcrumb}
{...sharedProps}
isFirstBreadcrumb={isFirstBreadcrumb}
isLastBreadcrumb={isLastBreadcrumb}
isOnlyBreadcrumb={isOnlyBreadcrumb}
highlightLastBreadcrumb={
isLastBreadcrumb && lastBreadcrumbIsCurrentPage
}
{...sharedProps}
{...breadcrumb}
truncateLastBreadcrumb={
isLastBreadcrumb && truncate && breadcrumb.truncate == null
}
/>
</EuiBreadcrumb>
);
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/6280.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed the last breadcrumb in `EuiBreadcrumbs`'s `breadcrumbs` array not respecting `truncate` overrides