From 9dbd9299af932b7d58cc1958b826f4f627ad10ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20THOMAS?= Date: Wed, 20 Dec 2017 10:49:45 -0500 Subject: [PATCH 1/5] Add Switch and Link components --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++ package-lock.json | 2 +- package.json | 10 +++++----- src/reactRouter.re | 50 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 97 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 36e4862..832dc91 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,48 @@ # NOT READY will eventually be [Reason](https://reasonml.github.io) bindings to [React Router](https://reacttraining.com/react-router/) + +## Install + +```bash +npm install --save bs-react-router +``` + +Then add bs-react-router to bs-dependencies in your bsconfig.json: + +```bash +{ + ... + "bs-dependencies": ["bs-react-router"] +} +``` + +## Example + +```jsx +let component = ReasonReact.statelessComponent("App"); +let blueStyle = ReactDOMRe.Style.make(~color="#000099", ()); + +let make = (_children) => { + ...component, + render: (_self) => +
+ +
+

{ReasonReact.stringToElement("Using NavLink")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + +

{ReasonReact.stringToElement("Using Link")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + + + ) /> + ) /> + +
+
+
+}; +``` \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c5496c0..30ea3b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bs-react-router", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a44bff6..368a067 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bs-react-router", - "version": "1.1.1", + "version": "1.1.2", "description": "", "main": "index.js", "scripts": { @@ -24,13 +24,13 @@ "bs-platform": "^2.0.0", "react": "^15.4.0", "react-dom": "^15.4.2", - "react-router": "^4.1.1", - "react-router-dom": "^4.1.1", + "react-router": "^4.2.0", + "react-router-dom": "^4.2.2", "reason-react": "^0.3.0" }, "peerDependencies": { - "react-router": "^4.1.1", - "react-router-dom": "^4.1.1", + "react-router": "^4.2.0", + "react-router-dom": "^4.2.2", "reason-react": "^0.3.0" } } diff --git a/src/reactRouter.re b/src/reactRouter.re index 11f8498..6798a98 100644 --- a/src/reactRouter.re +++ b/src/reactRouter.re @@ -1,4 +1,4 @@ -type renderFunc = +type renderFunc = { . "_match": Js.Dict.t(string), @@ -7,7 +7,7 @@ type renderFunc = } => ReasonReact.reactElement; -let optionToBool = (optional) => +let optionToBool = (optional) => switch optional { | Some(_) => true | _ => false @@ -35,10 +35,52 @@ module Route = { ); }; +module Switch = { + [@bs.module "react-router-dom"] external _switch : ReasonReact.reactClass = "Switch"; + let make = (children) => + ReasonReact.wrapJsForReason( + ~reactClass=_switch, + ~props=Js.Obj.empty(), + children + ); +}; + +module Link = { + [@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; + let make = + ( + ~_to: string, + children + ) => + ReasonReact.wrapJsForReason( + ~reactClass=link, + ~props={ + "to": _to + }, + children + ); +}; + module NavLink = { [@bs.module "react-router-dom"] external navLink : ReasonReact.reactClass = "NavLink"; - let make = (~_to: string, children) => - ReasonReact.wrapJsForReason(~reactClass=navLink, ~props={"to": _to}, children); + let make = + ( + ~_to: string, + ~activeClassName: option(string)=?, + ~style: option(ReactDOMRe.style)=?, + ~activeStyle: option(ReactDOMRe.style)=?, + children + ) => + ReasonReact.wrapJsForReason( + ~reactClass=navLink, + ~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 + ); }; module BrowserRouter = { From f2d68f8707d2de46d02149f233d1befe654117a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20THOMAS?= Date: Tue, 9 Jan 2018 20:43:00 -0500 Subject: [PATCH 2/5] PR corrections --- .gitignore | 3 ++- README.md | 30 ++------------------- bsconfig.json | 7 ++++- examples/reason_react_router.re | 47 +++++++++++++++++++++++++++++++++ package.json | 6 ++--- src/reactRouter.re | 12 ++++----- 6 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 examples/reason_react_router.re diff --git a/.gitignore b/.gitignore index 3e9ed7b..1b278f9 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,5 @@ typings/ # End of https://www.gitignore.io/api/node lib -.merlin \ No newline at end of file +.merlin +.bsb.lock \ No newline at end of file diff --git a/README.md b/README.md index 832dc91..099ea76 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ will eventually be [Reason](https://reasonml.github.io) bindings to [React Route npm install --save bs-react-router ``` -Then add bs-react-router to bs-dependencies in your bsconfig.json: +Add `bs-react-router` to your `bs-dependencies`: **bsconfig.json** ```bash { @@ -19,30 +19,4 @@ Then add bs-react-router to bs-dependencies in your bsconfig.json: ## Example -```jsx -let component = ReasonReact.statelessComponent("App"); -let blueStyle = ReactDOMRe.Style.make(~color="#000099", ()); - -let make = (_children) => { - ...component, - render: (_self) => -
- -
-

{ReasonReact.stringToElement("Using NavLink")}

- {ReasonReact.stringToElement("Home")} - {ReasonReact.stringToElement("User")} - -

{ReasonReact.stringToElement("Using Link")}

- {ReasonReact.stringToElement("Home")} - {ReasonReact.stringToElement("User")} - - - ) /> - ) /> - -
-
-
-}; -``` \ No newline at end of file +See [examples](./examples) folder. \ No newline at end of file diff --git a/bsconfig.json b/bsconfig.json index dbb163d..9cdc6db 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -3,7 +3,12 @@ "namespace": true, "sources": [ { - "dir": "src" + "dir": "src", + "public": "all" + }, + { + "dir": "examples", + "type": "dev" } ], "refmt": 3, diff --git a/examples/reason_react_router.re b/examples/reason_react_router.re new file mode 100644 index 0000000..da88af5 --- /dev/null +++ b/examples/reason_react_router.re @@ -0,0 +1,47 @@ +open ReactRouter; + +module HomePage = { + let component = ReasonReact.statelessComponent("HomePage"); + let make = (_children) => { + ...component, + render: (_self) => +

{ReasonReact.stringToElement("Home page")}

+ } +}; + +module UserPage = { + let component = ReasonReact.statelessComponent("UserPage"); + let make = (_children) => { + ...component, + render: (_self) => +

{ReasonReact.stringToElement("User page")}

+ } +}; + +module App = { + let component = ReasonReact.statelessComponent("App"); + let blueStyle = ReactDOMRe.Style.make(~color="#000099", ()); + + let make = (_children) => { + ...component, + render: (_self) => +
+ +
+

{ReasonReact.stringToElement("Using NavLink")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + +

{ReasonReact.stringToElement("Using Link")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + + + ) /> + ) /> + +
+
+
+ }; +}; \ No newline at end of file diff --git a/package.json b/package.json index 368a067..4d02dcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bs-react-router", - "version": "1.1.2", + "version": "1.1.1", "description": "", "main": "index.js", "scripts": { @@ -29,8 +29,8 @@ "reason-react": "^0.3.0" }, "peerDependencies": { - "react-router": "^4.2.0", - "react-router-dom": "^4.2.2", + "react-router": "^4.1.1", + "react-router-dom": "^4.1.1", "reason-react": "^0.3.0" } } diff --git a/src/reactRouter.re b/src/reactRouter.re index 6798a98..eb16f2a 100644 --- a/src/reactRouter.re +++ b/src/reactRouter.re @@ -36,10 +36,10 @@ module Route = { }; module Switch = { - [@bs.module "react-router-dom"] external _switch : ReasonReact.reactClass = "Switch"; + [@bs.module "react-router-dom"] external reactClass : ReasonReact.reactClass = "Switch"; let make = (children) => ReasonReact.wrapJsForReason( - ~reactClass=_switch, + ~reactClass, ~props=Js.Obj.empty(), children ); @@ -49,13 +49,13 @@ module Link = { [@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; let make = ( - ~_to: string, + ~to_: string, children ) => ReasonReact.wrapJsForReason( ~reactClass=link, ~props={ - "to": _to + "to": to_ }, children ); @@ -65,7 +65,7 @@ module NavLink = { [@bs.module "react-router-dom"] external navLink : ReasonReact.reactClass = "NavLink"; let make = ( - ~_to: string, + ~to_: string, ~activeClassName: option(string)=?, ~style: option(ReactDOMRe.style)=?, ~activeStyle: option(ReactDOMRe.style)=?, @@ -74,7 +74,7 @@ module NavLink = { ReasonReact.wrapJsForReason( ~reactClass=navLink, ~props={ - "to": _to, + "to": to_, "activeClassName": Js.Null_undefined.from_opt(activeClassName), "style": Js.Null_undefined.from_opt(style), "activeStyle": Js.Null_undefined.from_opt(activeStyle) From a14f32bbf9b10ee89bba40aaf838149fff5408b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20THOMAS?= Date: Wed, 20 Dec 2017 10:49:45 -0500 Subject: [PATCH 3/5] Add Switch and Link components --- .gitignore | 3 +- README.md | 19 +++++++++++++ bsconfig.json | 7 ++++- examples/reason_react_router.re | 49 +++++++++++++++++++++++++++++++++ package-lock.json | 34 ++++++++--------------- package.json | 4 +-- src/reactRouter.re | 44 +++++++++++++++++++++++++++-- 7 files changed, 130 insertions(+), 30 deletions(-) create mode 100644 examples/reason_react_router.re diff --git a/.gitignore b/.gitignore index f49acea..ac8d13d 100644 --- a/.gitignore +++ b/.gitignore @@ -66,4 +66,5 @@ typings/ lib .merlin -*.bs.js \ No newline at end of file +.bsb.lock +*.bs.js diff --git a/README.md b/README.md index 59079dd..f231062 100644 --- a/README.md +++ b/README.md @@ -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**git status + +```bash +{ + ... + "bs-dependencies": ["bs-react-router"] +} +``` + +## Example + +See [examples](./examples) folder. diff --git a/bsconfig.json b/bsconfig.json index cdc3421..2e5abce 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -3,7 +3,12 @@ "namespace": true, "sources": [ { - "dir": "src" + "dir": "src", + "public": "all" + }, + { + "dir": "examples", + "type": "dev" } ], "bsc-flags": ["-bs-super-errors"], diff --git a/examples/reason_react_router.re b/examples/reason_react_router.re new file mode 100644 index 0000000..cb932b7 --- /dev/null +++ b/examples/reason_react_router.re @@ -0,0 +1,49 @@ +open ReactRouter; + +module HomePage = { + let component = ReasonReact.statelessComponent("HomePage"); + let make = (_children) => { + ...component, + render: (_self) => +

{ReasonReact.stringToElement("Home page")}

+ } +}; + +module UserPage = { + let component = ReasonReact.statelessComponent("UserPage"); + let make = (_children) => { + ...component, + render: (_self) => +

{ReasonReact.stringToElement("User page")}

+ } +}; + +module App = { + let component = ReasonReact.statelessComponent("App"); + let blueStyle = ReactDOMRe.Style.make(~color="#000099", ()); + + let make = (_children) => { + ...component, + render: (_self) => +
+ +
+

{ReasonReact.stringToElement("Using NavLink")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + +

{ReasonReact.stringToElement("Using Link")}

+ {ReasonReact.stringToElement("Home")} + {ReasonReact.stringToElement("User")} + + + /* Component as Prop can't trivially pass + so we pass a callback as a component to send the whole component */ + ) /> + ) /> + +
+
+
+ }; +}; diff --git a/package-lock.json b/package-lock.json index c5496c0..31d8dfe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,17 +22,6 @@ "integrity": "sha1-ZSKUwUZR2yj6k70tX/KYOk8IxjY=", "dev": true }, - "create-react-class": { - "version": "15.6.2", - "resolved": "https://registry.npmjs.org/create-react-class/-/create-react-class-15.6.2.tgz", - "integrity": "sha1-zx7RXxKq1/FO9fLf4F5sQvke8Co=", - "dev": true, - "requires": { - "fbjs": "0.8.16", - "loose-envify": "1.3.1", - "object-assign": "4.1.1" - } - }, "encoding": { "version": "0.1.12", "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", @@ -174,12 +163,11 @@ } }, "react": { - "version": "15.6.2", - "resolved": "https://registry.npmjs.org/react/-/react-15.6.2.tgz", - "integrity": "sha1-26BDSrQ5z+gvEI8PURZjkIF5qnI=", + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/react/-/react-16.2.0.tgz", + "integrity": "sha512-ZmIomM7EE1DvPEnSFAHZn9Vs9zJl5A9H7el0EGTE6ZbW9FKe/14IYAlPbC8iH25YarEQxZL+E8VW7Mi7kfQrDQ==", "dev": true, "requires": { - "create-react-class": "15.6.2", "fbjs": "0.8.16", "loose-envify": "1.3.1", "object-assign": "4.1.1", @@ -187,9 +175,9 @@ } }, "react-dom": { - "version": "15.6.2", - "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-15.6.2.tgz", - "integrity": "sha1-Qc+t9pO3V/rycIRDodH9WgK+9zA=", + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.2.0.tgz", + "integrity": "sha512-zpGAdwHVn9K0091d+hr+R0qrjoJ84cIBFL2uU60KvWBPfZ7LPSrfqviTxGHWN0sjPZb2hxWzMexwrvJdKePvjg==", "dev": true, "requires": { "fbjs": "0.8.16", @@ -228,13 +216,13 @@ } }, "reason-react": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/reason-react/-/reason-react-0.3.0.tgz", - "integrity": "sha512-8PSJbLY+zv3x9gyQMnoMGEjSL+OTkK92ukjRZSMbF4lOsdFatpaxB7P8ybuQnMtpnRN6JCFPBddumHoBtABlQA==", + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/reason-react/-/reason-react-0.3.1.tgz", + "integrity": "sha512-T5BPJw/D/ezUAVjB2C4awpj4J88Zrp1XhV3z4xe6n0bdBBctXY2HV5G7AmberzABaspIVQnsKmfkd+fl+T613w==", "dev": true, "requires": { - "react": "15.6.2", - "react-dom": "15.6.2" + "react": "16.2.0", + "react-dom": "16.2.0" } }, "resolve-pathname": { diff --git a/package.json b/package.json index df17a8c..61d9362 100644 --- a/package.json +++ b/package.json @@ -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", - "reason-react": "^0.3.0" + "reason-react": "^0.3.1" } } diff --git a/src/reactRouter.re b/src/reactRouter.re index e4008f9..a625ddf 100644 --- a/src/reactRouter.re +++ b/src/reactRouter.re @@ -40,6 +40,32 @@ module Route = { ); }; +module Switch = { + [@bs.module "react-router-dom"] external _switch : ReasonReact.reactClass = "Switch"; + let make = (children) => + ReasonReact.wrapJsForReason( + ~reactClass=_switch, + ~props=Js.Obj.empty(), + children + ); +}; + +module Link = { + [@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; + let make = + ( + ~_to: string, + 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 ); -}; \ No newline at end of file +}; From fb86f0599b570e60b685e20b9d5eac9b02b85f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20THOMAS?= Date: Tue, 9 Jan 2018 20:43:00 -0500 Subject: [PATCH 4/5] PR corrections --- README.md | 2 +- package.json | 6 +++--- src/reactRouter.re | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f231062..f29bab7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is the Reason interop wrapper for [React Router](https://reacttraining.com/ npm install --save bs-react-router ``` -Add `bs-react-router` to your `bs-dependencies`: **bsconfig.json**git status +Add `bs-react-router` to your `bs-dependencies`: **bsconfig.json** ```bash { diff --git a/package.json b/package.json index 61d9362..d835a88 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ }, "peerDependencies": { "bs-platform": "^2.1.0", - "react-router": ">=4.2.0", - "react-router-dom": ">=4.2.0", - "reason-react": "^0.3.1" + "react-router": "^4.1.1", + "react-router-dom": "^4.1.1", + "reason-react": "^0.3.0" } } diff --git a/src/reactRouter.re b/src/reactRouter.re index a625ddf..7d56f5f 100644 --- a/src/reactRouter.re +++ b/src/reactRouter.re @@ -41,10 +41,10 @@ module Route = { }; module Switch = { - [@bs.module "react-router-dom"] external _switch : ReasonReact.reactClass = "Switch"; + [@bs.module "react-router-dom"] external reactClass : ReasonReact.reactClass = "Switch"; let make = (children) => ReasonReact.wrapJsForReason( - ~reactClass=_switch, + ~reactClass, ~props=Js.Obj.empty(), children ); @@ -54,13 +54,13 @@ module Link = { [@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; let make = ( - ~_to: string, + ~to_: string, children ) => ReasonReact.wrapJsForReason( ~reactClass=link, ~props={ - "to": _to + "to": to_ }, children ); @@ -87,7 +87,7 @@ module NavLink = { ReasonReact.wrapJsForReason( ~reactClass=navLink, ~props={ - "to": _to, + "to": to_, "activeClassName": Js.Null_undefined.from_opt(activeClassName), "style": Js.Null_undefined.from_opt(style), "activeStyle": Js.Null_undefined.from_opt(activeStyle) From fce3ee1cd412fa5da9d8fe4f0fe326191498141a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20THOMAS?= Date: Mon, 22 Jan 2018 09:37:58 -0500 Subject: [PATCH 5/5] Rewrite name attributes --- src/reactRouter.re | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reactRouter.re b/src/reactRouter.re index 7d56f5f..0526e12 100644 --- a/src/reactRouter.re +++ b/src/reactRouter.re @@ -54,13 +54,13 @@ module Link = { [@bs.module "react-router-dom"] external link : ReasonReact.reactClass = "Link"; let make = ( - ~to_: string, + ~_to: string, children ) => ReasonReact.wrapJsForReason( ~reactClass=link, ~props={ - "to": to_ + "to": _to }, children ); @@ -87,7 +87,7 @@ module NavLink = { ReasonReact.wrapJsForReason( ~reactClass=navLink, ~props={ - "to": to_, + "to": _to, "activeClassName": Js.Null_undefined.from_opt(activeClassName), "style": Js.Null_undefined.from_opt(style), "activeStyle": Js.Null_undefined.from_opt(activeStyle)