Skip to content

Update examples #33

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

Merged
merged 1 commit into from
May 4, 2018
Merged
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
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,34 @@ module React.Basic.Example where

import Prelude

import Control.Monad.Eff.Uncurried (mkEffFn1)
import React.Basic (ReactComponent, react)
import React.Basic.DOM as R
import React.Basic.Events as Events

-- The props for the component
type ExampleProps =
{ label :: String
}

-- Create a component by passing a record to the `react` function.
-- The `render` function takes a display name, the props and current state, as well as a
-- The `render` function takes the props and current state, as well as a
-- state update callback, and produces a document.
example :: ReactComponent ExampleProps
example = react
{ displayName: "Example"
, initialState: { counter: 0 }
, receiveProps: \_ _ _ -> pure unit
, render: \{ label } { counter } setState ->
R.button { onClick: mkEffFn1 \_ -> do
setState \s -> { counter: s.counter + 1 }
, children: [ R.text (label <> ": " <> show counter) ]
}
}
component :: ReactComponent ExampleProps
component = react { displayName: "Counter", initialState, receiveProps, render }
where
initialState =
{ counter: 0
}

receiveProps _ _ _ =
pure unit

render props state setState =
R.button
{ onClick: Events.handler_ do
setState \s -> s { counter = s.counter + 1 }
, children: [ R.text (props.label <> ": " <> show state.counter) ]
}
```

This component can be used directly from JavaScript. For example, if you are using `purs-loader`:
Expand Down
17 changes: 9 additions & 8 deletions examples/component/src/Container.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import React.Basic.DOM as R
import ToggleButton as ToggleButton

component :: ReactComponent {}
component = stateless
{ displayName: "Container"
, render: \_ ->
R.div { children: [ createElement ToggleButton.component { on: true }
, createElement ToggleButton.component { on: false }
]
}
}
component = stateless { displayName: "Container", render }
where
render _ =
R.div
{ children:
[ createElement ToggleButton.component { on: true }
, createElement ToggleButton.component { on: false }
]
}
24 changes: 14 additions & 10 deletions examples/component/src/ToggleButton.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ module ToggleButton where

import Prelude

import Control.Monad.Eff.Uncurried (mkEffFn1)
import React.Basic (ReactComponent, react)
import React.Basic.DOM as R
import React.Basic.Events as Events

type ExampleProps =
{ on :: Boolean
}

component :: ReactComponent ExampleProps
component = react
{ displayName: "ToggleButton"
, initialState: { on: false }
, receiveProps: \{ on } _ setState -> setState (const { on })
, render: \_ { on } setState ->
R.button { onClick: mkEffFn1 \_ -> setState \s -> { on: not s.on }
, children: [ R.text if on then "On" else "Off" ]
}
}
component = react { displayName: "ToggleButton", initialState, receiveProps, render }
where
initialState =
{ on: false }

receiveProps props _ setState =
setState _ { on = props.on }

render _ state setState =
R.button
{ onClick: Events.handler_ (setState \s -> s { on = not s.on })
, children: [ R.text if state.on then "On" else "Off" ]
}
39 changes: 23 additions & 16 deletions examples/controlled-input/src/ControlledInput.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ module ControlledInput where
import Prelude

import Data.Maybe (Maybe(..), fromMaybe, maybe)
import React.Basic (ReactComponent, react)
import React.Basic (ReactComponent, fragment, react)
import React.Basic.DOM as R
import React.Basic.DOM.Events (preventDefault, targetValue, timeStamp)
import React.Basic.Events as Events

component :: ReactComponent {}
component = react
{ displayName: "ControlledInput"
, initialState: { value: "hello world", timeStamp: Nothing }
, receiveProps: \_ _ _ -> pure unit
, render: \_ state setState ->
R.div_
[ R.p_ [ R.input { onChange: Events.handler (preventDefault >>> Events.merge { targetValue, timeStamp })
\{ timeStamp, targetValue } -> setState \_ ->
{ value: fromMaybe "" targetValue
, timeStamp: Just timeStamp
}
, value: state.value
}
]
component = react { displayName: "ControlledInput", initialState, receiveProps, render }
where
initialState =
{ value: "hello world"
, timeStamp: Nothing
}

receiveProps _ _ _ =
pure unit

render _ state setState =
fragment
[ R.input
{ onChange:
Events.handler
(preventDefault >>> Events.merge { targetValue, timeStamp })
\{ timeStamp, targetValue } ->
setState _ { value = fromMaybe "" targetValue
, timeStamp = Just timeStamp
}
, value: state.value
}
, R.p_ [ R.text ("Current value = " <> show state.value) ]
, R.p_ [ R.text ("Changed at = " <> maybe "never" show state.timeStamp) ]
]
}
27 changes: 16 additions & 11 deletions examples/counter/src/Counter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Counter where

import Prelude

import Control.Monad.Eff.Uncurried (mkEffFn1)
import React.Basic (ReactComponent, react)
import React.Basic.DOM as R
import React.Basic.Events as Events

-- The props for the component
type ExampleProps =
Expand All @@ -15,13 +15,18 @@ type ExampleProps =
-- The `render` function takes the props and current state, as well as a
-- state update callback, and produces a document.
component :: ReactComponent ExampleProps
component = react
{ displayName: "Counter"
, initialState: { counter: 0 }
, receiveProps: \_ _ _ -> pure unit
, render: \{ label } { counter } setState ->
R.button { onClick: mkEffFn1 \_ -> do
setState \s -> { counter: s.counter + 1 }
, children: [ R.text (label <> ": " <> show counter) ]
}
}
component = react { displayName: "Counter", initialState, receiveProps, render }
where
initialState =
{ counter: 0
}

receiveProps _ _ _ =
pure unit

render props state setState =
R.button
{ onClick: Events.handler_ do
setState \s -> s { counter = s.counter + 1 }
, children: [ R.text (props.label <> ": " <> show state.counter) ]
}