InputWithEnter.purs 1.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
module Gargantext.Components.InputWithEnter where

import Data.Tuple.Nested ((/\))
import DOM.Simple.Console (log2)
import Effect (Effect)
import Reactix as R
import Reactix.DOM.HTML as H

import Gargantext.Prelude
import Gargantext.Utils.Reactix as R2

12 13
thisModule = "Gargantext.Components.InputWithEnter"

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
type Props a = (
    onEnter :: Unit -> Effect Unit
  , onValueChanged :: String -> Effect Unit

  , autoFocus :: Boolean
  , className :: String
  , defaultValue :: String
  , placeholder :: String
  , type :: String
  )

inputWithEnter :: forall a. Record (Props a) -> R.Element
inputWithEnter props = R.createElement inputWithEnterCpt props []

inputWithEnterCpt :: forall a. R.Component (Props a)
29
inputWithEnterCpt = R.hooksComponentWithModule thisModule "inputWithEnter" cpt
30 31 32 33 34 35 36 37 38 39 40 41 42
  where
    cpt props@{ onEnter, onValueChanged
              , autoFocus, className, defaultValue, placeholder } _ = do
      pure $ H.input { on: { input: onInput
                           , keyPress: onKeyPress }
                     , autoFocus
                     , className
                     , defaultValue
                     , placeholder
                     , type: props.type }

      where
        onInput e = do
43
           onValueChanged $ R.unsafeEventValue e
44 45 46 47 48 49 50 51

        onKeyPress e = do
          char <- R2.keyCode e
          if char == 13 then
            onEnter unit
          else
            pure unit