Skip to content

Commit b7aaaf4

Browse files
committed
Add an enable disable button to the virtual keypad example
1 parent 12e2457 commit b7aaaf4

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

examples/src/Test/Keyboard.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"use strict";
22

3-
var counter = 0;
4-
var observers = [];
3+
var observer = null;
54

65
function innerHandler(event) {
76
event.preventDefault();
8-
observers.forEach(function(o){o(event);});
7+
if(observer) observer(event);
98
}
109

1110
// Start listening for keys
@@ -23,10 +22,8 @@ exports.stopListening = function() {
2322
// Await a key
2423
// :: EffectFnAff KeyEvent
2524
exports._awaitKey = function (onError, onSuccess) {
26-
var id = counter++;
27-
observers[id] = onSuccess;
25+
observer = onSuccess;
2826
return function (cancelError, onCancelerError, onCancelerSuccess) {
29-
delete observers[id];
3027
onCancelerSuccess();
3128
};
3229
};

examples/src/Test/Keyboard.purs

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import Concur.React (HTML)
55
import Concur.React.DOM as D
66
import Concur.React.Props as P
77
import Control.Alt ((<|>))
8-
import Control.Applicative (pure, (*>))
9-
import Control.Bind (bind)
8+
import Control.Applicative (pure)
9+
import Control.Bind (bind, discard)
10+
import Data.BooleanAlgebra (not)
1011
import Data.Eq (class Eq, (==))
1112
import Data.Function (($))
1213
import Data.Maybe (Maybe(..))
@@ -29,12 +30,21 @@ import React.SyntheticEvent as R
2930
-- A never-ending virtual keypad widget.
3031
-- Allows the user to navigate and select a key. Displays the selected key.
3132
keypadWidget :: forall a. Widget HTML a
32-
keypadWidget = liftEffect startListening *> go Enter ""
33+
keypadWidget = go Enter "" <|> toggleEvents
3334
where
3435
go focus msg = do
3536
keyPressed <- virtualKeyInput focus <|> D.div' [D.text msg]
3637
go keyPressed $ "You clicked: " <> show keyPressed
3738

39+
-- On off button for key events
40+
toggleEvents :: forall a. Widget HTML a
41+
toggleEvents = go false
42+
where
43+
go enabled = do
44+
_ <- D.button [P.onClick] [D.text $ if enabled then "stop listening" else "start listening"]
45+
liftEffect (if enabled then stopListening else startListening)
46+
go (not enabled)
47+
3848
-- Displays a keypad with the supplied initial focus.
3949
-- Allows the user to navigate and select a key. Returns the selected key.
4050
virtualKeyInput :: Focus -> Widget HTML Key

0 commit comments

Comments
 (0)