Skip to content

Commit 9f9c9a7

Browse files
committed
fix: fix activeKay props issue.
1 parent 886cc84 commit 9f9c9a7

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

core/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ function App() {
145145
};
146146
const closeHandle = (item, evn) => {
147147
evn.stopPropagation();
148+
const idx = data.findIndex((m) => m.id === item.id);
149+
150+
let active = '';
151+
if (idx > -1 && activeKey) {
152+
active = data[idx - 1] ? data[idx - 1].id : data[idx].id;
153+
setActiveKey(active || '');
154+
}
148155
setData(data.filter((m) => m.id !== item.id));
149156
};
150157
const addHandle = () => {
@@ -161,6 +168,7 @@ function App() {
161168
<Fragment>
162169
<button onClick={addHandle}>Add{count}</button>
163170
<TabWarp
171+
activeKey={activeKey}
164172
style={{ gap: 3, overflow: 'auto' }}
165173
onTabClick={(id, evn) => tabClick(id, evn)}
166174
onTabDrop={(id, index) => tabDrop(id, index)}
@@ -256,7 +264,7 @@ function App() {
256264
return (
257265
<Fragment>
258266
<button onClick={addHandle}>Add{count}</button>
259-
<TabWarp style={{}} onTabClick={(id, evn) => tabClick(id, evn)} onTabDrop={(id, index) => tabDrop(id, index)}>
267+
<TabWarp onTabClick={(id, evn) => tabClick(id, evn)} onTabDrop={(id, index) => tabDrop(id, index)}>
260268
{data.map((m, idx) => {
261269
return (
262270
<TabItem key={idx} id={m.id}>

core/src/Tabs.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, isValidElement, PropsWithChildren, useLayoutEffect } from 'react';
1+
import React, { FC, isValidElement, PropsWithChildren, useEffect, useLayoutEffect } from 'react';
22
import { useDrop } from 'react-dnd';
33
import { useDataContext, InitialState } from './store';
44
import { ItemTypes } from './Tab';
@@ -10,6 +10,8 @@ export const Tabs: FC<PropsWithChildren<TabsProps>> = ({ children, activeKey, ..
1010
accept: ItemTypes.Tab,
1111
}));
1212

13+
useEffect(() => dispatch!({ activeKey }), [activeKey]);
14+
1315
useLayoutEffect(() => {
1416
if (children) {
1517
const data: InitialState['data'] = [];

core/src/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@ export interface TabsProps extends React.DetailedHTMLProps<React.HTMLAttributes<
1717
onTabDrop?: (id: string, index?: number) => void;
1818
}
1919

20-
const TabContainer: FC<PropsWithChildren<TabsProps>> = ({ activeKey: keyId, onTabClick, onTabDrop, ...props }) => {
20+
const TabContainer: FC<PropsWithChildren<TabsProps>> = ({ activeKey, onTabClick, onTabDrop, ...props }) => {
2121
const tabClick = useEventCallback(onTabClick!);
2222
const tabDrop = useEventCallback(onTabDrop!);
23-
const [activeKey, setActiveKey] = useState(keyId);
24-
25-
useEffect(() => setActiveKey(keyId), [keyId]);
2623

2724
return (
2825
<DndProvider backend={HTML5Backend}>
2926
<Provider init={{ data: [], activeKey, onTabClick: tabClick, onTabDrop: tabDrop }}>
30-
<Tabs {...props} />
27+
<Tabs {...props} activeKey={activeKey} />
3128
</Provider>
3229
</DndProvider>
3330
);

0 commit comments

Comments
 (0)