From 4ec921888511c598ef48262b3a8704a66e6ae29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Mon, 19 Dec 2022 15:47:31 +0800 Subject: [PATCH] fix: Tabs inkBar not show in StrictMode (#628) * fix: strictMode can get correct offset * chore: merge master * test: update snapshot --- src/TabNavList/TabNode.tsx | 42 +++++++++++-------------- src/TabNavList/index.tsx | 41 +++++++++++------------- src/hooks/useRefs.ts | 22 ------------- tests/__snapshots__/index.test.tsx.snap | 4 +++ tests/index.test.tsx | 30 +++++++++++++++++- 5 files changed, 69 insertions(+), 70 deletions(-) delete mode 100644 src/hooks/useRefs.ts 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([