Skip to content

Commit

Permalink
Merge pull request #32 from commitdev/react
Browse files Browse the repository at this point in the history
Adding views and routes
  • Loading branch information
kb-commit committed Oct 17, 2019
2 parents 025479a + 983fdb2 commit 9a9f97f
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 15 deletions.
13 changes: 13 additions & 0 deletions config/react.go
Expand Up @@ -8,17 +8,30 @@ type reactHeader struct {
Enabled bool
}

type reactSidenavItem struct {
Path string
Label string
Icon string
}
type reactSidenav struct {
Enabled bool
Items []reactSidenavItem
}

type reactAccount struct {
Enabled bool
Required bool
}

type reactView struct {
Path string
Component string
}

type React struct {
App reactApp
Account reactAccount
Header reactHeader
Sidenav reactSidenav
Views []reactView
}
13 changes: 12 additions & 1 deletion templates/commit0/commit0.tmpl
Expand Up @@ -28,5 +28,16 @@ react:
required: false
sidenav:
enabled: true

items:
- path: /
label: Home
icon: home
- path: /account
label: Account
icon: account_circle
views:
- path: /account
component: account
- path: /
component: home
services:
1 change: 1 addition & 0 deletions templates/react/public/index.html
Expand Up @@ -10,6 +10,7 @@
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand Down
44 changes: 44 additions & 0 deletions templates/react/public/index.html.tmpl
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>{{ .React.App.Name }}</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
21 changes: 12 additions & 9 deletions templates/react/src/App.js
@@ -1,21 +1,24 @@
import React from 'react';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Layout from 'components/layout';
import config from 'config';

const renderView = (view) => {
return (
<Route path={`${view.path}`} component={require(`views/${view.component}`).default} />
)
}

export default function App() {


return (
<Layout>
<Router>
<Switch>
<Route path="/a">
<span>a</span>
</Route>
<Route path="/b">
<span>b</span>
</Route>
<Route path="/">
<span>c</span>
</Route>
{
config.views && config.views.map(renderView)
}
</Switch>
</Router>
</Layout>
Expand Down
4 changes: 2 additions & 2 deletions templates/react/src/components/layout/header/account.js
@@ -1,11 +1,11 @@
import React from 'react';
import React, { useState } from 'react';
import IconButton from '@material-ui/core/IconButton';
import AccountCircle from '@material-ui/icons/AccountCircle';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';

export default function MenuAppBar() {
const [anchorEl, setAnchorEl] = React.useState(null);
const [anchorEl, setAnchorEl] = useState(null);
const open = Boolean(anchorEl);

const handleMenu = event => {
Expand Down
37 changes: 37 additions & 0 deletions templates/react/src/components/layout/header/sidenav/content.js
@@ -0,0 +1,37 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Icon from '@material-ui/core/Icon';
import config from 'config';

const useStyles = makeStyles({
list: {
width: 250,
},
});

export default function Content() {
const classes = useStyles();
return (
<div
className={classes.list}
role="presentation"
>
<List>
{
config.sidenav && config.sidenav.items && config.sidenav.items.map((item, index) => (
<ListItem button component="a" href={item.path} key={index}>
<ListItemIcon>
<Icon>{item.icon}</Icon>
</ListItemIcon>
<ListItemText primary={item.label} />
</ListItem>
))
}
</List>
</div>
);
}
@@ -1,7 +1,9 @@
import React, { Fragment } from 'react';
import React, { Fragment, useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import Drawer from '@material-ui/core/Drawer';
import Content from 'components/layout/header/sidenav/content';

const useStyles = makeStyles(theme => ({
menuButton: {
Expand All @@ -11,16 +13,25 @@ const useStyles = makeStyles(theme => ({

export default function Sidenav() {
const classes = useStyles();
const [open, setOpen] = useState(false);

const onClose = () => setOpen(false);
const onOpen = () => setOpen(true);

return (
<Fragment>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="open drawer"
onClick={onOpen}
>
<MenuIcon />
</IconButton>
<Drawer open={open} onClose={onClose}>
<Content />
</Drawer>
</Fragment>
);
}
24 changes: 23 additions & 1 deletion templates/react/src/config/index.js
Expand Up @@ -11,5 +11,27 @@ export default {
},
sidenav: {
enabled: true,
}
items: [
{
path: '/',
label: 'Home',
icon: 'home',
},
{
path: '/account',
label: 'Account',
icon: 'account_circle',
},
],
},
views: [
{
path: '/account',
component: 'account',
},
{
path: '/',
component: 'home',
},
],
}
19 changes: 18 additions & 1 deletion templates/react/src/config/index.js.tmpl
Expand Up @@ -11,5 +11,22 @@ export default {
},
sidenav: {
enabled: {{ .React.Sidenav.Enabled }},
}
items: [
{{ range .React.Sidenav.Items }}
{
path: '{{ .Path }}',
label: '{{ .Label }}',
icon: '{{ .Icon }}',
},
{{ end }}
]
},
views: [
{{ range .React.Views }}
{
path: '{{ .Path }}',
component: '{{ .Component }}',
},
{{ end }}
],
}
9 changes: 9 additions & 0 deletions templates/react/src/views/account.js
@@ -0,0 +1,9 @@
import React from 'react';

export default function Account() {
return (
<div>
Account
</div>
);
}
9 changes: 9 additions & 0 deletions templates/react/src/views/home.js
@@ -0,0 +1,9 @@
import React from 'react';

export default function Home() {
return (
<div>
Home
</div>
);
}

0 comments on commit 9a9f97f

Please sign in to comment.