Skip to content

Commit

Permalink
fix: quote in key should not cause tabs crash (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc authored Mar 9, 2023
1 parent 81f004d commit 18550e0
Show file tree
Hide file tree
Showing 4 changed files with 13 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
5 changes: 5 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,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: string): string {
return key.replace(/"/g, RC_TABS_DOUBLE_QUOTE);
}
4 changes: 4 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,8 @@ 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'}]} />)
})
});

1 comment on commit 18550e0

@vercel
Copy link

@vercel vercel bot commented on 18550e0 Mar 9, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

tabs – ./

tabs-git-master-react-component.vercel.app
tabs-react-component.vercel.app

Please sign in to comment.