Skip to content

fix(Toolbar): return correct value for overflowElements in onOverflowChange #4610

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
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions packages/main/src/components/Toolbar/OverflowPopover.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import iconOverflow from '@ui5/webcomponents-icons/dist/overflow.js';
import { Device, useSyncRef } from '@ui5/webcomponents-react-base';
import { clsx } from 'clsx';
import type { FC, ReactElement, ReactNode, Ref } from 'react';
import type { Dispatch, FC, ReactElement, ReactNode, Ref, SetStateAction } from 'react';
import React, { cloneElement, useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { ButtonDesign, PopoverPlacementType } from '../../enums/index.js';
Expand All @@ -27,6 +27,7 @@ interface OverflowPopoverProps {
showMoreText: string;
overflowPopoverRef?: Ref<PopoverDomRef>;
overflowButton?: ReactElement<ToggleButtonPropTypes> | ReactElement<ButtonPropTypes>;
setIsMounted: Dispatch<SetStateAction<boolean>>;
}

const isPhone = Device.isPhone();
Expand All @@ -40,13 +41,21 @@ export const OverflowPopover: FC<OverflowPopoverProps> = (props: OverflowPopover
overflowContentRef,
numberOfAlwaysVisibleItems,
showMoreText,
overflowButton,
overflowPopoverRef,
overflowButton
setIsMounted
} = props;
const [pressed, setPressed] = useState(false);
const toggleBtnRef = useRef<ToggleButtonDomRef>(null);
const [componentRef, popoverRef] = useSyncRef(overflowPopoverRef);

useEffect(() => {
setIsMounted(true);
return () => {
setIsMounted(false);
};
}, []);

const handleToggleButtonClick = (e) => {
e.stopPropagation();
setPressed((prev) => {
Expand Down
33 changes: 32 additions & 1 deletion packages/main/src/components/Toolbar/Toolbar.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ const OverflowTestComponent = (props: PropTypes) => {
const { onOverflowChange } = props;
const [width, setWidth] = useState(undefined);
const [additionalChildren, setAdditionalChildren] = useState([]);
const [eventProperties, setEventProperties] = useState({
toolbarElements: [],
overflowElements: undefined,
target: undefined
});

const handleOverflowChange = (e) => {
onOverflowChange(e);
setEventProperties(e);
};
return (
<>
<Input
Expand Down Expand Up @@ -62,7 +72,7 @@ const OverflowTestComponent = (props: PropTypes) => {
</Button>
<Toolbar
data-testid="toolbar"
onOverflowChange={onOverflowChange}
onOverflowChange={handleOverflowChange}
style={width ? { width: `${width}px`, maxWidth: 'none' } : undefined}
>
<Text data-testid="toolbar-item" style={{ width: '200px' }}>
Expand All @@ -79,6 +89,9 @@ const OverflowTestComponent = (props: PropTypes) => {
{additionalChildren}
<ToolbarSeparator data-testid="separator" />
</Toolbar>
<br />
toolbarElements: <span data-testid="toolbarElements">{eventProperties.toolbarElements.length}</span>
overflowElements: <span data-testid="overflowElements">{eventProperties.overflowElements?.length}</span>
</>
);
};
Expand Down Expand Up @@ -128,6 +141,8 @@ describe('Toolbar', () => {
cy.viewport(300, 500);
cy.mount(<OverflowTestComponent onOverflowChange={onOverflowChange} />);
cy.get('@overflowChangeSpy').should('have.been.calledOnce');
cy.findByTestId('toolbarElements').should('have.text', 2);
cy.findByTestId('overflowElements').should('have.text', 4);
cy.findByText('Item1').should('be.visible');
cy.get('[data-testid="toolbar-item2"]').should('not.be.visible');
cy.get('[data-testid="toolbar-item3"]').should('not.be.visible');
Expand All @@ -146,12 +161,16 @@ describe('Toolbar', () => {
cy.get('[ui5-popover]').should('not.have.attr', 'open');

cy.get('@overflowChangeSpy').should('have.callCount', 2);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 3);

cy.findByTestId('input').shadow().find('input').type('100');
cy.findByTestId('input').trigger('change');
cy.findByTestId('input').shadow().find('input').clear({ force: true });

cy.get('@overflowChangeSpy').should('have.callCount', 3);
cy.findByTestId('toolbarElements').should('have.text', 0);
cy.findByTestId('overflowElements').should('have.text', 6);

cy.get('[data-testid="toolbar-item"]').should('not.be.visible');
cy.get('[data-testid="toolbar-item2"]').should('not.be.visible');
Expand All @@ -161,6 +180,8 @@ describe('Toolbar', () => {
cy.findByTestId('input').trigger('change');

cy.get('@overflowChangeSpy').should('have.callCount', 4);
cy.findByTestId('toolbarElements').should('have.text', 6);
cy.findByTestId('overflowElements').should('not.have.text');

cy.get('[data-testid="toolbar-item"]').should('be.visible');
cy.get('[data-testid="toolbar-item2"]').should('be.visible');
Expand All @@ -170,10 +191,14 @@ describe('Toolbar', () => {
cy.findByTestId('input').trigger('change');

cy.get('@overflowChangeSpy').should('have.callCount', 5);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 3);

cy.findByText('Add').click();

cy.get('@overflowChangeSpy').should('have.callCount', 6);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 4);

cy.findByText('Add').click();
cy.findByText('Add').click();
Expand All @@ -182,10 +207,14 @@ describe('Toolbar', () => {
cy.findByText('Add').click();

cy.get('@overflowChangeSpy').should('have.callCount', 11);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 9);

cy.findByText('Remove').click();

cy.get('@overflowChangeSpy').should('have.callCount', 12);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 8);

cy.findByText('Remove').click();
cy.findByText('Remove').click();
Expand All @@ -194,6 +223,8 @@ describe('Toolbar', () => {
cy.findByText('Remove').click();

cy.get('@overflowChangeSpy').should('have.callCount', 17);
cy.findByTestId('toolbarElements').should('have.text', 3);
cy.findByTestId('overflowElements').should('have.text', 3);

cy.get(`[ui5-toggle-button]`).click();

Expand Down
15 changes: 10 additions & 5 deletions packages/main/src/components/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface ToolbarPropTypes extends Omit<CommonProps, 'onClick' | 'childre
*/
onOverflowChange?: (event: {
toolbarElements: HTMLElement[];
overflowElements: HTMLCollection;
overflowElements: HTMLCollection | undefined;
target: HTMLElement;
}) => void;
}
Expand All @@ -109,7 +109,7 @@ const OVERFLOW_BUTTON_WIDTH = 36 + 8 + 8; // width + padding end + spacing start
* The content of the `Toolbar` moves into the overflow area from right to left when the available space is not enough in the visible area of the container.
* It can be accessed by the user through the overflow button that opens it in a popover.
*
* __Note:__ The overflow popover is mounted only when opened, i.e., any child component of the popover will be remounted, when moved into it.
* __Note:__ The overflow popover is mounted only when the overflow button is displayed, i.e., any child component of the popover will be remounted, when moved into it.
*/
const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
const {
Expand All @@ -134,6 +134,7 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
const [componentRef, outerContainer] = useSyncRef<HTMLDivElement>(ref);
const controlMetaData = useRef([]);
const [lastVisibleIndex, setLastVisibleIndex] = useState<number>(null);
const [isPopoverMounted, setIsPopoverMounted] = useState(false);
const contentRef = useRef(null);
const overflowContentRef = useRef(null);
const overflowBtnRef = useRef(null);
Expand Down Expand Up @@ -291,11 +292,14 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {

useEffect(() => {
const haveChildrenChanged = prevChildren.current.length !== flatChildren.length;
if ((lastVisibleIndex !== null || haveChildrenChanged) && typeof onOverflowChange === 'function') {
if ((lastVisibleIndex !== null || haveChildrenChanged) && typeof debouncedOverflowChange.current === 'function') {
prevChildren.current = flatChildren;
const toolbarChildren = contentRef.current?.children;
let toolbarElements = [];
const overflowElements = overflowContentRef.current?.children;
let overflowElements;
if (isPopoverMounted) {
overflowElements = overflowContentRef.current?.children;
}
if (toolbarChildren?.length > 0) {
toolbarElements = Array.from(toolbarChildren).filter((item, index) => index <= lastVisibleIndex);
}
Expand All @@ -308,7 +312,7 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
return () => {
debouncedOverflowChange.current.cancel();
};
}, [lastVisibleIndex, flatChildren, debouncedOverflowChange]);
}, [lastVisibleIndex, flatChildren.length, isPopoverMounted]);

const CustomTag = as as ElementType;
const styleWithMinWidth = minWidth !== '0' ? { minWidth, ...style } : style;
Expand Down Expand Up @@ -352,6 +356,7 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
numberOfAlwaysVisibleItems={numberOfAlwaysVisibleItems}
showMoreText={showMoreText}
overflowButton={overflowButton}
setIsMounted={setIsPopoverMounted}
>
{flatChildren}
</OverflowPopover>
Expand Down