Skip to content

Commit 67e35fb

Browse files
authored
fix(Toolbar): recalculate overflow elements on children change (#4383)
Fixes #4357
1 parent 67648b0 commit 67e35fb

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

packages/main/src/components/Toolbar/Toolbar.cy.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
ToolbarSeparator,
1414
ToolbarSpacer,
1515
ToolbarStyle,
16-
OverflowToolbarToggleButton
16+
OverflowToolbarToggleButton,
17+
ToolbarPropTypes
1718
} from '../..';
1819
import { ButtonDesign, ToolbarDesign } from '../../enums';
1920
import { cssVarToRgb, cypressPassThroughTestsFactory, mountWithCustomTagName } from '@/cypress/support/utils';
@@ -490,6 +491,49 @@ describe('Toolbar', () => {
490491
cy.findByTestId('tb').should('have.css', 'boxShadow', 'rgb(0, 112, 242) 0px 0px 0px 2px inset');
491492
});
492493

494+
it('recalc on children change', () => {
495+
const TestComp = (props: ToolbarPropTypes) => {
496+
const [actions, setActions] = useState([]);
497+
return (
498+
<>
499+
<button
500+
onClick={() => {
501+
setActions([
502+
<Button key="0">Button</Button>,
503+
<Button key="1">Button</Button>,
504+
<Button key="2">Button</Button>,
505+
<Button key="3">Button</Button>,
506+
<Button key="4">Button</Button>,
507+
<Button key="5">Button</Button>,
508+
<Button key="6">Button</Button>,
509+
<Button key="7">Button</Button>
510+
]);
511+
}}
512+
>
513+
add
514+
</button>
515+
<button
516+
onClick={() => {
517+
setActions([]);
518+
}}
519+
>
520+
remove
521+
</button>
522+
<Toolbar {...props}>{actions}</Toolbar>
523+
</>
524+
);
525+
};
526+
const overflowChange = cy.spy().as('overflowChange');
527+
cy.mount(<TestComp onOverflowChange={overflowChange} style={{ width: '200px' }} />);
528+
529+
cy.get('[ui5-toggle-button]').should('not.exist');
530+
cy.findByText('add').click();
531+
cy.get('[ui5-toggle-button]').should('be.visible');
532+
cy.findByText('remove').click();
533+
cy.get('[ui5-toggle-button]').should('not.exist');
534+
cy.get('@overflowChange').should('have.been.calledOnce');
535+
});
536+
493537
mountWithCustomTagName(Toolbar);
494538
cypressPassThroughTestsFactory(Toolbar);
495539
});

packages/main/src/components/Toolbar/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
263263
};
264264
}, [calculateVisibleItems]);
265265

266+
useEffect(() => {
267+
if (Children.count(children) > 0) {
268+
calculateVisibleItems();
269+
}
270+
}, [children]);
271+
266272
useIsomorphicLayoutEffect(() => {
267273
calculateVisibleItems();
268274
}, [calculateVisibleItems]);

0 commit comments

Comments
 (0)