diff --git a/src/TabNavList/TabNode.tsx b/src/TabNavList/TabNode.tsx index a8a2ade7..932b32a1 100644 --- a/src/TabNavList/TabNode.tsx +++ b/src/TabNavList/TabNode.tsx @@ -1,7 +1,7 @@ -import * as React from 'react'; import classNames from 'classnames'; import KeyCode from 'rc-util/lib/KeyCode'; -import type { Tab, EditableConfig } from '../interface'; +import * as React from 'react'; +import type { EditableConfig, Tab } from '../interface'; export interface TabNodeProps { id: string; @@ -15,32 +15,25 @@ export interface TabNodeProps { renderWrapper?: (node: React.ReactElement) => React.ReactElement; removeAriaLabel?: string; removeIcon?: React.ReactNode; - onRemove: () => void; onFocus: React.FocusEventHandler; style?: React.CSSProperties; } -function TabNode( - { - prefixCls, - id, - active, - tab: { key, label, disabled, closeIcon }, - closable, - renderWrapper, - removeAriaLabel, - editable, - onClick, - onRemove, - onFocus, - style, - }: TabNodeProps, - ref: React.Ref, -) { +function TabNode({ + prefixCls, + id, + active, + tab: { key, label, disabled, closeIcon }, + closable, + renderWrapper, + removeAriaLabel, + editable, + onClick, + onFocus, + style, +}: TabNodeProps) { const tabPrefix = `${prefixCls}-tab`; - React.useEffect(() => onRemove, []); - const removable = editable && closable !== false && !disabled; function onInternalClick(e: React.MouseEvent | React.KeyboardEvent) { @@ -62,7 +55,8 @@ function TabNode( const node: React.ReactElement = (
) { const tabListRef = useRef(); const operationsRef = useRef(); const innerAddButtonRef = useRef(); - const [getBtnRef, removeBtnRef] = useRefs(); + // const [getBtnRef, removeBtnRef] = useRefs(); const tabPositionTopOrBottom = tabPosition === 'top' || tabPosition === 'bottom'; @@ -287,13 +286,9 @@ function TabNavList(props: TabNavListProps, ref: React.Ref) { active={key === activeKey} renderWrapper={children} removeAriaLabel={locale?.removeAriaLabel} - ref={getBtnRef(key)} onClick={e => { onTabClick(key, e); }} - onRemove={() => { - removeBtnRef(key); - }} onFocus={() => { scrollToTab(key); doLockAnimation(); @@ -315,7 +310,7 @@ function TabNavList(props: TabNavListProps, ref: React.Ref) { setTabSizes(() => { const newSizes: TabSizeMap = new Map(); tabs.forEach(({ key }) => { - const btnNode = getBtnRef(key).current; + const btnNode = tabListRef.current.querySelector(`[data-node-key="${key}"]`); if (btnNode) { newSizes.set(key, { width: btnNode.offsetWidth, diff --git a/src/hooks/useRefs.ts b/src/hooks/useRefs.ts deleted file mode 100644 index ec573300..00000000 --- a/src/hooks/useRefs.ts +++ /dev/null @@ -1,22 +0,0 @@ -import * as React from 'react'; -import { useRef } from 'react'; - -export default function useRefs(): [ - (key: React.Key) => React.RefObject, - (key: React.Key) => void, -] { - const cacheRefs = useRef(new Map>()); - - function getRef(key: React.Key) { - if (!cacheRefs.current.has(key)) { - cacheRefs.current.set(key, React.createRef()); - } - return cacheRefs.current.get(key); - } - - function removeRef(key: React.Key) { - cacheRefs.current.delete(key); - } - - return [getRef, removeRef]; -} diff --git a/tests/__snapshots__/index.test.tsx.snap b/tests/__snapshots__/index.test.tsx.snap index 9fcec63a..737aeeac 100644 --- a/tests/__snapshots__/index.test.tsx.snap +++ b/tests/__snapshots__/index.test.tsx.snap @@ -17,6 +17,7 @@ exports[`Tabs.Basic Normal 1`] = ` >
{ render(getTabs({ items: null })); }); + it('strictMode should show correct ink bar', () => { + jest.useFakeTimers(); + + const { container } = render( + + ({ + label: `Tab ${index}`, + key: `${index}`, + children: `Content of Tab Pane ${index}`, + }))} + activeKey="0" + /> + , + ); + + for (let i = 0; i < 10; i += 1) { + act(() => { + jest.runAllTimers(); + }); + } + + expect(container.querySelector('.rc-tabs-ink-bar')).toHaveStyle({ + width: '20px', + }); + jest.useRealTimers(); + }); + it('same width in windows call resize', async () => { const App = () => { const [list, setList] = React.useState([