-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Added truncation and tooltips to the Breadcrumb #27859
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
Merged
ValentinaKozlova
merged 4 commits into
microsoft:master
from
ValentinaKozlova:feat/breadcrumb-truncation
May 31, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b903b3b
Added tooltip for BreadcrumbItem and truncation of long content
ValentinaKozlova 9c0858b
Mde length according to the design. They don't include '...' as part …
ValentinaKozlova ce268e0
PR fix
ValentinaKozlova 13b83a4
PR fix #2
ValentinaKozlova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
40 changes: 40 additions & 0 deletions
40
packages/react-components/react-breadcrumb/src/utils/truncateLongContent.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) => { | ||
| 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...", | ||
| ); | ||
| }); | ||
| }); | ||
16 changes: 16 additions & 0 deletions
16
packages/react-components/react-breadcrumb/src/utils/truncateLongContent.ts
|
Hotell marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 => { | ||
|
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 => { | ||
|
Hotell marked this conversation as resolved.
Outdated
|
||
| const truncateLength = maxLength || MAX_TOOLTIP_LENGTH; | ||
| return truncate(content, truncateLength); | ||
| }; | ||
116 changes: 102 additions & 14 deletions
116
...eact-components/react-breadcrumb/stories/BreadcrumbItem/BreadcrumbItemDefault.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' }} />); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.