Skip to content

Commit 3597665

Browse files
committed
feat: resolve #37
1 parent 33c9f61 commit 3597665

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

src/components/tabs/index.tsx

+8-11
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import { Tab, ITab, tabItemClassName } from './tab';
1616
import './style.scss';
1717

1818
export type TabsType = 'line' | 'card';
19-
export interface ITabsProps {
19+
export interface ITabsProps<T>{
2020
closable?: boolean;
21-
data: ITab[];
21+
data: (ITab<T>)[];
2222
activeTab?: string;
2323
type?: TabsType;
2424
onCloseTab?: (key?: string) => void;
25-
onMoveTab?: (tabs: ITab[]) => void;
25+
onMoveTab?: (tabs: (ITab<T>)[]) => void;
2626
onSelectTab?: (key?: string) => void;
2727
}
2828

@@ -32,7 +32,7 @@ export const tabsContent = getBEMElement(tabsClassName, 'content');
3232
export const tabsContentItem = getBEMElement(tabsContent, 'item');
3333
export const tabItemCloseClassName = getBEMElement(tabItemClassName, 'close');
3434

35-
const Tabs = (props: ITabsProps) => {
35+
export function Tabs<T>(props: ITabsProps<T>) {
3636
const { activeTab, data, type = 'line', onMoveTab, ...resetProps } = props;
3737

3838
const onChangeTab = useCallback(
@@ -59,24 +59,23 @@ const Tabs = (props: ITabsProps) => {
5959
)}
6060
>
6161
<div className={tabsHeader}>
62-
{data?.map((tab: ITab, index: number) => {
62+
{data?.map((tab: (ITab<T>), index: number) => {
6363
return (
6464
<Tab
6565
active={activeTab === tab.key}
6666
index={index}
6767
label={tab.label}
68-
modified={tab.modified}
68+
data={tab.data}
6969
key={tab.key}
7070
propsKey={tab.key}
71-
title={tab.tip}
7271
onMoveTab={onChangeTab}
7372
{...resetProps}
7473
></Tab>
7574
);
7675
})}
7776
</div>
7877
<div className={tabsContent}>
79-
{data?.map((tab: ITab) => {
78+
{data?.map((tab: ITab<T>) => {
8079
return (
8180
<div
8281
className={classNames(tabsContentItem, {
@@ -92,6 +91,4 @@ const Tabs = (props: ITabsProps) => {
9291
</div>
9392
</DndProvider>
9493
);
95-
};
96-
97-
export default Tabs;
94+
};

src/components/tabs/tab.tsx

+15-14
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@ import {
1515
prefixClaName,
1616
} from 'mo/common/className';
1717
import TabDot from './tabDot';
18-
19-
export interface TabData<T = any> {
20-
modified?: boolean;
21-
language?: string | undefined;
22-
path?: string;
23-
value?: string;
24-
}
25-
export interface ITab extends TabData {
18+
export interface ITab<T> {
19+
active?: boolean;
20+
closable?: boolean;
21+
index?: number;
2622
key?: string;
23+
propsKey?: string;
2724
name?: string;
2825
label?: React.ReactNode;
2926
tip?: string | React.ReactNode;
3027
renderPanel?: React.ReactNode;
28+
data?: T
3129
}
3230

31+
export interface ITabEvent {
32+
onMoveTab?: (dragIndex: number, hoverIndex: number) => void;
33+
onCloseTab?: (key?: string) => void;
34+
onSelectTab?: (key?: string) => void;
35+
}
3336
export const tabClassName = prefixClaName('tab');
3437
export const tabItemClassName = getBEMElement(tabClassName, 'item');
3538

36-
export const Tab = (props) => {
39+
export function Tab<T> (props: ITab<T> & ITabEvent) {
3740
const {
3841
closable,
3942
index,
@@ -67,7 +70,7 @@ export const Tab = (props) => {
6770
if (!ref.current) return;
6871
const component = ref.current;
6972
const dragIndex = monitor.getItem().index;
70-
let hoverIndex = index;
73+
let hoverIndex = index!;
7174
// Don't replace items with themselves
7275
if (dragIndex === hoverIndex) {
7376
return;
@@ -89,7 +92,7 @@ export const Tab = (props) => {
8992
if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
9093
return;
9194
}
92-
onMoveTab(dragIndex, hoverIndex);
95+
onMoveTab?.(dragIndex, hoverIndex);
9396
monitor.getItem().index = hoverIndex;
9497
},
9598
});
@@ -101,7 +104,7 @@ export const Tab = (props) => {
101104
className={classNames(tabItemClassName, {
102105
[getBEMModifier(tabItemClassName, 'active')]: active,
103106
})}
104-
onClick={(event: React.MouseEvent) => onSelectTab(propsKey)}
107+
onClick={(event: React.MouseEvent) => onSelectTab?.(propsKey)}
105108
onMouseOver={handleMouseOver}
106109
onMouseOut={handleMouseOut}
107110
>
@@ -118,5 +121,3 @@ export const Tab = (props) => {
118121
</div>
119122
);
120123
};
121-
122-
export default Tab;

src/extensions/search/searchPane.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ export default class SearchPane extends React.Component<
8484
const tabData = {
8585
key: `${key}`,
8686
name: `editor.js`,
87-
modified: false,
88-
value: `hello javascript ${key}`,
89-
path: 'desktop/molecule/editor1',
87+
data: {
88+
modified: false,
89+
value: `hello javascript ${key}`,
90+
path: 'desktop/molecule/editor1',
91+
language: 'javascript',
92+
}
9093
};
9194
console.log('open editor:', tabData);
9295
editorService.open(tabData, 1);

src/workbench/editor/editor.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ import SplitPane from 'react-split-pane';
44

55
import { getBEMElement, prefixClaName } from 'mo/common/className';
66
import MonacoEditor from 'mo/components/monaco-editor';
7-
import Tabs from 'mo/components/tabs';
7+
import { Tabs } from 'mo/components/tabs';
88
import { tabItemClassName } from 'mo/components/tabs/tab';
99
import { Icon } from 'mo/components/icon';
1010
import Welcome from './welcome';
1111
import { IEditor, IEditorGroup } from 'mo/model';
1212

13+
interface dataType {
14+
modified?: boolean;
15+
language?: string | undefined;
16+
path?: string;
17+
value?: string;
18+
}
19+
1320
const defaultEditorClassName = prefixClaName('editor');
1421
const groupClassName = getBEMElement(defaultEditorClassName, 'group');
1522

@@ -22,8 +29,6 @@ function renderEditorGroup(
2229
const editor = group.activeTab;
2330
const tabs = group.tabs?.map((item, index) => {
2431
return Object.assign({}, item, {
25-
key: item.key,
26-
tip: item.path,
2732
label: [
2833
<Icon type="new-file" />,
2934
<span className={getBEMElement(tabItemClassName, 'name')}>
@@ -33,8 +38,8 @@ function renderEditorGroup(
3338
renderPanel: (
3439
<MonacoEditor
3540
options={{
36-
value: item.value,
37-
language: item.language || 'sql',
41+
value: item?.data?.value,
42+
language: item?.data?.language || 'sql',
3843
automaticLayout: true,
3944
}}
4045
editorInstanceRef={(editorInstance) => {
@@ -48,6 +53,7 @@ function renderEditorGroup(
4853
return (
4954
<div className={groupClassName} key={`group-${group.id}`}>
5055
<Tabs
56+
<dataType>
5157
closable={true}
5258
type="card"
5359
data={tabs}

stories/components/3-Tabs.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { useState } from 'react';
3-
import Tabs from 'mo/components/tabs';
3+
import { Tabs } from 'mo/components/tabs';
44
import { storiesOf } from '@storybook/react';
55
import { withKnobs } from '@storybook/addon-knobs';
66
const stories = storiesOf('Tab', module);

0 commit comments

Comments
 (0)