Skip to content

Commit 70f85e4

Browse files
committed
feat: refactor closeTab logic
1 parent 5f37731 commit 70f85e4

File tree

4 files changed

+61
-42
lines changed

4 files changed

+61
-42
lines changed

src/components/tabs/index.tsx

+9-12
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,22 @@ export interface ITabsProps {
2525
closable?: boolean;
2626
data: ITab[];
2727
activeTab?: string;
28-
type?: 'line' | 'editable-card';
29-
onCloseTab?: (item?: ITab) => void ;
28+
// TODO 支持Card editable-card 默认 inline
29+
type?: 'line' | 'card' |'editable-card';
30+
onCloseTab?: (key?: string) => void ;
3031
onMoveTab?: (tabs: ITab[]) => void;
3132
onSelectTab?: (event: React.MouseEvent, key?: string) => void;
32-
onTabChange: (index: string) => void;
3333
}
3434

3535
export const tabsClassName = prefixClaName('tabs')
3636
export const tabsHeader = getBEMElement(tabsClassName, 'header')
3737
export const tabsContent = getBEMElement(tabsClassName, 'content')
3838
export const tabsContentItem = getBEMElement(tabsContent, 'item')
39+
export const tabItemCloseClassName = getBEMElement(tabItemClassName, 'close')
3940

