Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -151,6 +151,15 @@ export const renderBreadcrumbItem_unstable: (state: BreadcrumbItemState) => JSX.
// @public
export const renderBreadcrumbLink_unstable: (state: BreadcrumbLinkState) => JSX.Element;

// @public (undocumented)
export const truncate: (content: string, maxLength: number) => string;

// @public (undocumented)
export const truncateLongName: (content: string, maxLength?: number | undefined) => string;

// @public (undocumented)
export const truncateLongTooltip: (content: string, maxLength?: number | undefined) => string;

// @public
export const useBreadcrumb_unstable: (props: BreadcrumbProps, ref: React_2.Ref<HTMLElement>) => BreadcrumbState;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/react-breadcrumb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export {
useBreadcrumbItem_unstable,
} from './BreadcrumbItem';
export type { BreadcrumbItemProps, BreadcrumbItemSlots, BreadcrumbItemState } from './BreadcrumbItem';
export { partitionBreadcrumbItems } from './utils/index';
export { partitionBreadcrumbItems, truncate, truncateLongName, truncateLongTooltip } from './utils/index';
export type { PartitionBreadcrumbItemsOptions, PartitionBreadcrumbItems } from './utils/index';
export {
BreadcrumbButton,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { partitionBreadcrumbItems } from './partitionBreadcrumbItems';
export type { PartitionBreadcrumbItems, PartitionBreadcrumbItemsOptions } from './partitionBreadcrumbItems';
export { truncate, truncateLongName, truncateLongTooltip } from './truncateLongContent';
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { truncateLongName, truncateLongTooltip } from './truncateLongContent';

const defaultTestNames = [
['', ''],
['Regular name', 'Regular name'],
['Name which is longer than 30 characters', 'Name which is longer than 30 c...'],
['Name which equal 30 characters', 'Name which equal 30 characters'],
];

describe('truncate method', () => {
it.each(defaultTestNames)('truncates long name correctly', (name, expected) => {
Comment thread
Hotell marked this conversation as resolved.
Outdated
expect(truncateLongName(name)).toBe(expected);
});
it('truncates name correctly with custom length', () => {
expect(truncateLongName('Name which is longer than 10 characters', 10)).toEqual('Name which...');
});
it('truncates RTL name correctly with custom length', () => {
expect(truncateLongName('رحیمی خسرو رحیمی', 10)).toEqual('رحیمی خسرو...');
});
it('returns the same content for regular tooltips', () => {
expect(truncateLongTooltip('Just a tooltip')).toEqual('Just a tooltip');
});
it('truncates long tooltip correctly', () => {
const longTooltipContent =
"Super long tooltip which is longer than 80 characters. Don't think about what you want to be, but what you want to do.";
expect(truncateLongTooltip(longTooltipContent)).toEqual(
"Super long tooltip which is longer than 80 characters. Don't think about what yo...",
);
expect(truncateLongTooltip(longTooltipContent, 100)).toEqual(
"Super long tooltip which is longer than 80 characters. Don't think about what you want to be, but wh...",
);
});
it('truncates long tooltip correctly with custom length', () => {
const longTooltipContent =
"Super long tooltip which is longer than 80 characters. Don't think about what you want to be, but what you want to do.";
expect(truncateLongTooltip(longTooltipContent, 100)).toEqual(
"Super long tooltip which is longer than 80 characters. Don't think about what you want to be, but wh...",
);
});
});
Comment thread
Hotell marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const MAX_NAME_LENGTH = 30;
const MAX_TOOLTIP_LENGTH = 80;

export const truncate = (content: string, maxLength: number): string => {
return content.length > maxLength ? content.trim().slice(0, maxLength).concat('...') : content;
};

export const truncateLongName = (content: string, maxLength?: number): string => {
Comment thread
ValentinaKozlova marked this conversation as resolved.
Outdated
const truncateLength = maxLength || MAX_NAME_LENGTH;
return truncate(content, truncateLength);
};

export const truncateLongTooltip = (content: string, maxLength?: number): string => {
Comment thread
Hotell marked this conversation as resolved.
Outdated
const truncateLength = maxLength || MAX_TOOLTIP_LENGTH;
return truncate(content, truncateLength);
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,103 @@
import * as React from 'react';
import { Breadcrumb, BreadcrumbItem, BreadcrumbDivider } from '@fluentui/react-breadcrumb';
import { ArrowRight16Filled } from '@fluentui/react-icons';

export const Default = () => (
<Breadcrumb>
<BreadcrumbItem>Item with custom divider</BreadcrumbItem>
<BreadcrumbDivider>
<ArrowRight16Filled />
</BreadcrumbDivider>
<BreadcrumbItem>Item</BreadcrumbItem>
<BreadcrumbDivider />
<BreadcrumbItem current>Item</BreadcrumbItem>
</Breadcrumb>
);
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbDivider,
BreadcrumbProps,
partitionBreadcrumbItems,
BreadcrumbButton,
truncateLongName,
truncateLongTooltip,
} from '@fluentui/react-breadcrumb';
import type { PartitionBreadcrumbItems } from '@fluentui/react-breadcrumb';
import { ArrowRight16Filled, MoreHorizontalRegular, MoreHorizontalFilled, bundleIcon } from '@fluentui/react-icons';
import { Tooltip } from '@fluentui/react-components';

const MoreHorizontal = bundleIcon(MoreHorizontalFilled, MoreHorizontalRegular);

type Item = {
key: number;
value: string;
};
const items: Item[] = [
{
key: 0,
value: 'Item 1',
},
{
key: 1,
value: 'Item 2',
},
{
key: 2,
value: 'Item 3',
},
{
key: 3,
value: 'Item 4',
},
{
key: 4,
value: 'Item 5 which is longer than 30 characters',
},
{
key: 5,
value: "Item 6 is long even for tooltip. Don't think about what you want to be, but what you want to do.",
},
];

function renderItem(item: Item, size: BreadcrumbProps['size']) {
const isLastItem = items.length - 1 === item.key;
return (
<React.Fragment key={`${size}-item-${item.key}`}>
<Tooltip withArrow content={truncateLongTooltip(item.value)} relationship="label">
<BreadcrumbItem current={isLastItem}>{truncateLongName(item.value)}</BreadcrumbItem>
</Tooltip>
{!isLastItem && <BreadcrumbDivider />}
</React.Fragment>
);
}

export const Default = () => {
const { startDisplayedItems, overflowItems, endDisplayedItems }: PartitionBreadcrumbItems<Item> =
partitionBreadcrumbItems({
items,
maxDisplayedItems: 3,
});
return (
<>
<Breadcrumb aria-label="breadcrumb-with-overflow">
{startDisplayedItems.map(item => renderItem(item, 'medium'))}
{overflowItems && (
<BreadcrumbItem>
<Tooltip withArrow content={getTooltipContent(overflowItems)} relationship="label">
<BreadcrumbButton icon={<MoreHorizontal />} aria-label={`more items`} />
</Tooltip>
</BreadcrumbItem>
)}
{endDisplayedItems && endDisplayedItems.map(item => renderItem(item, 'medium'))}
</Breadcrumb>
<Breadcrumb>
<BreadcrumbItem>Item with custom divider</BreadcrumbItem>
<BreadcrumbDivider>
<ArrowRight16Filled />
</BreadcrumbDivider>
<BreadcrumbItem>Item</BreadcrumbItem>
<BreadcrumbDivider />
<BreadcrumbItem current>Item</BreadcrumbItem>
</Breadcrumb>
</>
);
};

const getTooltipContent = (breadcrumbItems: readonly Item[]) => {
return breadcrumbItems.reduce((acc, initialValue, idx, arr) => {
return (
<div style={{ display: 'flex' }}>
{acc}
{arr[0].value !== initialValue.value && <BreadcrumbDivider />}
{initialValue.value}
</div>
);
}, <div style={{ display: 'flex' }} />);
};