Skip to content

Commit

Permalink
fix: DropdownContainer items width calculation (#22371)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Dec 8, 2022
1 parent f64423a commit 1edfd7e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions superset-frontend/src/components/DropdownContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,25 @@ const DropdownContainer = forwardRef(
[items, overflowingIndex],
);

useEffect(() => {
if (itemsWidth.length !== items.length) {
const container = current?.children.item(0);
if (container) {
const { children } = container;
const childrenArray = Array.from(children);
setItemsWidth(
childrenArray.map(child => child.getBoundingClientRect().width),
);
}
}
}, [current?.children, items.length, itemsWidth.length]);

useLayoutEffect(() => {
const container = current?.children.item(0);
if (container) {
const { children } = container;
const childrenArray = Array.from(children);

// If items length change, add all items to the container
// and recalculate the widths
if (itemsWidth.length !== items.length) {
if (childrenArray.length === items.length) {
setItemsWidth(
childrenArray.map(child => child.getBoundingClientRect().width),
);
} else {
setOverflowingIndex(-1);
return;
}
}

// Calculates the index of the first overflowed element
// +1 is to give at least one pixel of difference and avoid flakiness
const index = childrenArray.findIndex(
Expand Down

0 comments on commit 1edfd7e

Please sign in to comment.