Skip to content

Commit 7682179

Browse files
authored
feat:Exposing more props to renderPane for EditorTab (#759)
* feat: tab.renderPane method arguments add tab and group * feat: add renderPane example * test: add the renderPane method use case
1 parent 3093a21 commit 7682179

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

src/components/tabs/tab.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import { useRef } from 'react';
33
import { findDOMNode } from 'react-dom';
44
import { useDrag, useDrop } from 'react-dnd';
55

6+
import { IEditorGroup } from 'mo/model';
7+
import type { UniqueId } from 'mo/common/types';
68
import {
79
classNames,
810
getBEMElement,
911
getBEMModifier,
1012
prefixClaName,
1113
} from 'mo/common/className';
14+
1215
import TabExtra from './tabExtra';
1316
import { Icon } from '../icon';
14-
import type { UniqueId } from 'mo/common/types';
1517

1618
export interface ITabEvent {
1719
onDrag?: (
@@ -49,7 +51,9 @@ export interface ITabProps<T = any, P = any> {
4951
icon?: string | JSX.Element;
5052
id: UniqueId;
5153
name?: string;
52-
renderPane?: ((item: P) => React.ReactNode) | React.ReactNode;
54+
renderPane?:
55+
| ((item: P, tab?: ITabProps, group?: IEditorGroup) => React.ReactNode)
56+
| React.ReactNode;
5357
data?: T;
5458
}
5559

src/workbench/editor/__tests__/group.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import 'reflect-metadata';
12
import React from 'react';
23
import { render, cleanup, fireEvent, waitFor } from '@testing-library/react';
34
import { tabItemActiveClassName } from 'mo/components/tabs/tab';
45

56
import EditorGroup from '../group';
7+
import { IEditorTab } from 'mo/model';
68

79
const TEST_ID = 'test-id';
810
const TEST_MENU = 'test-menu';
@@ -130,4 +132,24 @@ describe('The Editor Component', () => {
130132
expect(fn).toBeCalled();
131133
});
132134
});
135+
136+
test('use renderPane method', async () => {
137+
const renderPane = jest.fn((tabData, tab, group) => {
138+
return <div className={tab.id}>{tab.id}</div>;
139+
});
140+
141+
const testTab: IEditorTab = {
142+
id: TEST_ID,
143+
name: TEST_ID,
144+
data: {},
145+
renderPane,
146+
};
147+
148+
const { container } = render(
149+
<EditorGroup id="test" onClickActions={jest.fn()} tab={testTab} />
150+
);
151+
const renderDiv = container.querySelector(`.${TEST_ID}`);
152+
153+
expect(renderDiv?.innerHTML).toEqual(TEST_ID);
154+
});
133155
});

src/workbench/editor/editor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function Editor(
5252
<EditorGroup
5353
editorOptions={editorOptions}
5454
currentGroup={current!}
55+
group={groups[0]}
5556
{...groups[0]}
5657
{...getEvents(groups[0].id!)}
5758
/>
@@ -74,6 +75,7 @@ export function Editor(
7475
<EditorGroup
7576
editorOptions={editorOptions}
7677
currentGroup={current!}
78+
group={g}
7779
{...g}
7880
{...getEvents(g.id!)}
7981
/>

src/workbench/editor/group.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { tabItemActiveClassName } from 'mo/components/tabs/tab';
2222
export interface IEditorGroupProps extends IEditorGroup {
2323
currentGroup?: IEditorGroup;
2424
editorOptions?: IEditorOptions;
25+
group?: IEditorGroup;
2526
}
2627

2728
export function EditorGroup(props: IEditorGroupProps & IEditorController) {
@@ -30,6 +31,7 @@ export function EditorGroup(props: IEditorGroupProps & IEditorController) {
3031
data,
3132
tab,
3233
currentGroup,
34+
group,
3335
actions = [],
3436
menu = [],
3537
onMoveTab,
@@ -114,7 +116,13 @@ export function EditorGroup(props: IEditorGroupProps & IEditorController) {
114116
// Default we use monaco editor, but also you can customize by renderPanel() function or a react element
115117
tab?.renderPane ? (
116118
typeof tab.renderPane === 'function' ? (
117-
tab.renderPane(tab.data)
119+
tab.renderPane(
120+
{
121+
...tab.data,
122+
},
123+
tab,
124+
group
125+
)
118126
) : (
119127
tab.renderPane
120128
)

stories/extensions/test/testPane.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,31 @@ export type GenericClassDecorator<T> = (target: T) => void;`,
228228
}
229229
};
230230

231+
const newPane = function () {
232+
const key = shortRandomId();
233+
const name = `pane-${key}.ts`;
234+
const tabData: IEditorTab = {
235+
id: `${key}`,
236+
name,
237+
icon: 'selection',
238+
data: {},
239+
breadcrumb: [{ id: `${key}`, name }],
240+
renderPane: (tabData, tab, group) => {
241+
console.log(tabData, tab, group);
242+
const style: React.CSSProperties = {
243+
display: 'flex',
244+
width: '100%',
245+
height: '100%',
246+
fontSize: 48,
247+
alignItems: 'center',
248+
justifyContent: 'center',
249+
};
250+
return <div style={style}>{name}</div>;
251+
},
252+
};
253+
molecule.editor.open(tabData);
254+
};
255+
231256
const updateEntryPage = () => {
232257
const style: React.CSSProperties = {
233258
display: 'flex',
@@ -435,6 +460,7 @@ PARTITIONED BY (DE STRING) LIFECYCLE 1000;
435460
<Button onClick={toggleEditorStatus}>
436461
Toggle Editor status
437462
</Button>
463+
<Button onClick={newPane}>New Pane</Button>
438464
<Button onClick={updateEntryPage}>
439465
Update Entry Page
440466
</Button>

0 commit comments

Comments
 (0)