Skip to content

Commit

Permalink
Add an enable disable button to the virtual keypad example
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnsit committed Jan 20, 2020
1 parent 12e2457 commit b7aaaf4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
9 changes: 3 additions & 6 deletions examples/src/Test/Keyboard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"use strict";

var counter = 0;
var observers = [];
var observer = null;

function innerHandler(event) {
event.preventDefault();
observers.forEach(function(o){o(event);});
if(observer) observer(event);
}

// Start listening for keys
Expand All @@ -23,10 +22,8 @@ exports.stopListening = function() {
// Await a key
// :: EffectFnAff KeyEvent
exports._awaitKey = function (onError, onSuccess) {
var id = counter++;
observers[id] = onSuccess;
observer = onSuccess;
return function (cancelError, onCancelerError, onCancelerSuccess) {
delete observers[id];
onCancelerSuccess();
};
};
16 changes: 13 additions & 3 deletions examples/src/Test/Keyboard.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Concur.React (HTML)
import Concur.React.DOM as D
import Concur.React.Props as P
import Control.Alt ((<|>))
import Control.Applicative (pure, (*>))
import Control.Bind (bind)
import Control.Applicative (pure)
import Control.Bind (bind, discard)
import Data.BooleanAlgebra (not)
import Data.Eq (class Eq, (==))
import Data.Function (($))
import Data.Maybe (Maybe(..))
Expand All @@ -29,12 +30,21 @@ import React.SyntheticEvent as R
-- A never-ending virtual keypad widget.
-- Allows the user to navigate and select a key. Displays the selected key.
keypadWidget :: forall a. Widget HTML a
keypadWidget = liftEffect startListening *> go Enter ""
keypadWidget = go Enter "" <|> toggleEvents
where
go focus msg = do
keyPressed <- virtualKeyInput focus <|> D.div' [D.text msg]
go keyPressed $ "You clicked: " <> show keyPressed

-- On off button for key events
toggleEvents :: forall a. Widget HTML a
toggleEvents = go false
where
go enabled = do
_ <- D.button [P.onClick] [D.text $ if enabled then "stop listening" else "start listening"]
liftEffect (if enabled then stopListening else startListening)
go (not enabled)

-- Displays a keypad with the supplied initial focus.
-- Allows the user to navigate and select a key. Returns the selected key.
virtualKeyInput :: Focus -> Widget HTML Key
Expand Down

0 comments on commit b7aaaf4

Please sign in to comment.