Skip to content

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ typings/

lib
.merlin
*.bs.js
.bsb.lock
*.bs.js
19 changes: 19 additions & 0 deletions README.md
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
Copy link
Contributor

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

Copy link
Author

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 !


See [examples](./examples) folder.
7 changes: 6 additions & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"namespace": true,
"sources": [
{
"dir": "src"
"dir": "src",
"public": "all"
},
{
"dir": "examples",
"type": "dev"
}
],
"bsc-flags": ["-bs-super-errors"],
Expand Down
49 changes: 49 additions & 0 deletions examples/reason_react_router.re
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>
};
};
36 changes: 12 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"reason-react": "^0.3.0"
"reason-react": "^0.3.1"
},
"peerDependencies": {
"bs-platform": "^2.1.0",
"react-router": ">=4.2.0",
"react-router-dom": ">=4.2.0",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"reason-react": "^0.3.0"
}
}
44 changes: 41 additions & 3 deletions src/reactRouter.re
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use _to or to_? Do you know if there is a convention for such cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a good question ! My guess is to_ because _to looks like a non-used variable so I change uses.

children
) =>
ReasonReact.wrapJsForReason(
~reactClass=link,
~props={
"to": _to
},
children
);
};

module Redirect = {
[@bs.module "react-router-dom"]
external reactClass : ReasonReact.reactClass = "Redirect";
Expand All @@ -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
);
};
Expand All @@ -78,4 +116,4 @@ module ServerRouter = {
~props={"context": context, "location": location},
children
);
};
};