Skip to content

Commit de20cd6

Browse files
committed
Make it so you can change tabs from menu
1 parent 3037506 commit de20cd6

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

src/App.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ const App: React.FC = () => {
5353
const [toolboxSettingsModalIsOpen, setToolboxSettingsModalIsOpen] = React.useState(false);
5454
const [project, setProject] = React.useState<commonStorage.Project | null>(null);
5555
const [tabItems, setTabItems] = React.useState<Tabs.TabItem[]>([]);
56+
const [activeTab, setActiveTab] = React.useState('');
5657

5758
const [shownPythonToolboxCategories, setShownPythonToolboxCategories] = React.useState<Set<string>>(new Set());
5859
const blocksEditor = React.useRef<editor.Editor | null>(null);
5960
const [triggerPythonRegeneration, setTriggerPythonRegeneration] = React.useState(0);
6061
const generatorContext = React.useRef<GeneratorContext | null>(null);
6162
const blocklyComponent = React.useRef<BlocklyComponentType | null>(null);
63+
6264

6365
const [leftCollapsed, setLeftCollapsed] = React.useState(false);
6466
const [rightCollapsed, setRightCollapsed] = React.useState(false);
@@ -136,10 +138,11 @@ const App: React.FC = () => {
136138
{ key: opmode.modulePath, title: opmode.className, type: Tabs.TabType.OPMODE }
137139
);
138140
});
139-
setTabItems(myTabs);
141+
setTabItems(myTabs);
142+
setActiveTab(project.modulePath);
140143
}
141144
else{
142-
setTabItems([]);
145+
//setTabItems([]);
143146
}
144147
}, [project]);
145148

@@ -237,6 +240,12 @@ const areBlocksModified = (): boolean => {
237240
}
238241
return false;
239242
}
243+
const changeModule = async (module : commonStorage.Module | null) =>{
244+
if(currentModule && areBlocksModified()){
245+
await saveBlocks();
246+
}
247+
setCurrentModule(module);
248+
}
240249

241250
const { Sider } = Antd.Layout;
242251

@@ -276,20 +285,20 @@ return (
276285
<Menu.Component
277286
storage={storage}
278287
setAlertErrorMessage={setAlertErrorMessage}
279-
saveBlocks={saveBlocks}
280-
areBlocksModified={areBlocksModified}
281-
currentModule={currentModule}
282-
setCurrentModule={setCurrentModule}
288+
gotoTab={setActiveTab}
283289
project={project}
284290
setProject={setProject}
285291
/>
286292
</Sider>
287293
<Antd.Layout>
288294
<Tabs.Component
289295
tabList={tabItems}
296+
activeTab={activeTab}
297+
setTabList={setTabItems}
290298
setAlertErrorMessage={setAlertErrorMessage}
291299
currentModule={currentModule}
292-
setCurrentModule={setCurrentModule}
300+
setCurrentModule={changeModule}
301+
project={project}
293302
/>
294303
<Antd.Splitter>
295304
{/*

src/reactComponents/Menu.tsx

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import {
1818
export interface MenuProps {
1919
setAlertErrorMessage: (message: string) => void;
2020
storage: commonStorage.Storage | null;
21-
currentModule: commonStorage.Module | null;
22-
setCurrentModule: (module: commonStorage.Module | null) => void;
23-
saveBlocks: () => Promise<boolean>;
24-
areBlocksModified: () => boolean;
21+
gotoTab: (tabKey : string) => void;
2522
project: commonStorage.Project | null;
2623
setProject: (project: commonStorage.Project | null) => void;
2724
}
@@ -149,34 +146,11 @@ export function Component(props: MenuProps) {
149146
}
150147
});
151148
};
152-
const moduleFromPath = (key : string) : (commonStorage.Module | null) =>{
153-
let foundModule = null;
154-
155-
if(props.project){
156-
if (key == props.project.modulePath){
157-
foundModule = props.project;
158-
return foundModule;
159-
}
160-
props.project.mechanisms.forEach((mechanism) => {
161-
if(key == mechanism.modulePath){
162-
foundModule = mechanism;
163-
return foundModule;
164-
}
165-
});
166-
props.project.opModes.forEach((opmode) => {
167-
if(key == opmode.modulePath){
168-
foundModule = opmode;
169-
return foundModule;
170-
}
171-
});
172-
}
173-
174-
return foundModule;
175-
};
149+
176150
const handleSelect: Antd.MenuProps['onSelect'] = ({ key }) => {
177-
let newModule = moduleFromPath(key);
151+
let newModule = props.project ? commonStorage.findModuleInProject(props.project, key) : null;
178152
if(newModule){
179-
props.setCurrentModule(newModule);
153+
props.gotoTab(newModule.modulePath);
180154
}
181155
else{
182156
// TODO: It wasn't a module, so do the other thing...

src/reactComponents/Tabs.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ export interface TabItem {
4444

4545
export interface TabsProps {
4646
tabList: TabItem[];
47+
setTabList: (items: TabItem[]) => void;
48+
activeTab: string;
49+
project: commonStorage.Project | null;
4750
setAlertErrorMessage: (message: string) => void;
48-
currentModule: commonStorage.Module | null;
51+
currentModule: commonStorage.Module | null;
4952
setCurrentModule: (module: commonStorage.Module | null) => void;
5053
}
5154

@@ -61,15 +64,23 @@ function getIcon(type: TabType) {
6164

6265
export function Component(props: TabsProps) {
6366
const { t } = I18Next.useTranslation();
64-
65-
const [items, setItems] = React.useState<TabsProps['tabList']>(props.tabList);
66-
const [activeKey, setActiveKey] = React.useState('1');
67-
67+
68+
const [activeKey, setActiveKey] = React.useState(props.activeTab);
69+
6870
const onChange = (key: string) => {
69-
console.log(key);
71+
if (props.project) {
72+
props.setCurrentModule(commonStorage.findModuleInProject(props.project, key));
73+
setActiveKey(key);
74+
}
7075
}
76+
React.useEffect(() => {
77+
if (activeKey != props.activeTab) {
78+
onChange(props.activeTab);
79+
}
80+
}, [props.activeTab]);
7181

7282
const onEdit = (targetKey: React.MouseEvent | React.KeyboardEvent | string, action: 'add' | 'remove') => {
83+
const items = props.tabList;
7384
if (action === 'remove') {
7485
if (!items) return;
7586
const targetIndex = items.findIndex((item) => item.key === targetKey);
@@ -81,16 +92,15 @@ export function Component(props: TabsProps) {
8192
setActiveKey(newActiveKey);
8293
}
8394

84-
setItems(newItems);
95+
props.setTabList(newItems);
8596
};
8697
};
8798

88-
8999
return (
90100
<Antd.Tabs type="editable-card"
91101
onChange={onChange}
92102
onEdit={onEdit}
93-
defaultActiveKey={props.tabList.length > 1 ? props.tabList[0].key : undefined}
103+
activeKey={activeKey}
94104
tabBarStyle={{ padding: 0, margin: 0 }}
95105
hideAdd={false}
96106
items={props.tabList.map((tab) => {

0 commit comments

Comments
 (0)