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

import Effect (Effect)
import Reactix as R
import Reactix.DOM.HTML as H

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

10 11
here :: R2.Here
here = R2.here "Gargantext.Components.InputWithEnter"
12

13
type Props a = (
14 15
    onBlur         :: String -> Effect Unit
  , onEnter        :: Unit -> Effect Unit
16 17
  , onValueChanged :: String -> Effect Unit

18 19 20 21 22
  , autoFocus      :: Boolean
  , className      :: String
  , defaultValue   :: String
  , placeholder    :: String
  , type           :: String
23 24 25 26
  )

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

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

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

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