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: quotes in key crashed #656

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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'}]} />)
})
});