This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add full support for wrapping JS component, and examples.
The old way of wrapping components is now deprecated.
- Loading branch information
1 parent
4037e4f
commit 50b8e87
Showing
18 changed files
with
443 additions
and
164 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
/* The @genType annotation is ignored if there's also a .rei file */ | ||
[@genType] [@bs.module] | ||
external myBanner: ReasonReact.reactClass = "./MyBanner"; | ||
/** | ||
* Wrap component MyBanner to be used from Reason. | ||
*/ | ||
[@genType.import "./MyBanner"] /* Module with the JS component to be wrapped. */ | ||
[@bs.module "./MyBannerRe.re"] /* This must always be the name of the current module. */ | ||
/* The make function will be automatically generated from the types below. */ | ||
external make: | ||
(~show: bool, ~message: Js.Nullable.t(string), 'a) => | ||
ReasonReact.component( | ||
ReasonReact.stateless, | ||
ReasonReact.noRetainedProps, | ||
ReasonReact.actionless, | ||
) = | ||
""; | ||
|
||
[@bs.deriving abstract] | ||
type jsProps = { | ||
show: bool, | ||
message: Js.Nullable.t(string), | ||
}; | ||
|
||
/* The @genType annotation is ignored if there's also a .rei file */ | ||
[@genType] | ||
let make = (~show, ~message, children) => | ||
ReasonReact.wrapJsForReason( | ||
~reactClass=myBanner, | ||
~props=jsProps(~show, ~message), | ||
children, | ||
); | ||
let make = make; |
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 was deleted.
Oops, something went wrong.
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,25 @@ | ||
import * as React from "react"; | ||
import "./App.css"; | ||
|
||
// tslint:disable-next-line:interface-name | ||
export interface Props { | ||
show: boolean; | ||
message?: { text: string }; | ||
} | ||
|
||
class App extends React.PureComponent<Props> { | ||
public render() { | ||
if (this.props.show) { | ||
return ( | ||
<div className="App"> | ||
{"Here's the message from the owner: " + | ||
(this.props.message === undefined ? "3" : this.props.message.text)} | ||
</div> | ||
); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
export default App; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,23 +1,20 @@ | ||
/** | ||
* ReactJS used by ReasonReact | ||
* This component wraps a ReactJS one, so that ReasonReact components can consume it | ||
* Typing the myBanner.js component's output as a `reactClass`. | ||
*/ | ||
* Wrap component MyBanner to be used from Reason. | ||
*/ | ||
|
||
[@genType] | ||
[@bs.module] | ||
external myBanner : ReasonReact.reactClass = "./MyBanner"; | ||
type message = {text: string}; | ||
|
||
[@bs.deriving abstract] | ||
type jsProps = { | ||
show: bool, | ||
message: string, | ||
}; | ||
[@genType.import "./MyBanner"] /* Module with the JS component to be wrapped. */ | ||
[@bs.module "./MyBannerRe"] /* This must always be the name of the current module. */ | ||
/* The make function will be automatically generated from the types below. */ | ||
external make: | ||
(~show: bool, ~message: option(message)=?, 'a) => | ||
ReasonReact.component( | ||
ReasonReact.stateless, | ||
ReasonReact.noRetainedProps, | ||
ReasonReact.actionless, | ||
) = | ||
""; | ||
|
||
/** This is like declaring a normal ReasonReact component's `make` function, except the body is a the interop hook wrapJsForReason */ | ||
[@genType] | ||
let make = (~show, ~message, children) => | ||
ReasonReact.wrapJsForReason( | ||
~reactClass=myBanner, | ||
~props=jsProps(~show, ~message), | ||
children, | ||
); | ||
let make = make; |
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 |
---|---|---|
@@ -1,13 +1,22 @@ | ||
/* Typescript file generated by genType. */ | ||
|
||
import MyBanner from'./MyBanner'; | ||
|
||
import * as React from "react"; | ||
|
||
// tslint:disable-next-line:no-var-requires | ||
const MyBanner = require('./MyBanner'); | ||
const ReasonReact = require('reason-react/src/ReasonReact.js'); | ||
|
||
// tslint:disable-next-line:interface-over-type-literal | ||
export type Props = {show: boolean, message: string}; | ||
export type Props = {show: boolean, message?: message}; | ||
|
||
// In case of type error, check the type of 'make' in 'MyBannerRe.re' and the props of './MyBanner'. | ||
export function MyBannerTypeChecked(props: Props) { | ||
return <MyBanner {...props}/>; | ||
} | ||
|
||
export function checkJsWrapperType(props: Props) { | ||
return <MyBanner {...props}/>; | ||
} | ||
// Export 'make' early to allow circular import from the '.bs.js' file. | ||
export const make: unknown = function _(show: any, message: any, children: any) { return ReasonReact.wrapJsForReason(MyBanner, {show: show, message: (message == null ? message : {text:message[0]})}, children); }; | ||
|
||
// tslint:disable-next-line:interface-over-type-literal | ||
export type message = {text: string}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.