4041
const Tabs = (props: ITabsProps) => {
41-
const { closable, data, activeTab: newActiveTab, type = 'line', onCloseTab, onSelectTab } = props;
42-
const [activeTab, setActiveTab] = useState<string | number | void>(newActiveTab)
42+
const { closable, data, activeTab, type = 'line', onCloseTab, onSelectTab } = props;
43+
// const [activeTab, setActiveTab] = useState<string | number | void>(newActiveTab)
4344
const onMoveTab = useCallback(
4445
(dragIndex, hoverIndex) => {
4546
const dragTab = data[dragIndex];
@@ -56,14 +57,11 @@ const Tabs = (props: ITabsProps) => {
5657
);
5758

5859
const onTabClick = (event: React.MouseEvent, key?: string) => {
59-
setActiveTab(key)
6060
onSelectTab?.(event, key);
6161
};
6262

6363
const onTabClose = (item: ITab) => {
64-
let activekey = (data.filter(tab => item.key === tab.key))?.[0]?.key
65-
setActiveTab(activekey)
66-
onCloseTab?.(item)
64+
onCloseTab?.(item.key)
6765
};
6866

6967
const renderTabBar = (tab) => (
@@ -72,8 +70,7 @@ const Tabs = (props: ITabsProps) => {
7270
name={tab.name}
7371
modified={tab.modified}
7472
active={activeTab === tab.key}
75-
onClose={() => onCloseTab?.(tab)}
76-
className={'tab-button'}
73+
onClose={() => onCloseTab?.(tab.key)}
7774
/>)
7875
return (
7976
<DndProvider backend={HTML5Backend}>
@@ -92,7 +89,7 @@ const Tabs = (props: ITabsProps) => {
9289
>
9390
{type === 'editable-card' ? renderTabBar?.(tab) : tab.label}
9491
{closable && (
95-
<div className={getBEMModifier(tabItemClassName, 'close')} onClick={(e) => {
92+
<div className={classNames(tabItemCloseClassName, {[getBEMModifier(tabItemCloseClassName, 'active')]: activeTab === tab.key })} onClick={(e) => {
9693
e.stopPropagation()
9794
onTabClose(tab)
9895
}}>

src/components/tabs/style.scss

+2-16
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,13 @@
5353
justify-content: flex-start;
5454
}
5555

56-
&--close {
56+
&__close {
5757
font-size: 13px;
5858
font-weight: 700;
5959
height: 14px;
6060
margin-left: 8px;
61+
visibility: visible;
6162
width: 14px;
62-
63-
&:hover {
64-
visibility: hidden;
65-
}
66-
67-
&:active {
68-
transform: scale(1.1);
69-
}
7063
}
7164

7265
&__button {
@@ -134,10 +127,3 @@
134127
}
135128
}
136129
}
137-
138-
139-
.mo-ta {
140-
margin-bottom: auto;
141-
overflow: hidden;
142-
overflow-y: auto;
143-
}

src/workbench/editor/editor.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const groupBreadcrumbsClassName = getBEMElement(
2727

2828
function renderEditorGroup(group: IEditorGroup, onMoveTab, onSelectTab) {
2929
const editor = group.activeTab;
30+
// Todo 测试editor tabs
3031
return (
3132
<div className={groupClassName} key={`group-${group.id}`}>
3233
<div className={groupHeaderClassName}>

stories/components/3-Tabs.stories.tsx

+49-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ stories.add('Basic Usage', () => {
1919
renderPanel: 'this is a workSpace'
2020
},
2121
];
22-
const tabs = [
22+
const tabArr = [
2323
{
2424
key: '1',
2525
label: 'Tab1',
@@ -30,17 +30,53 @@ stories.add('Basic Usage', () => {
3030
label: 'Tab2',
3131
renderPanel: 'this is a tab2'
3232
},
33+
{
34+
key: '3',
35+
label: 'Tab3',
36+
renderPanel: 'this is a tab3'
37+
},
38+
{
39+
key: '4',
40+
label: 'Tab4',
41+
renderPanel: 'this is a tab4'
42+
}
3343
];
3444

35-
const [tabs1, setTabs1] = useState(userSetting);
36-
const [tabs2, setTabs2] = useState(tabs)
45+
const [tabs, setTabs] = useState(userSetting);
46+
const [tabs1, setTabs1] = useState(tabArr)
47+
const [activeTab, setActiveTab] = useState('1')
48+
const [activeTab1, setActiveTab1] = useState('2')
3749

3850
const onSelectTab = (e, tabKey) => {
39-
console.log(tabKey)
51+
setActiveTab(tabKey)
4052
}
41-
const onMoveTab = data => setTabs1(data)
53+
54+
const onSelectTab1 = (e, tabKey) => {
55+
setActiveTab1(tabKey)
56+
}
57+
const onMoveTab = data => setTabs(data)
4258

43-
const onMoveTab1 = data => setTabs2(data)
59+
const onMoveTab1 = data => setTabs1(data)
60+
61+
const onCloseTab1 = targetKey => {
62+
let newActiveKey = activeTab;
63+
let lastIndex;
64+
tabs1.forEach((pane, i) => {
65+
if (pane.key === targetKey) {
66+
lastIndex = i - 1;
67+
}
68+
});
69+
const newPanes = tabs1.filter(pane => pane.key !== targetKey);
70+
if (newPanes.length && newActiveKey === targetKey) {
71+
if (lastIndex >= 0) {
72+
newActiveKey = newPanes[lastIndex].key;
73+
} else {
74+
newActiveKey = newPanes[0].key;
75+
}
76+
}
77+
setTabs1(newPanes)
78+
setActiveTab1(newActiveKey)
79+
}
4480
return (
4581
<div>
4682
<h2>简述</h2>
@@ -49,22 +85,21 @@ stories.add('Basic Usage', () => {
4985
<h3>使用示例 1 - 基本使用</h3>
5086
<div style={{ height: 300 }}>
5187
<Tabs
52-
data={tabs1}
53-
activeTab={'1'}
88+
data={tabs}
89+
activeTab={activeTab}
5490
onMoveTab={onMoveTab}
5591
onSelectTab={onSelectTab}
56-
onTabChange={(data) => console.log(data)}
5792
/>
5893
</div>
5994
<h3>使用示例2 - 带关闭状态的tab</h3>
60-
<div style={{ height: 400 }}>
95+
<div style={{ height: 300 }}>
6196
<Tabs
62-
data={tabs2}
63-
activeTab={'2'}
97+
data={tabs1}
98+
activeTab={activeTab1}
6499
onMoveTab={onMoveTab1}
65-
onSelectTab={onSelectTab}
66-
onTabChange={(data) => console.log(data)}
100+
onSelectTab={onSelectTab1}
67101
closable={true}
102+
onCloseTab={onCloseTab1}
68103
/>
69104
</div>
70105
</div>

0 commit comments

Comments
 (0)