Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
import * as React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { Link as RouterLink, MemoryRouter, StaticRouter } from 'react-router';
|
|
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
|
import Button from '@mui/material/Button';
|
|
import Stack from '@mui/material/Stack';
|
|
import Link from '@mui/material/Link';
|
|
|
|
const LinkBehavior = React.forwardRef((props, ref) => {
|
|
const { href, ...other } = props;
|
|
// Map href (MUI) -> to (react-router)
|
|
return <RouterLink data-testid="custom-link" ref={ref} to={href} {...other} />;
|
|
});
|
|
|
|
LinkBehavior.propTypes = {
|
|
href: PropTypes.oneOfType([
|
|
PropTypes.shape({
|
|
hash: PropTypes.string,
|
|
pathname: PropTypes.string,
|
|
search: PropTypes.string,
|
|
}),
|
|
PropTypes.string,
|
|
]).isRequired,
|
|
};
|
|
|
|
function Router(props) {
|
|
const { children } = props;
|
|
if (typeof window === 'undefined') {
|
|
return <StaticRouter location="/">{children}</StaticRouter>;
|
|
}
|
|
|
|
return <MemoryRouter>{children}</MemoryRouter>;
|
|
}
|
|
|
|
Router.propTypes = {
|
|
children: PropTypes.node,
|
|
};
|
|
|
|
const theme = createTheme({
|
|
components: {
|
|
MuiLink: {
|
|
defaultProps: {
|
|
component: LinkBehavior,
|
|
},
|
|
},
|
|
MuiButtonBase: {
|
|
defaultProps: {
|
|
LinkComponent: LinkBehavior,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
export default function LinkRouterWithTheme() {
|
|
return (
|
|
<Stack spacing={1} sx={{ alignItems: 'center', typography: 'body1' }}>
|
|
<ThemeProvider theme={theme}>
|
|
<Router>
|
|
<Link href="/">Link</Link>
|
|
<Button href="/" variant="contained">
|
|
Link
|
|
</Button>
|
|
</Router>
|
|
</ThemeProvider>
|
|
</Stack>
|
|
);
|
|
}
|