-
Notifications
You must be signed in to change notification settings - Fork 133
feat: add ParentBreadcrumbs component [FC-0090] #2223
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
Open
rpenido
wants to merge
5
commits into
openedx:master
Choose a base branch
from
open-craft:rpenido/fal-4216-unit-page-breadcrumbs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+373
−128
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"comment": "This mock is captured from a real search result and roughly edited to match the mocks in src/library-authoring/data/api.mocks.ts", | ||
"note": "The _formatted fields have been removed from this result and should be re-added programatically when mocking.", | ||
"results": [ | ||
{ | ||
"indexUid": "studio_content", | ||
"hits": [ | ||
{ | ||
"display_name": "Test Unit", | ||
"block_id": "test-unit-9284e2", | ||
"id": "lctAximTESTunittest-unit-9284e2-a9a4386e", | ||
"type": "library_container", | ||
"breadcrumbs": [ | ||
{ | ||
"display_name": "Test Library" | ||
} | ||
], | ||
"created": 1742221203.895054, | ||
"modified": 1742221203.895054, | ||
"usage_key": "lct:org:lib:unit:test-unit-9a207", | ||
"block_type": "unit", | ||
"context_key": "lib:Axim:TEST", | ||
"org": "Axim", | ||
"access_id": 15, | ||
"num_children": 0, | ||
"_formatted": { | ||
"display_name": "Test Unit", | ||
"block_id": "test-unit-9284e2", | ||
"id": "lctAximTESTunittest-unit-9284e2-a9a4386e", | ||
"type": "library_container", | ||
"breadcrumbs": [ | ||
{ | ||
"display_name": "Test Library" | ||
} | ||
], | ||
"created": "1742221203.895054", | ||
"modified": "1742221203.895054", | ||
"usage_key": "lct:org:lib:unit:test-unit-9a207", | ||
"block_type": "unit", | ||
"context_key": "lib:Axim:TEST", | ||
"org": "Axim", | ||
"access_id": "15", | ||
"num_children": "0", | ||
"published": { | ||
"display_name": "Published Test Unit" | ||
} | ||
}, | ||
"published": { | ||
"display_name": "Published Test Unit" | ||
} | ||
} | ||
], | ||
"query": "", | ||
"processingTimeMs": 1, | ||
"limit": 20, | ||
"offset": 0, | ||
"estimatedTotalHits": 10 | ||
} | ||
] | ||
} |
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 @@ | ||
@import "./history-widget/HistoryWidget"; | ||
@import "./status-widget/StatusWidget"; | ||
@import "./parent-breadcrumbs"; |
99 changes: 99 additions & 0 deletions
99
src/library-authoring/generic/parent-breadcrumbs/ParentBreadcrumbs.test.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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||
import { fireEvent, render, screen } from '@testing-library/react'; | ||||||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||||||
import { BrowserRouter } from 'react-router-dom'; | ||||||
|
||||||
import { ContainerType } from '@src/generic/key-utils'; | ||||||
|
||||||
import { type ContainerParents, ParentBreadcrumbs } from '.'; | ||||||
|
||||||
const mockNavigate = jest.fn(); | ||||||
|
||||||
jest.mock('react-router-dom', () => ({ | ||||||
...jest.requireActual('react-router-dom'), | ||||||
useNavigate: () => mockNavigate, | ||||||
})); | ||||||
|
||||||
const renderComponent = (containerType: ContainerType, parents: ContainerParents) => ( | ||||||
render( | ||||||
<BrowserRouter> | ||||||
<IntlProvider locale="en"> | ||||||
<ParentBreadcrumbs | ||||||
libraryData={{ id: 'library-id', title: 'Library Title' }} | ||||||
containerType={containerType} | ||||||
parents={parents} | ||||||
/> | ||||||
</IntlProvider> | ||||||
</BrowserRouter>, | ||||||
) | ||||||
); | ||||||
|
||||||
describe('<ParentBreadcrumbs />', () => { | ||||||
it('show breadcrubs without parent', async () => { | ||||||
renderComponent(ContainerType.Unit, { displayName: [], key: [] }); | ||||||
const links = screen.queryAllByRole('link'); | ||||||
expect(links).toHaveLength(2); // Library link + Empty link | ||||||
|
||||||
expect(links[0]).toHaveTextContent('Library Title'); | ||||||
expect(links[0]).toHaveProperty('href', 'http://localhost/library/library-id'); | ||||||
|
||||||
expect(links[1]).toHaveTextContent(''); // Empty link for no parent | ||||||
expect(links[1]).toHaveProperty('href', 'http://localhost/'); | ||||||
}); | ||||||
|
||||||
it('show breadcrubs to a unit without one parent', async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
renderComponent(ContainerType.Unit, { displayName: ['Parent Subsection'], key: ['subsection-key'] }); | ||||||
const links = screen.queryAllByRole('link'); | ||||||
expect(links).toHaveLength(2); // Library link + Parent Subsection link | ||||||
|
||||||
expect(links[0]).toHaveTextContent('Library Title'); | ||||||
expect(links[0]).toHaveProperty('href', 'http://localhost/library/library-id'); | ||||||
|
||||||
expect(links[1]).toHaveTextContent('Parent Subsection'); | ||||||
expect(links[1]).toHaveProperty('href', 'http://localhost/library/library-id/subsection/subsection-key'); | ||||||
}); | ||||||
|
||||||
it('show breadcrubs to a subsection without one parent', async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
renderComponent(ContainerType.Subsection, { displayName: ['Parent Section'], key: ['section-key'] }); | ||||||
const links = screen.queryAllByRole('link'); | ||||||
expect(links).toHaveLength(2); // Library link + Parent Subsection link | ||||||
|
||||||
expect(links[0]).toHaveTextContent('Library Title'); | ||||||
expect(links[0]).toHaveProperty('href', 'http://localhost/library/library-id'); | ||||||
|
||||||
expect(links[1]).toHaveTextContent('Parent Section'); | ||||||
expect(links[1]).toHaveProperty('href', 'http://localhost/library/library-id/section/section-key'); | ||||||
}); | ||||||
|
||||||
it('should throw an error if displayName and key arrays are not the same length', async () => { | ||||||
expect(() => renderComponent(ContainerType.Unit, { | ||||||
displayName: ['Parent 1'], | ||||||
key: ['key1', 'key2'], | ||||||
})).toThrow('Parents key and displayName arrays must have the same length.'); | ||||||
}); | ||||||
|
||||||
it('show breadcrubs with multiple parents', async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
renderComponent(ContainerType.Unit, { | ||||||
displayName: ['Parent Subsection 1', 'Parent Subsection 2'], | ||||||
key: ['subsection-key-1', 'subsection-key-2'], | ||||||
}); | ||||||
const links = screen.queryAllByRole('link'); | ||||||
expect(links).toHaveLength(1); // Library link only. Parents are displayed in a dropdown. | ||||||
|
||||||
expect(links[0]).toHaveTextContent('Library Title'); | ||||||
expect(links[0]).toHaveProperty('href', 'http://localhost/library/library-id'); | ||||||
|
||||||
const dropdown = screen.getByRole('button', { name: '2 Subsections' }); | ||||||
expect(dropdown).toBeInTheDocument(); | ||||||
|
||||||
fireEvent.click(dropdown); | ||||||
|
||||||
const subsectionLinks = screen.queryAllByRole('link'); | ||||||
expect(subsectionLinks).toHaveLength(2); // Library link only. Parents are displayed in a dropdown. | ||||||
|
||||||
expect(subsectionLinks[0]).toHaveTextContent('Parent Subsection 1'); | ||||||
expect(subsectionLinks[0]).toHaveProperty('href', 'http://localhost/library/library-id/subsection/subsection-key-1'); | ||||||
|
||||||
expect(subsectionLinks[1]).toHaveTextContent('Parent Subsection 2'); | ||||||
expect(subsectionLinks[1]).toHaveProperty('href', 'http://localhost/library/library-id/subsection/subsection-key-2'); | ||||||
}); | ||||||
}); |
13 changes: 13 additions & 0 deletions
13
src/library-authoring/generic/parent-breadcrumbs/index.scss
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,13 @@ | ||
.breadcrumb-menu { | ||
button { | ||
padding: 0; | ||
} | ||
} | ||
|
||
.parents-breadcrumb { | ||
max-width: 700px; | ||
display: block; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} |
121 changes: 121 additions & 0 deletions
121
src/library-authoring/generic/parent-breadcrumbs/index.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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import type { ReactNode } from 'react'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { Link } from 'react-router-dom'; | ||
import { | ||
Breadcrumb, MenuItem, SelectMenu, | ||
} from '@openedx/paragon'; | ||
import { ContainerType } from '@src/generic/key-utils'; | ||
import type { ContentLibrary } from '../../data/api'; | ||
import messages from './messages'; | ||
|
||
interface OverflowLinksProps { | ||
to: string | string[]; | ||
children: ReactNode | ReactNode[]; | ||
containerType: ContainerType; | ||
} | ||
|
||
const OverflowLinks = ({ children, to, containerType }: OverflowLinksProps) => { | ||
const intl = useIntl(); | ||
|
||
if (typeof to === 'string') { | ||
return ( | ||
<Link className="parents-breadcrumb link-muted" to={to}> | ||
{children} | ||
</Link> | ||
); | ||
} | ||
|
||
// istanbul ignore if: this should never happen | ||
if (!Array.isArray(to) || !Array.isArray(children) || to.length !== children.length) { | ||
throw new Error('Both "to" and "children" should have the same length.'); | ||
} | ||
|
||
// to is string[] that should be converted to overflow menu | ||
const items = to.map((link, index) => ( | ||
<MenuItem key={link} to={link} as={Link}> | ||
{children[index]} | ||
</MenuItem> | ||
)); | ||
|
||
const containerTypeName = containerType === ContainerType.Unit | ||
? intl.formatMessage(messages.breadcrumbsSubsectionsDropdown) | ||
: intl.formatMessage(messages.breadcrumbsSectionsDropdown); | ||
|
||
return ( | ||
<SelectMenu | ||
className="breadcrumb-menu" | ||
variant="link" | ||
defaultMessage={`${items.length} ${containerTypeName}`} | ||
> | ||
{items} | ||
</SelectMenu> | ||
); | ||
}; | ||
|
||
export interface ContainerParents { | ||
displayName?: string[]; | ||
key?: string[]; | ||
} | ||
|
||
type ContentLibraryPartial = Pick<ContentLibrary, 'id' | 'title'> & Partial<ContentLibrary>; | ||
|
||
interface ParentBreadcrumbsProps { | ||
libraryData: ContentLibraryPartial; | ||
parents?: ContainerParents; | ||
containerType: ContainerType; | ||
} | ||
|
||
export const ParentBreadcrumbs = ({ libraryData, parents, containerType }: ParentBreadcrumbsProps) => { | ||
const intl = useIntl(); | ||
const { id: libraryId, title: libraryTitle } = libraryData; | ||
|
||
const links: Array<{ label: string | string[], to: string | string[], containerType: ContainerType }> = [ | ||
{ | ||
label: libraryTitle, | ||
to: `/library/${libraryId}`, | ||
containerType, | ||
}, | ||
]; | ||
|
||
const parentLength = parents?.key?.length || 0; | ||
const parentNameLength = parents?.displayName?.length || 0; | ||
|
||
if (parentLength !== parentNameLength) { | ||
throw new Error('Parents key and displayName arrays must have the same length.'); | ||
} | ||
|
||
const parentType = containerType === ContainerType.Unit | ||
? 'subsection' | ||
: 'section'; | ||
|
||
if (parentLength === 0 || !parents) { | ||
// Adding empty breadcrumb to add the last `>` spacer. | ||
links.push({ | ||
label: '', | ||
to: '', | ||
containerType, | ||
}); | ||
} else if (parentLength === 1) { | ||
links.push({ | ||
label: parents.displayName?.[0] || '', | ||
to: `/library/${libraryId}/${parentType}/${parents.key?.[0]}`, | ||
containerType, | ||
}); | ||
} else { | ||
// Add all parents as a single object containing list of links | ||
// This is converted to overflow menu by OverflowLinks component | ||
links.push({ | ||
label: parents.displayName || [], | ||
to: parents.key?.map((parentKey) => `/library/${libraryId}/${parentType}/${parentKey}`) || [], | ||
containerType, | ||
}); | ||
} | ||
|
||
return ( | ||
<Breadcrumb | ||
ariaLabel={intl.formatMessage(messages.breadcrumbsAriaLabel)} | ||
links={links} | ||
linkAs={OverflowLinks} | ||
/> | ||
); | ||
}; |
21 changes: 21 additions & 0 deletions
21
src/library-authoring/generic/parent-breadcrumbs/messages.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,21 @@ | ||
import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
||
const messages = defineMessages({ | ||
breadcrumbsAriaLabel: { | ||
id: 'course-authoring.library-authoring.parent-breadcrumbs.label.text', | ||
defaultMessage: 'Navigation breadcrumbs', | ||
description: 'Aria label for navigation breadcrumbs', | ||
}, | ||
breadcrumbsSectionsDropdown: { | ||
id: 'course-authoring.library-authoring.parent-breadcrumbs.dropdown.sections', | ||
defaultMessage: 'Sections', | ||
description: 'Title for dropdown menu containing sections', | ||
}, | ||
breadcrumbsSubsectionsDropdown: { | ||
id: 'course-authoring.library-authoring.parent-breadcrumbs.dropdown.subsections', | ||
defaultMessage: 'Subsections', | ||
description: 'Title for dropdown menu containing subsections', | ||
}, | ||
}); | ||
|
||
export default messages; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.