fix menus

This commit is contained in:
2025-09-19 09:43:30 +07:00
parent a56801f296
commit bce5cc37e5
3 changed files with 79 additions and 43 deletions

View File

@@ -1,17 +1,22 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { AppstoreOutlined, MailOutlined, SettingOutlined } from '@ant-design/icons'; import { AppstoreOutlined, MailOutlined, SettingOutlined, CloseOutlined } from '@ant-design/icons';
import { Menu } from 'antd'; import { Menu } from 'antd';
import { Radio, Tabs } from 'antd'; import { Radio, Tabs } from 'antd';
const items = [ const items = [
{
key: 'Dashboard',
icon: <MailOutlined />,
label: 'Dashboard'
},
{ {
key: '1', key: '1',
icon: <MailOutlined />, icon: <MailOutlined />,
label: 'Navigation One', label: 'Master',
children: [ children: [
{ key: '11', label: 'Option 1' }, { key: 'Users', label: 'Users' },
{ key: '12', label: 'Option 2' }, { key: 'Devices', label: 'Devices' },
{ key: '13', label: 'Option 3' }, { key: 'Tags', label: 'Tags' },
{ key: '14', label: 'Option 4' }, { key: 'Products', label: 'Products' },
], ],
}, },
{ {
@@ -72,28 +77,28 @@ const levelKeys = getLevelKeys(items);
const TheChild = () => {return(<div>Hahalo</div>)} const TheChild = () => {return(<div>Hahalo</div>)}
const App = () => { const App = () => {
// const [stateOpenKeys, setStateOpenKeys] = useState(['2', '23']); // const [stateOpenKeys, setStateOpenKeys] = useState(['2', '23']);
const [stateOpenKeys, setStateOpenKeys] = useState(['1']); const [stateOpenKeys, setStateOpenKeys] = useState(['0']);
const [itemsTab, setItemsTab] = useState([ const [itemsTab, setItemsTab] = useState([
{ {
label: 'Tab 1', label: 'Dashboard',
key: '1', key: 'Dashboard',
children: 'Content of editable tab 1',
},
{
label: 'Tab 2',
key: '2',
children: 'Content of editable tab 2',
},
{
label: 'Tab 3',
key: '3',
// children: 'Content of editable tab 3',
children: <TheChild />, children: <TheChild />,
}, },
// {
// label: 'Tab 2',
// key: '2',
// children: 'Content of editable tab 2',
// },
// {
// label: 'Tab 3',
// key: '3',
// // children: 'Content of editable tab 3',
// children: <TheChild />,
// },
]); ]);
const [size, setSize] = useState('small'); const [size, setSize] = useState('small');
const [activeKey, setActiveKey] = useState('1'); const [activeKey, setActiveKey] = useState('Dashboard');
const onOpenChange = openKeys => { const onOpenChange = openKeys => {
const currentOpenKey = openKeys.find(key => stateOpenKeys.indexOf(key) === -1); const currentOpenKey = openKeys.find(key => stateOpenKeys.indexOf(key) === -1);
@@ -127,25 +132,35 @@ const App = () => {
} }
setItemsTab(newItems); setItemsTab(newItems);
}; };
const add = () => { const add = (key) => {
// const newKey = String((items || []).length + 1); // const newKey = String((items || []).length + 1);
const newKey = String((itemsTab || []).length + 1); // const newKey = String((itemsTab || []).length + 1);
console.log("newKey", newKey) // console.log("newKey", newKey)
// console.log("items", items) // console.log("items", items)
console.log("itemsTab", itemsTab) // console.log("itemsTab before", itemsTab)
setItemsTab([ let newtab = {
...(itemsTab || []), // label: `Tab ${newKey}`,
{ label: `${key[0]}`,
label: `Tab ${newKey}`, key: key[0],
key: newKey, children: `Content of editable tab ${key[0]}`,
children: `Content of editable tab ${newKey}`, }
},
]); if (!itemsTab.find(item => item.key === newtab.key)) {
setActiveKey(newKey); setItemsTab([
...(itemsTab || []),
newtab,
]);
} else {
// Do nothing
console.log("Do nothing")
}
setActiveKey(key[0]);
}; };
const onEdit = (targetKey, action) => { const onAddTab = (targetKey, action) => {
if (action === 'add') { if (action === 'add') {
add(); add(key);
// console.log("Plus plus")
} else { } else {
// console.log("targetKey", targetKey) // console.log("targetKey", targetKey)
remove(targetKey); remove(targetKey);
@@ -154,22 +169,39 @@ const App = () => {
// const onChange = e => { // const onChange = e => {
// setSize(e.target.value); // setSize(e.target.value);
// }; // };
const renderTabBar = (props, DefaultTabBar) => {
return (
<DefaultTabBar {...props}>
{(node) => {
// Filter out the add button node
if (node.key && node.key.toString().startsWith('rc-tabs-add')) {
return null;
}
return node;
}}
</DefaultTabBar>
);
};
return ( return (
<React.Fragment> <React.Fragment>
<div <div
style={{ style={{
display: "flex" display: "flex",
height: "100vh"
}} }}
> >
<Menu <Menu
mode="inline" mode="inline"
defaultSelectedKeys={['231']} // defaultSelectedKeys={['231']}
defaultSelectedKeys={['Dashboard']}
openKeys={stateOpenKeys} openKeys={stateOpenKeys}
onOpenChange={onOpenChange} onOpenChange={onOpenChange}
style={{ width: 256 }} style={{ width: 256 }}
items={items} items={items}
// onClick={(e) => {console.log(e.key)}} // onClick={(e) => {console.log(e)}}
onClick={(e) => add()} onClick={(e) => add(e.keyPath)}
/> />
<div <div
style={{ style={{
@@ -179,11 +211,15 @@ const App = () => {
> >
<Tabs <Tabs
type="editable-card" type="editable-card"
// type="card"
size={size} size={size}
activeKey={activeKey} activeKey={activeKey}
onChange={setActiveKey} onChange={setActiveKey}
onEdit={onEdit} onEdit={onAddTab}
items={itemsTab} items={itemsTab}
hideAdd
// removeIcon={<CloseOutlined />}
// renderTabBar={renderTabBar}
/> />
</div> </div>
</div> </div>

View File

@@ -25,7 +25,7 @@ a:hover {
body { body {
margin: 0; margin: 0;
display: flex; display: flex;
place-items: center; /* place-items: center; */
min-width: 320px; min-width: 320px;
min-height: 100vh; min-height: 100vh;
} }

View File

@@ -1,6 +1,6 @@
import { StrictMode } from 'react' import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client' import { createRoot } from 'react-dom/client'
import './index.css' // import './index.css'
import App from './App.jsx' import App from './App.jsx'
import 'antd/dist/reset.css'; // Ant Design base styles import 'antd/dist/reset.css'; // Ant Design base styles