Skip to content

Commit 0bba35a

Browse files
nazimamindannifyLFDanLu
authored
Breadcrumbs fails to render with single child and showRoot (#2927)
* fixes(#2923): breadcrumbs fails to render with single child and showRoot=true * fixes(#2923): breadcrumbs fails to render with single child and showRoot=true * fixes(#2923): breadcrumbs fails to render with single child and showRoot=true -- added tests Co-authored-by: Danni <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent 8978003 commit 0bba35a

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

packages/@react-spectrum/breadcrumbs/src/Breadcrumbs.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ function Breadcrumbs<T>(props: SpectrumBreadcrumbsProps<T>, ref: DOMRef) {
8888
listItems.pop();
8989
newVisibleItems++;
9090
} else {
91-
// Ensure the last breadcrumb isn't truncated when we measure it.
92-
let last = listItems.pop();
93-
last.style.overflow = 'visible';
91+
if (listItems.length > 0) {
92+
// Ensure the last breadcrumb isn't truncated when we measure it.
93+
let last = listItems.pop();
94+
last.style.overflow = 'visible';
9495

95-
calculatedWidth += last.offsetWidth;
96-
if (calculatedWidth < containerWidth) {
97-
newVisibleItems++;
98-
}
96+
calculatedWidth += last.offsetWidth;
97+
if (calculatedWidth < containerWidth) {
98+
newVisibleItems++;
99+
}
99100

100-
last.style.overflow = '';
101+
last.style.overflow = '';
102+
}
101103
}
102104

103105
for (let breadcrumb of listItems.reverse()) {

packages/@react-spectrum/breadcrumbs/stories/Breadcrumbs.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ storiesOf('Breadcrumbs', module)
136136
<Item>Root</Item>
137137
</Breadcrumbs>
138138
)
139+
).add(
140+
'Only one item, showRoot',
141+
() => (
142+
<Breadcrumbs showRoot>
143+
<Item key="Root-1">Root</Item>
144+
</Breadcrumbs>
145+
)
139146
);
140147

141148
function render(props = {}) {

packages/@react-spectrum/breadcrumbs/test/Breadcrumbs.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('Breadcrumbs', function () {
8080
expect(item3).toHaveAttribute('aria-current', 'page');
8181
});
8282

83+
it('Handles single item and showRoot', () => {
84+
let {getByText} = render(
85+
<Breadcrumbs showRoot>
86+
<Item key="Folder-1">Folder 1</Item>
87+
</Breadcrumbs>
88+
);
89+
let item = getByText('Folder 1');
90+
expect(item).toBeTruthy();
91+
expect(item.tabIndex).toBe(-1);
92+
});
93+
8394
it('Should handle forward ref', function () {
8495
let ref;
8596
let Component = () => {

0 commit comments

Comments
 (0)