Skip to content

Commit a9354ec

Browse files
committed
feat: 新增tagsView
1 parent 75e36c7 commit a9354ec

File tree

14 files changed

+242
-36
lines changed

14 files changed

+242
-36
lines changed

src/components/BreadCrumb/index.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import "./index.less";
66
/**
77
* 根据当前浏览器地址栏的路由地址,在menuConfig中查找路由跳转的路径
88
* 如路由地址为/charts/keyboard,则查找到的路径为[{title: "图表",...},{title: "键盘图表",...}]
9-
* @param {*} menuList
10-
* @param {*} pathname
119
*/
1210
const getPath = (menuList, pathname) => {
1311
let temppath = [];

src/components/TagList/index.jsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
import { withRouter } from "react-router-dom";
4+
import { Tag } from "antd";
5+
import { deleteTaglist } from "@/store/actions";
6+
7+
const TagList = (props) => {
8+
const { history, deleteTaglist, taglist, tag } = props;
9+
const title = tag.title;
10+
const path = tag.path;
11+
const currentPath = history.location.pathname;
12+
13+
const handleClose = (path) => {
14+
const length = taglist.length;
15+
// 如果关闭的是当前页,跳转到最后一个tag
16+
if (path === currentPath) {
17+
history.push(taglist[length - 1].path);
18+
}
19+
// 如果关闭的是最后的tag ,且当前显示的也是最后的tag对应的页面,才做路由跳转
20+
if (
21+
path === taglist[length - 1].path &&
22+
currentPath === taglist[length - 1].path
23+
) {
24+
// 因为cutTaglist在最后执行,所以跳转到上一个tags的对应的路由,应该-2
25+
if (length - 2 > 0) {
26+
history.push(taglist[length - 2].path);
27+
} else if (length === 2) {
28+
history.push(taglist[0].path);
29+
}
30+
}
31+
32+
// 先跳转路由,再修改state树的taglist
33+
deleteTaglist(tag);
34+
};
35+
36+
const handleClick = (path, event) => {
37+
history.push(path);
38+
};
39+
40+
return (
41+
<Tag
42+
onClose={() => {
43+
handleClose(path);
44+
}}
45+
closable
46+
color={currentPath === path ? "geekblue" : "gold"}
47+
onClick={(e) => handleClick(path, e)}
48+
>
49+
{title}
50+
</Tag>
51+
);
52+
};
53+
54+
export default withRouter(
55+
connect((state) => state.tagsView, { deleteTaglist })(TagList)
56+
);

src/store/action-types.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ export const APP_TOGGLE_SIDEBAR = "APP_TOGGLE_SIDEBAR";
77
export const APP_TOGGLE_SETTINGPANEL = "APP_TOGGLE_SETTINGPANEL";
88

99
// settings
10-
1110
export const SETTINGS_CHANGE_SETTINGS = "SETTINGS_CHANGE_SETTINGS";
11+
12+
// tagsView
13+
export const TAGSVIEW_ADD_TAGLIST = "TAGSVIEW_ADD_TAGLIST";
14+
export const TAGSVIEW_DELETE_TAGLIST = "TAGSVIEW_DELETE_TAGLIST";
15+
export const TAGSVIEW_EMPTY_TAGLIST = "TAGSVIEW_EMPTY_TAGLIST";
16+

src/store/actions/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { login, logout } from "./auth";
22
import { getUserInfo, setUserToken, setUserInfo, resetUser } from "./user";
33
import { toggleSiderBar, toggleSettingPanel } from "./app";
44
import { changeSetting } from "./settings";
5+
import { addTaglist, emptyTaglist, deleteTaglist } from "./tagsView";
56
export {
67
login,
78
logout,
@@ -12,4 +13,7 @@ export {
1213
toggleSiderBar,
1314
toggleSettingPanel,
1415
changeSetting,
16+
addTaglist,
17+
emptyTaglist,
18+
deleteTaglist,
1519
};

src/store/actions/tagsView.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as types from "../action-types";
2+
export const addTaglist = (tag) => {
3+
return {
4+
type: types.TAGSVIEW_ADD_TAGLIST,
5+
tag
6+
};
7+
};
8+
9+
export const emptyTaglist = () => {
10+
return {
11+
type: types.TAGSVIEW_EMPTY_TAGLIST
12+
};
13+
};
14+
15+
export const deleteTaglist = (tag) => {
16+
return {
17+
type: types.TAGSVIEW_DELETE_TAGLIST,
18+
tag
19+
};
20+
};

src/store/reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { combineReducers } from "redux";
22
import user from "./user";
33
import app from "./app";
44
import settings from "./settings";
5+
import tagsView from "./tagsView";
56

67
export default combineReducers({
78
user,
89
app,
9-
settings
10+
settings,
11+
tagsView,
1012
});

src/store/reducers/tagsView.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as types from "../action-types";
2+
const initState = {
3+
taglist: [],
4+
};
5+
export default function app(state = initState, action) {
6+
switch (action.type) {
7+
case types.TAGSVIEW_ADD_TAGLIST:
8+
const tag = action.tag;
9+
if (state.taglist.includes(tag)) {
10+
return state;
11+
} else {
12+
return {
13+
...state,
14+
taglist: [...state.taglist, tag],
15+
};
16+
}
17+
case types.TAGSVIEW_DELETE_TAGLIST:
18+
return {
19+
...state,
20+
taglist: [...state.taglist.filter((item) => item !== action.tag)],
21+
};
22+
case types.TAGSVIEW_EMPTY_TAGLIST:
23+
return {
24+
...state,
25+
taglist: [],
26+
};
27+
default:
28+
return state;
29+
}
30+
}

src/utils/index.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
export function debounce(func, wait, immediate) {
2-
let timeout, args, context, timestamp, result
2+
let timeout, args, context, timestamp, result;
33

4-
const later = function() {
4+
const later = function () {
55
// 据上一次触发时间间隔
6-
const last = +new Date() - timestamp
6+
const last = +new Date() - timestamp;
77

88
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
99
if (last < wait && last > 0) {
10-
timeout = setTimeout(later, wait - last)
10+
timeout = setTimeout(later, wait - last);
1111
} else {
12-
timeout = null
12+
timeout = null;
1313
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
1414
if (!immediate) {
15-
result = func.apply(context, args)
16-
if (!timeout) context = args = null
15+
result = func.apply(context, args);
16+
if (!timeout) context = args = null;
1717
}
1818
}
19-
}
19+
};
2020

21-
return function(...args) {
22-
context = this
23-
timestamp = +new Date()
24-
const callNow = immediate && !timeout
21+
return function (...args) {
22+
context = this;
23+
timestamp = +new Date();
24+
const callNow = immediate && !timeout;
2525
// 如果延时不存在,重新设定延时
26-
if (!timeout) timeout = setTimeout(later, wait)
26+
if (!timeout) timeout = setTimeout(later, wait);
2727
if (callNow) {
28-
result = func.apply(context, args)
29-
context = args = null
28+
result = func.apply(context, args);
29+
context = args = null;
3030
}
3131

32-
return result
32+
return result;
33+
};
34+
}
35+
// 根据某个属性值从MenuList查找拥有该属性值的menuItem
36+
export function getMenuItemInMenuListByProperty(menuList, key, value) {
37+
let stack = [];
38+
stack = stack.concat(menuList);
39+
let res;
40+
while (stack.length) {
41+
let cur = stack.shift();
42+
if (cur.children && cur.children.length > 0) {
43+
stack = cur.children.concat(stack);
44+
}
45+
if (value === cur[key]) {
46+
res = cur;
47+
}
3348
}
34-
}
49+
return res;
50+
}

src/views/layout/Content/index.jsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@ import { Redirect, withRouter, Route, Switch } from "react-router-dom";
33
import DocumentTitle from "react-document-title";
44
import { connect } from "react-redux";
55
import { Layout } from "antd";
6+
import { getMenuItemInMenuListByProperty } from "@/utils";
67
import routeList from "@/config/routeMap";
78
import menuList from "@/config/menuConfig";
89
const { Content } = Layout;
910

1011
const getPageTitle = (menuList, pathname) => {
11-
let stack = [];
1212
let title = "Ant Design Pro";
13-
stack = stack.concat(menuList);
14-
15-
while (stack.length) {
16-
let cur = stack.shift();
17-
if (cur.children && cur.children.length > 0) {
18-
stack = cur.children.concat(stack);
19-
}
20-
if (pathname === cur.path) {
21-
title = `${cur.title} - Ant Design Pro`;
22-
}
13+
let item = getMenuItemInMenuListByProperty(menuList, "path", pathname);
14+
if (item) {
15+
title = `${item.title} - Ant Design Pro`;
2316
}
2417
return title;
2518
};
@@ -33,7 +26,7 @@ const LayoutContent = (props) => {
3326
};
3427
return (
3528
<DocumentTitle title={getPageTitle(menuList, pathname)}>
36-
<Content style={{"height":"calc(100% - 100px)"}}>
29+
<Content style={{ height: "calc(100% - 100px)" }}>
3730
<Switch>
3831
<Redirect exact from="/" to="/dashboard" />
3932
{routeList.map((route) => {

src/views/layout/Sider/Menu/index.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { Link, withRouter } from "react-router-dom";
44
import { Scrollbars } from "react-custom-scrollbars";
55
import { connect } from "react-redux";
66
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
7+
import { addTaglist } from "@/store/actions";
8+
import { getMenuItemInMenuListByProperty } from "@/utils";
79
import menuList from "@/config/menuConfig";
810
import "./index.less";
911
const SubMenu = Menu.SubMenu;
1012
// 重新记录数组顺序
1113
const reorder = (list, startIndex, endIndex) => {
1214
const result = Array.from(list);
13-
1415
const [removed] = result.splice(startIndex, 1);
15-
1616
result.splice(endIndex, 0, removed);
1717
return result;
1818
};
@@ -97,6 +97,11 @@ class Meun extends Component {
9797
});
9898
};
9999

100+
handleMenuSelect = ({ key }) => {
101+
let menuItem = getMenuItemInMenuListByProperty(menuList,'path',key)
102+
this.props.addTaglist(menuItem);
103+
};
104+
100105
componentWillMount() {
101106
const menuTreeNode = this.getMenuNodes(menuList);
102107
this.setState({
@@ -128,6 +133,7 @@ class Meun extends Component {
128133
<Menu
129134
mode="inline"
130135
theme="dark"
136+
onSelect={this.handleMenuSelect}
131137
selectedKeys={[path]}
132138
defaultOpenKeys={openKey}
133139
>
@@ -147,4 +153,4 @@ class Meun extends Component {
147153
}
148154
}
149155

150-
export default connect((state) => state.user)(withRouter(Meun));
156+
export default connect((state) => state.user, { addTaglist })(withRouter(Meun));

0 commit comments

Comments
 (0)