Skip to content

Commit 10dc958

Browse files
committed
Updated docs links to work with scoped mapper.
1 parent e77c1fb commit 10dc958

File tree

5 files changed

+126
-27
lines changed

5 files changed

+126
-27
lines changed

packages/react-renderer-demo/src/app/src/components/common/connected-links.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import ChevronLeft from '@material-ui/icons/ChevronLeft';
77
import Link from 'next/link';
88

99
import MenuContext from '../navigation/menu-context';
10+
import { useRouter } from 'next/router';
11+
import { getScopedLink, getPrefix } from '../../helpers/scoped-links';
1012

1113
const useStyles = makeStyles(() => ({
1214
linksContainer: {
@@ -22,13 +24,15 @@ const useStyles = makeStyles(() => ({
2224

2325
const ConnectedLinks = () => {
2426
const { prev, next } = useContext(MenuContext);
27+
const { pathname } = useRouter();
28+
const activeScope = getPrefix(pathname);
2529
const classNames = useStyles();
2630
return (
2731
<Grid container justify="space-between" className={classNames.linksContainer}>
2832
<Grid item>
2933
{prev && prev.link && (
30-
<Link href={`/${prev.link}`}>
31-
<a className={classNames.link} href={`/${prev.link}`}>
34+
<Link href={getScopedLink(`/${prev.link}`, activeScope)}>
35+
<a className={classNames.link} href={getScopedLink(`/${prev.link}`, activeScope)}>
3236
<Button>
3337
<ChevronLeft />
3438
{prev.label}
@@ -39,8 +43,8 @@ const ConnectedLinks = () => {
3943
</Grid>
4044
<Grid item>
4145
{next && next.link && (
42-
<Link href={`/${next.link}`}>
43-
<a className={classNames.link} href={`/${next.link}`}>
46+
<Link href={getScopedLink(`/${next.link}`, activeScope)}>
47+
<a className={classNames.link} href={getScopedLink(`/${next.link}`, activeScope)}>
4448
<Button>
4549
{next.label}
4650
<ChevronRight />

packages/react-renderer-demo/src/app/src/components/layout.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Footer from './footer';
2323

2424
import dynamic from 'next/dynamic';
2525
import NotificationPanel from './notification-panel';
26+
import MapperMenu from './mapper-menu';
27+
import { getPrefix, getScopedLink } from '../helpers/scoped-links';
2628
const DocSearch = dynamic(import('./docsearch'), {
2729
ssr: false
2830
});
@@ -140,10 +142,11 @@ const Layout = ({ children }) => {
140142
const [newMessages, setNewMessages] = useState(0);
141143

142144
useEffect(() => {
143-
setLinks(findConnectedLinks(router.asPath, flatSchema) || {});
144145
if (window && window.innerWidth > 960 && router.pathname !== '/') {
145146
setOpen(true);
146147
}
148+
149+
setLinks(findConnectedLinks(getScopedLink(router.asPath, 'mui'), flatSchema) || {});
147150
}, [router.asPath]);
148151

149152
const handleDrawerOpen = () => {
@@ -215,6 +218,7 @@ const Layout = ({ children }) => {
215218
})}
216219
>
217220
<DocSearch />
221+
<MapperMenu />
218222
<IconButton aria-label="show new notifications" onClick={handleToggle} color="inherit" ref={anchorRef} className={clsx(classes.menuButton)}>
219223
<Badge badgeContent={newMessages} color="secondary">
220224
<NotificationsIcon className={classes.menuIcons} />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React, { useState, Fragment } from 'react';
2+
import Button from '@material-ui/core/Button';
3+
import Checkbox from '@material-ui/core/Checkbox';
4+
import Menu from '@material-ui/core/Menu';
5+
import MenuItem from '@material-ui/core/MenuItem';
6+
import { useRouter } from 'next/router';
7+
import Link from 'next/link';
8+
import { makeStyles } from '@material-ui/core/styles';
9+
import { getScopedLink, getPrefix } from '../helpers/scoped-links';
10+
11+
const useStyles = makeStyles((theme) => ({
12+
menuTrigger: {
13+
color: '#fff'
14+
},
15+
menuItem: {
16+
paddingRight: 24
17+
},
18+
scopeLink: {
19+
textDecoration: 'none',
20+
color: theme.palette.text.primary
21+
}
22+
}));
23+
24+
const MapperMenu = () => {
25+
const [anchorEl, setAnchorEl] = useState(null);
26+
const router = useRouter();
27+
const classes = useStyles();
28+
const handleClick = (event) => {
29+
setAnchorEl(event.currentTarget);
30+
};
31+
32+
const handleClose = () => {
33+
setAnchorEl(null);
34+
};
35+
36+
const muiLink = getScopedLink(router.pathname, 'mui');
37+
const pf4Link = getScopedLink(router.pathname, 'pf4');
38+
const activeScope = getPrefix(router.pathname);
39+
40+
return (
41+
<div>
42+
<Button aria-controls="simple-menu" aria-haspopup="true" className={classes.menuTrigger} onClick={handleClick}>
43+
Mapper: {activeScope}
44+
</Button>
45+
<Menu id="simple-menu" anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
46+
<MenuItem className={classes.menuItem} onClick={handleClose} selected={activeScope === 'mui'}>
47+
<Link href={muiLink}>
48+
<a href={muiLink} className={classes.scopeLink}>
49+
<Checkbox checked={activeScope === 'mui'} />
50+
Material UI
51+
</a>
52+
</Link>
53+
</MenuItem>
54+
<MenuItem className={classes.menuItem} onClick={handleClose} selected={activeScope === 'pf4'}>
55+
<Link href={pf4Link}>
56+
<a href={pf4Link} className={classes.scopeLink}>
57+
<Checkbox checked={activeScope === 'pf4'} />
58+
Patternfly 4
59+
</a>
60+
</Link>
61+
</MenuItem>
62+
</Menu>
63+
</div>
64+
);
65+
};
66+
67+
export default MapperMenu;

packages/react-renderer-demo/src/app/src/components/navigation/menu-renderer.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/* eslint-disable react/prop-types */
2-
import React, { useState } from 'react';
2+
import React, { useState, useRef } from 'react';
33
import TextField from '@material-ui/core/TextField';
44
import { useRouter } from 'next/router';
55
import Mapper from './mapper';
66
import { makeStyles } from '@material-ui/core/styles';
77
import { navStyles } from './nav-styles';
8+
import { getPrefix, getScopedLink } from '../../helpers/scoped-links';
89

910
const useStyles = makeStyles(navStyles);
1011

1112
const createLink = (...args) => args.join('/');
1213

13-
const renderItems = (items, level = 0, previousLinks = ['']) => {
14+
const renderItems = (items, level = 0, previousLinks = [''], activeScope) => {
1415
if (!items) {
1516
return null;
1617
}
1718

1819
if (Array.isArray(items)) {
19-
return items.map((item) => renderItems(item, level, previousLinks));
20+
return items.map((item) => renderItems(item, level, previousLinks, activeScope));
2021
}
2122

2223
const { fields, title, link, linkText, component, open, ...props } = items;
@@ -31,26 +32,21 @@ const renderItems = (items, level = 0, previousLinks = ['']) => {
3132
link={link}
3233
level={level}
3334
previousLinks={previousLinks}
34-
renderItems={renderItems}
35+
renderItems={(...args) => renderItems(...args, activeScope)}
3536
{...props}
3637
/>
3738
);
3839
}
3940

40-
return (
41-
<Mapper.Item
42-
href={createLink(...previousLinks, link || component)}
43-
level={level}
44-
key={`${link || component}-${linkText}`}
45-
linkText={linkText}
46-
component={component}
47-
{...props}
48-
/>
49-
);
41+
const href = getScopedLink(createLink(...previousLinks, link || component), activeScope);
42+
43+
return <Mapper.Item href={href} level={level} key={`${link || component}-${linkText}`} linkText={linkText} component={component} {...props} />;
5044
};
5145

5246
const MenuRenderer = ({ schema }) => {
53-
return <React.Fragment>{renderItems(schema)}</React.Fragment>;
47+
const { pathname } = useRouter();
48+
const activeScope = getPrefix(pathname);
49+
return <React.Fragment>{renderItems(schema, undefined, undefined, activeScope)}</React.Fragment>;
5450
};
5551

5652
const searchFunction = (linkText, value) =>
@@ -98,24 +94,25 @@ const memoizeSearch = () => {
9894
};
9995
};
10096

101-
const findSelected = (schema, currentLocation, level = 1) => {
97+
const findSelected = (schema, currentLocation, level = 1, activeScope) => {
10298
if (schema.fields) {
99+
console.log({ link: schema.link, activeScope, currentLocation: currentLocation[level] });
103100
return {
104101
...schema,
105102
open: schema.link === currentLocation[level],
106103
level,
107-
fields: findSelected(schema.fields, currentLocation, level + 1)
104+
fields: findSelected(schema.fields, currentLocation, level + 1, activeScope)
108105
};
109106
}
110107

111108
if (Array.isArray(schema)) {
112-
return schema.map((field) => findSelected(field, currentLocation, level));
109+
return schema.map((field) => findSelected(field, currentLocation, level, activeScope));
113110
}
114111

115112
return schema;
116113
};
117114

118-
const memoizeCurrent = () => {
115+
const memoizeCurrent = (activeScope) => {
119116
const cache = {};
120117

121118
return (schema, currentLocation) => {
@@ -125,19 +122,20 @@ const memoizeCurrent = () => {
125122
return cache[value];
126123
}
127124

128-
cache[value] = findSelected(schema, currentLocation);
125+
cache[value] = findSelected(schema, currentLocation, undefined, activeScope);
129126
return cache[value];
130127
};
131128
};
132129

133130
const search = memoizeSearch();
134-
const current = memoizeCurrent();
135131

136132
const Menu = ({ schema, searchRef }) => {
137133
const router = useRouter();
138134
const [value, setValue] = useState('');
139135
const classes = useStyles();
140-
const currentLocation = router.pathname.split('/');
136+
const currentLocation = getScopedLink(router.pathname, 'mui').split('/');
137+
const activeScope = getPrefix(router.pathname);
138+
const { current } = useRef(memoizeCurrent(activeScope));
141139

142140
const schemaFiltered = value !== '' ? search(schema, value) : current(schema, currentLocation);
143141

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const getScopedLink = (pathname, scope) => {
2+
if (scope === 'mui') {
3+
return pathname.replace(/^\/(pf4|pf3)/, '');
4+
}
5+
6+
if (pathname.match(new RegExp(`^/${scope}/`))) {
7+
return pathname;
8+
}
9+
10+
if (!pathname.match(/^\/(pf4|pf3)/, '/')) {
11+
return `/${scope}${pathname}`;
12+
}
13+
14+
return pathname.replace(/^\/(pf4|pf3)/, `/${scope}`);
15+
};
16+
17+
export const getPrefix = (pathname) => {
18+
if (!pathname.match(/^\/(pf4|pf3)/, '/')) {
19+
return 'mui';
20+
}
21+
22+
return pathname
23+
.match(/^\/[a-z0-9]+\//)
24+
.pop()
25+
.replace(/\//g, '');
26+
};

0 commit comments

Comments
 (0)