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: DropdownContainer resize algorithm #22318

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { useEffect, useState } from 'react';
import React, { useCallback, useState } from 'react';
import { isEqual } from 'lodash';
import { css } from '@superset-ui/core';
import Select from '../Select/Select';
Expand Down Expand Up @@ -63,10 +63,15 @@ export const Component = (props: DropdownContainerProps) => {
const [items, setItems] = useState<ItemsType>([]);
const [overflowingState, setOverflowingState] = useState<OverflowingState>();
const containerRef = React.useRef<Ref>(null);

useEffect(() => {
setItems(generateItems(overflowingState));
}, [overflowingState]);
const onOverflowingStateChange = useCallback(
value => {
if (!isEqual(overflowingState, value)) {
setItems(generateItems(value));
setOverflowingState(value);
}
},
[overflowingState],
);

return (
<div>
Expand All @@ -86,11 +91,7 @@ export const Component = (props: DropdownContainerProps) => {
<DropdownContainer
{...props}
items={items}
onOverflowingStateChange={value => {
if (!isEqual(overflowingState, value)) {
setOverflowingState(value);
}
}}
onOverflowingStateChange={onOverflowingStateChange}
ref={containerRef}
/>
</div>
Expand Down
31 changes: 20 additions & 11 deletions superset-frontend/src/components/DropdownContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,42 @@ const DropdownContainer = forwardRef(
const childrenArray = Array.from(children);

// Stores items width once
if (itemsWidth.length === 0) {
if (itemsWidth.length === 0 && childrenArray.length > 0) {
setItemsWidth(
childrenArray.map(child => child.getBoundingClientRect().width),
);
}

// 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(
child =>
child.getBoundingClientRect().right >
container.getBoundingClientRect().right,
container.getBoundingClientRect().right + 1,
);
setOverflowingIndex(index === -1 ? children.length : index);

if (width > previousWidth && overflowingIndex !== -1) {
let newOverflowingIndex = index === -1 ? children.length : index;

if (width > previousWidth && newOverflowingIndex !== -1) {
// Calculates remaining space in the container
const button = current?.children.item(1);
const buttonRight = button?.getBoundingClientRect().right || 0;
const containerRight = current?.getBoundingClientRect().right || 0;
const remainingSpace = containerRight - buttonRight;
// Checks if the first element in the dropdown fits in the remaining space
const fitsInRemainingSpace = remainingSpace >= itemsWidth[0];
if (fitsInRemainingSpace && overflowingIndex < items.length) {
// Moves element from dropdown to container
setOverflowingIndex(overflowingIndex + 1);

// Checks if some elements in the dropdown fits in the remaining space
let sum = 0;
for (let i = newOverflowingIndex; i < items.length; i += 1) {
sum += itemsWidth[i];
if (sum <= remainingSpace) {
newOverflowingIndex = i + 1;
} else {
break;
}
}
}

setOverflowingIndex(newOverflowingIndex);
}
}, [
current,
Expand Down Expand Up @@ -206,7 +215,7 @@ const DropdownContainer = forwardRef(
const [overflowedItems, overflowedIds] = useMemo(
() =>
overflowingIndex !== -1
? reduceItems(items.slice(overflowingIndex, items.length))
? reduceItems(items.slice(overflowingIndex))
: [[], []],
[items, overflowingIndex],
);
Expand Down Expand Up @@ -296,7 +305,7 @@ const DropdownContainer = forwardRef(
align-items: center;
gap: ${theme.gridUnit * 4}px;
margin-right: ${theme.gridUnit * 3}px;
min-width: 100px;
min-width: 0px;
`}
data-test="container"
style={style}
Expand Down