Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
๐Ÿ› bug: fix menudata not using the latest version (#575)
Browse files Browse the repository at this point in the history
* ๐Ÿ› bug: fix menudata not using the latest version

* snapshots updated
  • Loading branch information
chenshuai2144 authored Jul 18, 2020
1 parent 28cfeb2 commit 850b6da
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 121 deletions.
29 changes: 23 additions & 6 deletions src/SiderMenu/BaseMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {
// ็”จไบŽๅ‡ๅฐ‘ defaultOpenKeys ่ฎก็ฎ—็š„็ป„ไปถ
const defaultOpenKeysRef = useRef<string[]>([]);
const [postMenuData, setPostMenuData] = useState(() => menuData);
const postMenuDataRef = useRef(postMenuData);

const { pathname } = location;

Expand Down Expand Up @@ -313,7 +314,10 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {
if (menu.defaultOpenAll || propsOpenKeys === false || flatMenuKeys.length) {
return;
}
const keys = getSelectedMenuKeys(location.pathname || '/', menuData || []);
const keys = getSelectedMenuKeys(
location.pathname || '/',
postMenuDataRef.current || [],
);
if (keys) {
openKeysRef.current = keys;
setOpenKeys(keys);
Expand All @@ -332,7 +336,10 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {

useEffect(() => {
// if pathname can't match, use the nearest parent's key
const keys = getSelectedMenuKeys(location.pathname || '/', menuData || []);
const keys = getSelectedMenuKeys(
location.pathname || '/',
postMenuDataRef.current || [],
);
const animationFrameId = requestAnimationFrame(() => {
if (keys.join('-') !== (selectedKeys || []).join('-')) {
setSelectedKeys(keys);
Expand Down Expand Up @@ -364,6 +371,10 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {

const [menuUtils] = useState(() => new MenuUtil(props));

/**
* ่ฟ™้‡Œ้œ€่ฆ็”จ menuData
* ไธบไบ†่ฎก็ฎ— splitMenus ้œ€่ฆ็”จๆœ€ๅ…จ็š„ menuData
*/
useEffect(() => {
if (splitMenus && openKeys) {
const keys = getSelectedMenuKeys(
Expand Down Expand Up @@ -391,6 +402,15 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {
}
}

const finallyData = props.postMenuData
? props.postMenuData(postMenuData)
: postMenuData;

/**
* ่ฎฐไธ‹ๆœ€ๆ–ฐ็š„ menuData
*/
postMenuDataRef.current = finallyData;

return (
<Menu
{...openKeysProps}
Expand All @@ -405,10 +425,7 @@ const BaseMenu: React.FC<BaseMenuProps> = (props) => {
onOpenChange={(keys) => setOpenKeys(keys as string[])}
{...props.menuProps}
>
{menuUtils.getNavMenuItems(
props.postMenuData ? props.postMenuData(postMenuData) : postMenuData,
false,
)}
{menuUtils.getNavMenuItems(finallyData, false)}
</Menu>
);
};
Expand Down
Loading

0 comments on commit 850b6da

Please sign in to comment.