fix(core): use Link from react-router-dom (#3342)

This commit is contained in:
Alex Yang
2023-07-21 18:29:36 +08:00
committed by GitHub
parent 869d98d019
commit f05cd66368
5 changed files with 34 additions and 30 deletions

View File

@@ -52,7 +52,7 @@ export const ReferencePage = ({
data-type="favorite-list-item" data-type="favorite-list-item"
data-testid={`favorite-list-item-${pageId}`} data-testid={`favorite-list-item-${pageId}`}
active={active} active={active}
href={`/workspace/${workspace.id}/${pageId}`} to={`/workspace/${workspace.id}/${pageId}`}
icon={icon} icon={icon}
collapsed={collapsible ? collapsed : undefined} collapsed={collapsible ? collapsed : undefined}
onCollapsedChange={setCollapsed} onCollapsedChange={setCollapsed}

View File

@@ -53,7 +53,7 @@ const RouteMenuLinkItem = React.forwardRef<
HTMLDivElement, HTMLDivElement,
{ {
currentPath: string; // todo: pass through useRouter? currentPath: string; // todo: pass through useRouter?
path?: string | null; path: string;
icon: ReactElement; icon: ReactElement;
children?: ReactElement; children?: ReactElement;
isDraggedOver?: boolean; isDraggedOver?: boolean;
@@ -66,7 +66,7 @@ const RouteMenuLinkItem = React.forwardRef<
ref={ref} ref={ref}
{...props} {...props}
active={active} active={active}
href={path ?? ''} to={path ?? ''}
icon={icon} icon={icon}
> >
{children} {children}
@@ -169,7 +169,7 @@ export const RootAppSidebar = ({
<RouteMenuLinkItem <RouteMenuLinkItem
icon={<FolderIcon />} icon={<FolderIcon />}
currentPath={currentPath} currentPath={currentPath}
path={currentWorkspaceId && paths.all(currentWorkspaceId)} path={paths.all(currentWorkspaceId)}
onClick={backToAll} onClick={backToAll}
> >
<span data-testid="all-pages">{t['All pages']()}</span> <span data-testid="all-pages">{t['All pages']()}</span>
@@ -198,7 +198,7 @@ export const RootAppSidebar = ({
isDraggedOver={trashDroppable.isOver} isDraggedOver={trashDroppable.isOver}
icon={<DeleteTemporarilyIcon />} icon={<DeleteTemporarilyIcon />}
currentPath={currentPath} currentPath={currentPath}
path={currentWorkspaceId && paths.trash(currentWorkspaceId)} path={paths.trash(currentWorkspaceId)}
> >
<span data-testid="trash-page">{t['Trash']()}</span> <span data-testid="trash-page">{t['Trash']()}</span>
</RouteMenuLinkItem> </RouteMenuLinkItem>

View File

@@ -24,6 +24,7 @@ import {
import type { Meta, StoryFn } from '@storybook/react'; import type { Meta, StoryFn } from '@storybook/react';
import { useAtom } from 'jotai'; import { useAtom } from 'jotai';
import { type PropsWithChildren, useState } from 'react'; import { type PropsWithChildren, useState } from 'react';
import { MemoryRouter } from 'react-router-dom';
export default { export default {
title: 'Components/AppSidebar', title: 'Components/AppSidebar',
@@ -31,6 +32,7 @@ export default {
} satisfies Meta; } satisfies Meta;
const Container = ({ children }: PropsWithChildren) => ( const Container = ({ children }: PropsWithChildren) => (
<MemoryRouter>
<main <main
style={{ style={{
position: 'relative', position: 'relative',
@@ -43,6 +45,7 @@ const Container = ({ children }: PropsWithChildren) => (
> >
{children} {children}
</main> </main>
</MemoryRouter>
); );
const Main = () => { const Main = () => {
const [open, setOpen] = useAtom(appSidebarOpenAtom); const [open, setOpen] = useAtom(appSidebarOpenAtom);
@@ -94,21 +97,21 @@ export const WithItems: StoryFn = () => {
<div style={{ height: '20px' }} /> <div style={{ height: '20px' }} />
<MenuLinkItem <MenuLinkItem
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Settings Settings
</MenuLinkItem> </MenuLinkItem>
<MenuLinkItem <MenuLinkItem
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Settings Settings
</MenuLinkItem> </MenuLinkItem>
<MenuLinkItem <MenuLinkItem
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Settings Settings
@@ -121,7 +124,7 @@ export const WithItems: StoryFn = () => {
collapsed={collapsed} collapsed={collapsed}
onCollapsedChange={setCollapsed} onCollapsedChange={setCollapsed}
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Collapsible Item Collapsible Item
@@ -130,14 +133,14 @@ export const WithItems: StoryFn = () => {
collapsed={!collapsed} collapsed={!collapsed}
onCollapsedChange={setCollapsed} onCollapsedChange={setCollapsed}
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Collapsible Item Collapsible Item
</MenuLinkItem> </MenuLinkItem>
<MenuLinkItem <MenuLinkItem
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Settings Settings
@@ -146,7 +149,7 @@ export const WithItems: StoryFn = () => {
<CategoryDivider label="Others" /> <CategoryDivider label="Others" />
<MenuLinkItem <MenuLinkItem
icon={<DeleteTemporarilyIcon />} icon={<DeleteTemporarilyIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Trash Trash

View File

@@ -18,7 +18,7 @@ export const Default: StoryFn = () => {
</MenuItem> </MenuItem>
<MenuLinkItem <MenuLinkItem
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Normal Link Item Normal Link Item
@@ -26,7 +26,7 @@ export const Default: StoryFn = () => {
<MenuLinkItem <MenuLinkItem
active active
icon={<SettingsIcon />} icon={<SettingsIcon />}
href="/test" to="/test"
onClick={() => alert('opened')} onClick={() => alert('opened')}
> >
Primary Item Primary Item

View File

@@ -1,7 +1,8 @@
import { ArrowDownSmallIcon } from '@blocksuite/icons'; import { ArrowDownSmallIcon } from '@blocksuite/icons';
import { Link, type LinkProps } from '@mui/material';
import clsx from 'clsx'; import clsx from 'clsx';
import React from 'react'; import React from 'react';
import type { LinkProps } from 'react-router-dom';
import { Link } from 'react-router-dom';
import * as styles from './index.css'; import * as styles from './index.css';
@@ -16,7 +17,7 @@ export interface MenuItemProps extends React.HTMLAttributes<HTMLDivElement> {
export interface MenuLinkItemProps export interface MenuLinkItemProps
extends MenuItemProps, extends MenuItemProps,
Pick<LinkProps, 'href'> {} Pick<LinkProps, 'to'> {}
const stopPropagation: React.MouseEventHandler = e => { const stopPropagation: React.MouseEventHandler = e => {
e.stopPropagation(); e.stopPropagation();
@@ -90,9 +91,9 @@ export const MenuItem = React.forwardRef<HTMLDivElement, MenuItemProps>(
MenuItem.displayName = 'MenuItem'; MenuItem.displayName = 'MenuItem';
export const MenuLinkItem = React.forwardRef<HTMLDivElement, MenuLinkItemProps>( export const MenuLinkItem = React.forwardRef<HTMLDivElement, MenuLinkItemProps>(
({ href, ...props }, ref) => { ({ to, ...props }, ref) => {
return ( return (
<Link href={href} className={styles.linkItemRoot}> <Link to={to} className={styles.linkItemRoot}>
{/* The <a> element rendered by Link does not generate display box due to `display: contents` style */} {/* The <a> element rendered by Link does not generate display box due to `display: contents` style */}
{/* Thus ref is passed to MenuItem instead of Link */} {/* Thus ref is passed to MenuItem instead of Link */}
<MenuItem ref={ref} {...props}></MenuItem> <MenuItem ref={ref} {...props}></MenuItem>