Skip to content
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

fix ButtonGroup overflowButton logic #1278

Merged
merged 6 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-tips-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': major
---

Adjusted calculations in ButtonGroup's `overflowButton` callback, so that it respects `overflowPlacement` and considers the presence of the overflowButton itself.
7 changes: 4 additions & 3 deletions apps/storybook/src/ButtonGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const Overflow: Story<ButtonGroupProps> = (args) => {
<IconButton
key={index}
onClick={() => action(`Clicked on button ${index + 1}`)()}
label={`Button #${index + 1}`}
>
<SvgPlaceholder />
</IconButton>
Expand All @@ -83,12 +84,12 @@ export const Overflow: Story<ButtonGroupProps> = (args) => {
overflowButton={(overflowStart) => (
<DropdownMenu
menuItems={(close) =>
Array(buttons.length - overflowStart + 1)
Array(buttons.length - overflowStart)
.fill(null)
.map((_, _index) => {
const index = overflowStart + _index;
const onClick = () => {
action(`Clicked button ${index} (overflow)`)();
action(`Clicked button ${index + 1} (overflow)`)();
close();
};
return (
Expand All @@ -97,7 +98,7 @@ export const Overflow: Story<ButtonGroupProps> = (args) => {
onClick={onClick}
icon={<SvgPlaceholder />}
>
Button #{index}
Button #{index + 1}
</MenuItem>
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ it.each(['start', 'end'] as const)(
const buttons = container.querySelectorAll('.iui-button');
expect(buttons).toHaveLength(2);
fireEvent.click(overflowPlacement === 'end' ? buttons[1] : buttons[0]);
expect(mockOnClick).toHaveBeenCalledWith(2);
expect(mockOnClick).toHaveBeenCalledWith(1);

scrollWidthSpy.mockRestore();
offsetWidthSpy.mockRestore();
Expand Down
39 changes: 24 additions & 15 deletions packages/itwinui-react/src/core/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,32 @@ export const ButtonGroup = React.forwardRef<HTMLDivElement, ButtonGroupProps>(
ref={refs}
{...rest}
>
<>
{visibleCount < items.length &&
overflowButton &&
overflowPlacement === 'start' && (
<div>{overflowButton(visibleCount)}</div>
)}
{(() => {
if (!(visibleCount < items.length)) {
return items;
}

{visibleCount < items.length
? items.slice(0, Math.max(0, visibleCount - 1))
: items}
const overflowStart =
overflowPlacement === 'start'
? items.length - visibleCount
: visibleCount - 1;

{visibleCount < items.length &&
overflowButton &&
overflowPlacement === 'end' && (
<div>{overflowButton(visibleCount)}</div>
)}
</>
return (
<>
{overflowButton && overflowPlacement === 'start' && (
<div>{overflowButton(overflowStart)}</div>
)}

{overflowPlacement === 'start'
? items.slice(overflowStart + 1)
: items.slice(0, Math.max(0, overflowStart))}

{overflowButton && overflowPlacement === 'end' && (
<div>{overflowButton(overflowStart)}</div>
)}
</>
);
})()}
</div>
);
},
Expand Down