Skip to content

Commit

Permalink
fix: more dropdown should not contain last tab if scrolled (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc authored Mar 2, 2022
1 parent 3aeba5a commit 729383f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
12 changes: 2 additions & 10 deletions src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {

const [wrapperScrollWidth, setWrapperScrollWidth] = useState<number>(0);
const [wrapperScrollHeight, setWrapperScrollHeight] = useState<number>(0);
const [wrapperContentWidth, setWrapperContentWidth] = useState<number>(0);
const [wrapperContentHeight, setWrapperContentHeight] = useState<number>(0);
const [wrapperWidth, setWrapperWidth] = useState<number>(null);
const [wrapperHeight, setWrapperHeight] = useState<number>(null);
const [addWidth, setAddWidth] = useState<number>(0);
Expand Down Expand Up @@ -265,8 +263,8 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
top: transformTop,
},
{
width: wrapperContentWidth,
height: wrapperContentHeight,
width: wrapperScrollWidth,
height: wrapperScrollHeight,
},
{
width: addWidth,
Expand Down Expand Up @@ -326,8 +324,6 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
const offsetHeight = tabsWrapperRef.current?.offsetHeight || 0;
const newAddWidth = innerAddButtonRef.current?.offsetWidth || 0;
const newAddHeight = innerAddButtonRef.current?.offsetHeight || 0;
const newOperationWidth = operationsRef.current?.offsetWidth || 0;
const newOperationHeight = operationsRef.current?.offsetHeight || 0;

setWrapperWidth(offsetWidth);
setWrapperHeight(offsetHeight);
Expand All @@ -340,10 +336,6 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
setWrapperScrollWidth(newWrapperScrollWidth);
setWrapperScrollHeight(newWrapperScrollHeight);

const isOperationHidden = operationsRef.current?.className.includes(operationsHiddenClassName);
setWrapperContentWidth(newWrapperScrollWidth - (isOperationHidden ? 0 : newOperationWidth));
setWrapperContentHeight(newWrapperScrollHeight - (isOperationHidden ? 0 : newOperationHeight));

// Update buttons records
setTabSizes(() => {
const newSizes: TabSizeMap = new Map();
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useVisibleRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function useVisibleRange(
const addSize = addNodeSize[unit];

let mergedBasicSize = basicSize;
if (tabContentSize + addSize > basicSize) {
if (tabContentSize + addSize > basicSize && tabContentSize < basicSize) {
mergedBasicSize = basicSize - addSize;
}

Expand Down
13 changes: 5 additions & 8 deletions tests/common/util.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-invalid-this */
import React from 'react';
import { ReactWrapper } from 'enzyme';
import type { ReactWrapper } from 'enzyme';
import Tabs, { TabPane } from '../../src';
import { TabsProps } from '../../src/Tabs';
import type { TabsProps } from '../../src/Tabs';

export function getOffsetSizeFunc(
info: {
Expand All @@ -18,7 +19,7 @@ export function getOffsetSizeFunc(
return 20;
}
if (this.className.includes('rc-tabs-nav-list')) {
return info.list || 5 * 20;
return info.list || 5 * 20 + 10;
}
if (this.className.includes('rc-tabs-nav-wrap')) {
return info.wrapper || 40;
Expand Down Expand Up @@ -85,9 +86,5 @@ export function getTabs(props: TabsProps = null) {
}

export function triggerResize(wrapper: ReactWrapper) {
(wrapper
.find('.rc-tabs-nav')
.find('ResizeObserver')
.first()
.props() as any).onResize();
(wrapper.find('.rc-tabs-nav').find('ResizeObserver').first().props() as any).onResize();
}
38 changes: 29 additions & 9 deletions tests/overflow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { mount, ReactWrapper } from 'enzyme';
import type { ReactWrapper } from 'enzyme';
import { mount } from 'enzyme';
import KeyCode from 'rc-util/lib/KeyCode';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { act } from 'react-dom/test-utils';
Expand All @@ -25,7 +26,7 @@ describe('Tabs.Overflow', () => {
function btnOffsetPosition() {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const btn = this as HTMLButtonElement;
const btnList = Array.from(btn.parentNode.childNodes).filter((ele) =>
const btnList = Array.from(btn.parentNode.childNodes).filter(ele =>
(ele as HTMLElement).className.includes('rc-tabs-tab'),
);
const index = btnList.indexOf(btn);
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('Tabs.Overflow', () => {
jest.useRealTimers();
});

[KeyCode.SPACE, KeyCode.ENTER].forEach((code) => {
[KeyCode.SPACE, KeyCode.ENTER].forEach(code => {
it(`keyboard with select keycode: ${code}`, () => {
jest.useFakeTimers();
const onChange = jest.fn();
Expand Down Expand Up @@ -171,7 +172,7 @@ describe('Tabs.Overflow', () => {
});

// Wheel to move
const node = (wrapper.find('.rc-tabs-nav-wrap').instance() as unknown) as HTMLElement;
const node = wrapper.find('.rc-tabs-nav-wrap').instance() as unknown as HTMLElement;

act(() => {
const touchStart = new WheelEvent('wheel', {
Expand Down Expand Up @@ -218,7 +219,7 @@ describe('Tabs.Overflow', () => {
});

// Wheel to move
const node = (wrapper.find('.rc-tabs-nav-wrap').instance() as unknown) as HTMLElement;
const node = wrapper.find('.rc-tabs-nav-wrap').instance() as unknown as HTMLElement;
const touchStart = new WheelEvent('wheel', {
deltaX: 20,
deltaY: 20,
Expand Down Expand Up @@ -322,7 +323,7 @@ describe('Tabs.Overflow', () => {

list.forEach(({ name, trigger }) => {
it(`remove by ${name} in dropdown menu`, () => {
console.log('run here')
console.log('run here');
jest.useFakeTimers();
const onEdit = jest.fn();
const wrapper = mount(getTabs({ editable: { onEdit } }));
Expand All @@ -343,15 +344,34 @@ describe('Tabs.Overflow', () => {

// Should be button to enable press SPACE key to trigger
expect(first.instance() instanceof HTMLButtonElement).toBeTruthy();

expect(onEdit).toHaveBeenCalledWith('remove', {
key: 'bamboo',
key: 'cute',
event: expect.anything(),
});

wrapper.unmount();
jest.useRealTimers();
});
});
})
});

it('should calculate hidden tabs correctly', () => {
jest.useFakeTimers();
const onEdit = jest.fn();
const wrapper = mount(getTabs({ editable: { onEdit }, activeKey: 'miu' }));

triggerResize(wrapper);
act(() => {
jest.runAllTimers();
wrapper.update();
});

wrapper.find('.rc-tabs-nav-more').simulate('mouseenter');
act(() => {
jest.runAllTimers();
wrapper.update();
});
expect(wrapper.find('.rc-tabs-dropdown-menu').first().text()).not.toContain('miu');
});
});

1 comment on commit 729383f

@vercel
Copy link

@vercel vercel bot commented on 729383f Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.