-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Found Basic JSX Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "basic-jsx", | ||
"version": "0.1.0", | ||
"private": true, | ||
"devDependencies": { | ||
"react-scripts": "0.7.0" | ||
}, | ||
"dependencies": { | ||
"found": "../..", | ||
"react": "^15.3.2", | ||
"react-dom": "^15.3.2" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Found Basic JSX Example</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import createBrowserRouter from 'found/lib/createBrowserRouter'; | ||
import Link from 'found/lib/Link'; | ||
import makeRouteConfig from 'found/lib/jsx/makeRouteConfig'; | ||
import Redirect from 'found/lib/jsx/Redirect'; | ||
import Route from 'found/lib/jsx/Route'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
function LinkItem(props) { | ||
// TODO: Remove the pragma once evcohen/eslint-plugin-jsx-a11y#81 lands. | ||
return ( | ||
<li> | ||
<Link // eslint-disable-line jsx-a11y/anchor-has-content | ||
{...props} | ||
activeStyle={{ fontWeight: 'bold' }} | ||
/> | ||
</li> | ||
); | ||
} | ||
|
||
const propTypes = { | ||
children: React.PropTypes.node, | ||
}; | ||
|
||
function App({ children }) { | ||
return ( | ||
<div> | ||
<ul> | ||
<LinkItem to="/"> | ||
Main | ||
</LinkItem> | ||
<ul> | ||
<LinkItem to="/foo"> | ||
Foo | ||
</LinkItem> | ||
<LinkItem to="/bar"> | ||
Bar (async) | ||
</LinkItem> | ||
<LinkItem to="/baz"> | ||
Baz (redirects to Foo) | ||
</LinkItem> | ||
<LinkItem to="/qux"> | ||
Qux (missing) | ||
</LinkItem> | ||
</ul> | ||
</ul> | ||
|
||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
App.propTypes = propTypes; | ||
|
||
const BrowserRouter = createBrowserRouter({ | ||
routeConfig: makeRouteConfig( | ||
<Route | ||
path="/" | ||
Component={App} | ||
> | ||
<Route | ||
Component={() => <div>Main</div>} | ||
/> | ||
<Route | ||
path="foo" | ||
Component={() => <div>Foo</div>} | ||
/> | ||
<Route | ||
path="bar" | ||
getComponent={() => new Promise((resolve) => { | ||
setTimeout(resolve, 1000, ({ data }) => <div>{data}</div>); | ||
})} | ||
getData={() => new Promise((resolve) => { | ||
setTimeout(resolve, 1000, 'Bar'); | ||
})} | ||
render={({ Component, props }) => ( | ||
Component && props ? ( | ||
<Component {...props} /> | ||
) : ( | ||
<div><small>Loading…</small></div> | ||
) | ||
)} | ||
/> | ||
<Redirect | ||
from="baz" | ||
to="/foo" | ||
/> | ||
</Route> | ||
), | ||
}); | ||
|
||
ReactDOM.render( | ||
<BrowserRouter | ||
renderError={error => ( | ||
<div> | ||
{error.status === 404 ? 'Not found' : 'Error'} | ||
</div> | ||
)} | ||
/>, | ||
document.getElementById('root'), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import invariant from 'invariant'; | ||
|
||
import RedirectObject from '../Redirect'; | ||
|
||
function createRoute(props) { | ||
return new RedirectObject(props); | ||
} | ||
|
||
function Redirect() { | ||
invariant( | ||
false, | ||
'<Redirect> is only for configuration and should not be rendered.', | ||
); | ||
} | ||
|
||
Redirect.createRoute = createRoute; | ||
|
||
export default Redirect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import invariant from 'invariant'; | ||
|
||
function createRoute(props) { | ||
return props; | ||
} | ||
|
||
function Route() { | ||
invariant( | ||
false, | ||
'<Route> is only for configuration and should not be rendered.', | ||
); | ||
} | ||
|
||
Route.createRoute = createRoute; | ||
|
||
export default Route; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
|
||
export default function makeRouteConfig(node) { | ||
return React.Children.toArray(node) | ||
.filter(React.isValidElement) | ||
.map(({ type, props: { children, ...props } }) => { | ||
const route = type.createRoute(props); | ||
|
||
if (children) { | ||
route.children = makeRouteConfig(children); | ||
} | ||
|
||
return route; | ||
}); | ||
} |