Skip to content

Commit

Permalink
Merge 8f9b2e5 into 69dd40f
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc authored Mar 9, 2023
2 parents 69dd40f + 8f9b2e5 commit 0f7f5b2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/TabNavList/TabNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import * as React from 'react';
import type { EditableConfig, Tab } from '../interface';
import { genDataNodeKey } from '../util';

export interface TabNodeProps {
id: string;
Expand Down Expand Up @@ -56,7 +57,7 @@ function TabNode({
<div
key={key}
// ref={ref}
data-node-key={key}
data-node-key={genDataNodeKey(key)}
className={classNames(tabPrefix, {
[`${tabPrefix}-with-remove`]: removable,
[`${tabPrefix}-active`]: active,
Expand Down
4 changes: 2 additions & 2 deletions src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
TabsLocale,
} from '../interface';
import TabContext from '../TabContext';
import { stringify } from '../util';
import { genDataNodeKey, stringify } from '../util';
import AddButton from './AddButton';
import ExtraContent from './ExtraContent';
import OperationNode from './OperationNode';
Expand Down Expand Up @@ -311,7 +311,7 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
setTabSizes(() => {
const newSizes: TabSizeMap = new Map();
tabs.forEach(({ key }) => {
const btnNode = tabListRef.current?.querySelector<HTMLElement>(`[data-node-key="${key}"]`);
const btnNode = tabListRef.current?.querySelector<HTMLElement>(`[data-node-key="${genDataNodeKey(key)}"]`);
if (btnNode) {
newSizes.set(key, {
width: btnNode.offsetWidth,
Expand Down
7 changes: 7 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';

/**
* We trade Map as deps which may change with same value but different ref object.
* We should make it as hash for deps
Expand All @@ -16,3 +18,8 @@ export function stringify<K extends string | number | symbol, V>(obj: Record<K,

return JSON.stringify(tgt);
}

const RC_TABS_DOUBLE_QUOTE = 'TABS_DQ';
export function genDataNodeKey(key: React.Key): string {
return String(key).replace(/"/g, RC_TABS_DOUBLE_QUOTE);
}
8 changes: 8 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,12 @@ describe('Tabs.Basic', () => {
const { container } = render(getTabs({ tabBarStyle: { background: 'red' } }));
expect(container.querySelector('.rc-tabs-nav')).toHaveStyle({ background: 'red' });
});

it('key contains double quote should not crash', () => {
render(<Tabs items={[{key: '"key"', label: 'test'}]} />)
});

it('key could be number', () => {
render(<Tabs items={[{key: 1 as any, label: 'test'}]} />)
})
});

0 comments on commit 0f7f5b2

Please sign in to comment.