-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setup function, to wrap componentDidMount (#10)
* Add setup function, to wrap componentDidMount * Bind this_ * Counter example (no webpack) * Update for new setup function * Add component function, and example * Rename to receiveProps, remove props from initialState * Update readme * Remove package-lock.json
- Loading branch information
Showing
19 changed files
with
242 additions
and
12 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
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,4 @@ | ||
output | ||
html/index.js | ||
package-lock.json | ||
node_modules |
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,5 @@ | ||
all: | ||
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs' | ||
purs bundle --module Container output/*/*.js > output/bundle.js | ||
echo 'module.exports = PS.Container;' >> output/bundle.js | ||
node_modules/browserify/bin/cmd.js output/bundle.js index.js -o html/index.js |
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,12 @@ | ||
# Component Example | ||
|
||
## Building | ||
|
||
``` | ||
npm install | ||
make all | ||
``` | ||
|
||
This will compile the PureScript source files, bundle them, and use Browserify to combine PureScript and NPM sources into a single bundle. | ||
|
||
Then open `html/index.html` in your browser. |
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>react-basic example</title> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script src="index.js"></script> | ||
</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,10 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
var ReactDOM = require("react-dom"); | ||
var Container = require("./output/bundle.js"); | ||
|
||
ReactDOM.render( | ||
React.createElement(Container.component, { label: 'Increment' }), | ||
document.getElementById("container") | ||
); |
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 @@ | ||
{ | ||
"name": "component", | ||
"version": "1.0.0", | ||
"description": "", | ||
"keywords": [], | ||
"author": "", | ||
"dependencies": { | ||
"create-react-class": "^15.6.2", | ||
"react": "^15.6.2", | ||
"react-dom": "^15.6.2" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.1.0" | ||
} | ||
} |
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 @@ | ||
module Container where | ||
|
||
import Prelude | ||
|
||
import React.Basic as R | ||
import ToggleButton as ToggleButton | ||
|
||
component :: R.ReactComponent Unit | ||
component = R.react | ||
{ initialState: unit | ||
, receiveProps: \_ _ _ -> pure unit | ||
, render: \_ _ setState -> | ||
R.div { } [ R.component ToggleButton.component { on: true } | ||
, R.component ToggleButton.component { on: false } | ||
] | ||
} |
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,24 @@ | ||
module ToggleButton where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Eff.Uncurried (mkEffFn1) | ||
import React.Basic as R | ||
|
||
type ExampleProps = | ||
{ on :: Boolean | ||
} | ||
|
||
type ExampleState = | ||
{ on :: Boolean | ||
} | ||
|
||
component :: R.ReactComponent ExampleProps | ||
component = R.react | ||
{ initialState: { on: false } | ||
, receiveProps: \{ on } _ setState -> setState { on } | ||
, render: \_ { on } setState -> | ||
R.button { onClick: mkEffFn1 \_ -> setState { on: not on } | ||
} | ||
[ R.text if on then "On" else "Off" ] | ||
} |
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,4 @@ | ||
output | ||
html/index.js | ||
package-lock.json | ||
node_modules |
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,5 @@ | ||
all: | ||
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs' | ||
purs bundle --module Counter output/*/*.js > output/bundle.js | ||
echo 'module.exports = PS.Counter;' >> output/bundle.js | ||
node_modules/browserify/bin/cmd.js output/bundle.js index.js -o html/index.js |
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,12 @@ | ||
# Counter Example | ||
|
||
## Building | ||
|
||
``` | ||
npm install | ||
make all | ||
``` | ||
|
||
This will compile the PureScript source files, bundle them, and use Browserify to combine PureScript and NPM sources into a single bundle. | ||
|
||
Then open `html/index.html` in your browser. |
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>react-basic example</title> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script src="index.js"></script> | ||
</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,10 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
var ReactDOM = require("react-dom"); | ||
var Counter = require("./output/bundle.js"); | ||
|
||
ReactDOM.render( | ||
React.createElement(Counter.component, { label: 'Increment' }), | ||
document.getElementById("container") | ||
); |
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 @@ | ||
{ | ||
"name": "counter", | ||
"version": "1.0.0", | ||
"description": "", | ||
"keywords": [], | ||
"author": "", | ||
"dependencies": { | ||
"create-react-class": "^15.6.2", | ||
"react": "^15.6.2", | ||
"react-dom": "^15.6.2" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.1.0" | ||
} | ||
} |
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,30 @@ | ||
module Counter where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Eff.Uncurried (mkEffFn1) | ||
import React.Basic as R | ||
|
||
-- The props for the component | ||
type ExampleProps = | ||
{ label :: String | ||
} | ||
|
||
-- The internal state of the component | ||
type ExampleState = | ||
{ counter :: Int | ||
} | ||
|
||
-- Create a component by passing a record to the `react` function. | ||
-- The `render` function takes the props and current state, as well as a | ||
-- state update callback, and produces a document. | ||
component :: R.ReactComponent ExampleProps | ||
component = R.react | ||
{ initialState: { counter: 0 } | ||
, receiveProps: \_ _ _ -> pure unit | ||
, render: \{ label } { counter } setState -> | ||
R.button { onClick: mkEffFn1 \_ -> do | ||
setState { counter: counter + 1 } | ||
} | ||
[ R.text (label <> ": " <> show counter) ] | ||
} |
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
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,37 +1,56 @@ | ||
module React.Basic | ||
( react | ||
, component | ||
, module React.Basic.DOM | ||
, module React.Basic.Types | ||
) where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Eff (Eff, kind Effect) | ||
import Data.Function.Uncurried (Fn3, mkFn3) | ||
import Control.Monad.Eff.Uncurried (EffFn3, mkEffFn3) | ||
import Data.Function.Uncurried (Fn2, runFn2, Fn3, mkFn3) | ||
import React.Basic.DOM as React.Basic.DOM | ||
import React.Basic.Types (CSS, EventHandler, JSX, ReactComponent, ReactFX) | ||
import React.Basic.Types as React.Basic.Types | ||
|
||
foreign import react_ | ||
:: forall props state | ||
. { initialState :: props -> state | ||
. { initialState :: state | ||
, receiveProps :: EffFn3 (react :: ReactFX) props state (state -> Eff (react :: ReactFX) Unit) Unit | ||
, render :: Fn3 props state (state -> Eff (react :: ReactFX) Unit) JSX | ||
} | ||
-> ReactComponent props | ||
|
||
-- | Create a React component from a _specification_ of that component. | ||
-- | | ||
-- | A _specification_ consists of a state type, an initial value for that state, | ||
-- | and a rendering function which takes a value of that state type, additional | ||
-- | _props_ (which will be passed in by the user) and a state update function. | ||
-- | a function to apply incoming props to the internal state, and a rendering | ||
-- | function which takes props, state and a state update function. | ||
-- | | ||
-- | The rendering function should return a value of type `JSX`, which can be | ||
-- | constructed using the helper functions provided by the `React.Basic.DOM` | ||
-- | module (and re-exported here). | ||
react | ||
:: forall props state | ||
. { initialState :: props -> state | ||
. { initialState :: state | ||
, receiveProps :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> Eff (react :: ReactFX) Unit | ||
, render :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> JSX | ||
} | ||
-> ReactComponent props | ||
react { initialState, render } = react_ { initialState, render: mkFn3 render } | ||
react { initialState, receiveProps, render } = | ||
react_ | ||
{ initialState | ||
, receiveProps: mkEffFn3 receiveProps | ||
, render: mkFn3 render | ||
} | ||
|
||
foreign import component_ :: forall props. Fn2 (ReactComponent props) props JSX | ||
|
||
-- | Create a `JSX` node from another React component, by providing the props. | ||
component | ||
:: forall props | ||
. ReactComponent props | ||
-> props | ||
-> JSX | ||
component = runFn2 component_ |