vite-bolt/plugin/common/Nav.tsx

66 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2023-02-01 17:52:53 +08:00
import { useState, useEffect } from "react";
import Search20Regular from "@ricons/fluent/Search20Regular";
import { useLocation, useNavigate } from "react-router";
import { ICustomPage } from "../types";
interface IProps {
customPages: ICustomPage[];
}
interface INavItem extends ICustomPage {
active: boolean;
key: string;
}
export default function Nav(props: IProps) {
const navigate = useNavigate();
const location = useLocation();
const [navList, setNavList] = useState<INavItem[]>([]);
useEffect(() => {
const navList = props.customPages.map((i: ICustomPage) => ({
...i,
key: i.path,
active: false,
}));
setNavList(navList);
}, [props.customPages]);
/**
* active
*/
useEffect(() => {
const path = location.pathname;
setNavList((p) => p.map((i) => ({ ...i, active: path === i.path })));
}, [location.pathname]);
/**
*
*/
const onClickNavItem = (key: string) => {
const nav = navList.find((i: INavItem) => i.key === key);
setNavList((p: INavItem[]) =>
p.map((i: INavItem) => ({ ...i, active: key === i.key }))
);
navigate(nav!.path);
};
return (
<nav>
<div className="start"></div>
<div className="end">
<div className="search-container">
<Search20Regular />
<input type="text" placeholder="搜索 / 待开发" />
</div>
<ul>
{navList.map((item) => (
<li key={item.key} onClick={() => onClickNavItem(item.key)}>
<span className={item.active ? "active" : ""}>{item.title}</span>
</li>
))}
</ul>
</div>
</nav>
);
}