-
Notifications
You must be signed in to change notification settings - Fork 7
Add Switch and Link components #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9dbd929
f2d68f8
c7756ef
a14f32b
fb86f05
fce3ee1
89895fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,4 +66,5 @@ typings/ | |
|
||
lib | ||
.merlin | ||
*.bs.js | ||
.bsb.lock | ||
*.bs.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
# Deprecated | ||
|
||
This is the Reason interop wrapper for [React Router](https://reacttraining.com/react-router/). **Note** that ReasonReact itself [already comes with a router](https://reasonml.github.io/reason-react/docs/en/router.html). | ||
|
||
## Install | ||
|
||
```bash | ||
npm install --save bs-react-router | ||
``` | ||
|
||
Add `bs-react-router` to your `bs-dependencies`: **bsconfig.json** | ||
|
||
```bash | ||
{ | ||
... | ||
"bs-dependencies": ["bs-react-router"] | ||
} | ||
``` | ||
|
||
## Example | ||
|
||
See [examples](./examples) folder. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
open ReactRouter; | ||
|
||
module HomePage = { | ||
let component = ReasonReact.statelessComponent("HomePage"); | ||
let make = (_children) => { | ||
...component, | ||
render: (_self) => | ||
<h1>{ReasonReact.stringToElement("Home page")}</h1> | ||
} | ||
}; | ||
|
||
module UserPage = { | ||
let component = ReasonReact.statelessComponent("UserPage"); | ||
let make = (_children) => { | ||
...component, | ||
render: (_self) => | ||
<h1>{ReasonReact.stringToElement("User page")}</h1> | ||
} | ||
}; | ||
|
||
module App = { | ||
let component = ReasonReact.statelessComponent("App"); | ||
let blueStyle = ReactDOMRe.Style.make(~color="#000099", ()); | ||
|
||
let make = (_children) => { | ||
...component, | ||
render: (_self) => | ||
<div> | ||
<BrowserRouter> | ||
<div> | ||
<h1>{ReasonReact.stringToElement("Using NavLink")}</h1> | ||
<NavLink _to="/">{ReasonReact.stringToElement("Home")}</NavLink> | ||
<NavLink _to="/user" style=blueStyle>{ReasonReact.stringToElement("User")}</NavLink> | ||
|
||
<h1>{ReasonReact.stringToElement("Using Link")}</h1> | ||
<Link _to="/">{ReasonReact.stringToElement("Home")}</Link> | ||
<Link _to="/user">{ReasonReact.stringToElement("User")}</Link> | ||
|
||
<Switch> | ||
/* Component as Prop can't trivially pass | ||
so we pass a callback as a component to send the whole component */ | ||
<Route path="/" exact=true component=(() => <HomePage />) /> | ||
<Route path="/user" component=(() => <UserPage />) /> | ||
</Switch> | ||
</div> | ||
</BrowserRouter> | ||
</div> | ||
}; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,32 @@ module Route = { | |
); | ||
}; | ||
|
||
module Switch = { | ||
[@bs.module "react-router-dom"] external reactClass : ReasonReact.reactClass = "Switch"; | ||
let make = (children) => | ||
ReasonReact.wrapJsForReason( | ||
~reactClass, | ||
~props=Js.Obj.empty(), | ||
children | ||
); | ||
}; | ||
|
||
module Link = { | ||
[@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; | ||
let make = | ||
( | ||
~_to: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was a good question ! My guess is |
||
children | ||
) => | ||
ReasonReact.wrapJsForReason( | ||
~reactClass=link, | ||
~props={ | ||
"to": _to | ||
}, | ||
children | ||
); | ||
}; | ||
|
||
module Redirect = { | ||
[@bs.module "react-router-dom"] | ||
external reactClass : ReasonReact.reactClass = "Redirect"; | ||
|
@@ -50,10 +76,22 @@ module Redirect = { | |
module NavLink = { | ||
[@bs.module "react-router-dom"] | ||
external navLink : ReasonReact.reactClass = "NavLink"; | ||
let make = (~_to: string, children) => | ||
let make = | ||
( | ||
~_to: string, | ||
~activeClassName: option(string)=?, | ||
~style: option(ReactDOMRe.style)=?, | ||
~activeStyle: option(ReactDOMRe.style)=?, | ||
children | ||
) => | ||
ReasonReact.wrapJsForReason( | ||
~reactClass=navLink, | ||
~props={"to": _to}, | ||
~props={ | ||
"to": _to, | ||
"activeClassName": Js.Null_undefined.from_opt(activeClassName), | ||
"style": Js.Null_undefined.from_opt(style), | ||
"activeStyle": Js.Null_undefined.from_opt(activeStyle) | ||
}, | ||
children | ||
); | ||
}; | ||
|
@@ -78,4 +116,4 @@ module ServerRouter = { | |
~props={"context": context, "location": location}, | ||
children | ||
); | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: we can move the examples to actual
*.re
files, you know for sure then if it compiles ;)E.g. https://github.com/reasonml-community/bs-downshift/tree/master/examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is a very good point ! Good catch !