简体   繁体   English

Purescript卤素输入元素和自定义自动更正属性

[英]Purescript Halogen Input Element and Custom Autocorrect Property

I'm trying to disable autocorrect in inputs for safari on iOS. 我正在尝试在iOS的Safari中禁用自动更正输入。 There isn't currently an "autocorrect" property in purescript halogen, so I created one, and modified the indexed input element record to use it. purescript卤素灯目前没有“自动更正”属性,因此我创建了一个属性,并修改了索引输入元素记录以使用它。

My problem is that the new property isn't actually added to the dom when the input element is rendered. 我的问题是,渲染输入元素时,实际上不会将新属性添加到dom中。 I also created an "autocapitalize" property that is properly added to the dom when rendered in chrome, but not safari, so I know that the way I'm creating properties can work (though i'm not sure why neither property works in safari). 我还创建了一个“ autocapitalize”属性,该属性在以chrome呈现时会正确添加到dom中,但不能在Safari中显示,因此我知道我创建属性的方式可以正常工作(尽管我不确定为什么这两个属性都无法在Safari中工作)。 I can add the "autocorrect" property with purescript-jquery and everything works in safari, but is there a more natural way to get the property in the DOM? 我可以在purescript-jquery中添加“ autocorrect”属性,并且一切都可以在safari中运行,但是有没有更自然的方法在DOM中获取该属性?

Here's the reference to the safari-specific "autocorrect" property https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input . 这是对Safari特定的“自动更正”属性的参考https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

I also created a github repo with the code https://github.com/sportanova/purescript-input-example/tree/master 我还使用代码https://github.com/sportanova/purescript-input-example/tree/master创建了一个github回购

I'm using purescript v0.8.5 and halogen v0.8 我正在使用purescript v0.8.5和卤素v0.8

module Main where

import Prelude
import Control.Monad.Eff (Eff)
import Halogen
import Halogen.Util (awaitBody, runHalogenAff)
import Halogen.HTML.Properties.Indexed as PI
import Halogen.HTML.Elements as E
import Halogen.HTML.Indexed as HI
import Data.Generic (class Generic, gCompare, gEq)
import Control.Monad.Aff (Aff)
import Halogen.HTML.Core (Prop, prop, propName, attrName)
import Halogen.HTML.Elements.Indexed (Leaf)
import Halogen.HTML.Properties.Indexed (IProp, I)
import Unsafe.Coerce (unsafeCoerce)
import Data.Maybe (Maybe(..))
import Control.Monad.Eff.JQuery as J

inputx :: forall p i. Leaf (accept :: I,  autocapitalize :: I, autocomplete :: I, autocorrect :: I, autofocus :: I, checked :: I, disabled :: I, form :: I, formaction :: I, formenctype :: I, formmethod :: I, formnovalidate :: I, formtarget :: I, height :: I, list :: I, max :: I, min :: I, multiple :: I, onAbort :: I, onChange :: I, onError :: I, onInput :: I, onInvalid :: I, onLoad :: I, onSearch :: I, onSelect :: I, pattern :: I, placeholder :: I, readonly :: I, required :: I, size :: I, src :: I, step :: I, inputType :: I, value :: I, width :: I) p i
inputx = unsafeCoerce E.input

refine :: forall a r i. (a -> Prop i) -> a -> IProp r i
refine = unsafeCoerce

onOffProp :: forall i. String -> Boolean -> Prop i
onOffProp pName = prop (propName pName) (Just $ attrName pName) <<< (\b -> if b then "on" else "off")

autoCapitalizeP :: forall i. Boolean -> Prop i
autoCapitalizeP = onOffProp "autocapitalize"

autocapitalize :: forall r i. Boolean -> IProp (autocapitalize :: I | r) i
autocapitalize = refine autoCapitalizeP

autoCorrectP :: forall i. Boolean -> Prop i
autoCorrectP = onOffProp "autocorrect"

autocorrect :: forall r i. Boolean -> IProp (autocorrect :: I | r) i
autocorrect = refine autoCorrectP


type State = { }

data Query a =
    Initialize a
  | Finalize a

ui :: forall eff. Component State Query (ExEff eff)
ui = lifecycleComponent {
  render,
  eval,
  initializer: Just (action Initialize),
  finalizer: Just (action Finalize)
} where

  render :: State -> ComponentHTML Query
  render st = HI.div_
    [
      HI.div_ [HI.text "autocorrect attribute not added"]
    , inputx
        [
          PI.name "example"
        , PI.autocomplete false
        , autocorrect false
        , autocapitalize false
        , PI.spellcheck false
        , PI.inputType PI.InputText
        ]
    , HI.div_ [HI.text "autocorrect attribute added through jquery"]
    , inputx
      [
        PI.name "working-example"
      , PI.id_ "working-example"
      , PI.autocomplete false
      , autocorrect false
      , autocapitalize false
      , PI.spellcheck false
      , PI.inputType PI.InputText
      ]
    ]

  eval :: Natural Query (ComponentDSL State Query (ExEff eff))
  eval (Initialize next) = do
    inputEl <- fromEff $ J.select ("#working-example")
    x <- fromEff $ J.setAttr "autocorrect" "off" inputEl
    pure next
  eval (Finalize next) = pure next


data Slot = Slot

derive instance slotGeneric :: Generic Slot

instance eqSlot :: Eq Slot where
  eq = gEq

instance ordGeneric :: Ord Slot where
  compare = gCompare

type ExEff eff = Aff (EX eff)
type EX eff = HalogenEffects (eff)

main :: forall eff. Eff (EX eff) Unit
main = runHalogenAff do
  body <- awaitBody
  runUI ui {} body

Have you tried defining these with the Attr constructor rather than prop ? 您是否尝试过使用Attr构造函数而不是prop定义它们? I think since they're not part of the standard DOM API they will need to be defined as plain attributes rather than as properties. 我认为,由于它们不是标准DOM API的一部分,因此需要将它们定义为纯属性而不是属性。

The attribute/property distinction is an unfortunate thing inherited from the DOM API - properties are special, basically, as they are part of the interface of the element and not necessarily string-typed in their values. 属性/属性的区别是从DOM API继承而来的-不幸的是-属性基本上是特殊的,因为它们是元素接口的一部分,并且不一定是其值的字符串类型。

The other cause might be that there are properties for autocorrect and autocapitalize , but they are not named that way - property names are case sensitive, unlike attribute names. 另一个原因可能是有一些属性用于autocorrectautocapitalize ,但是它们的命名方式不是这样-与属性名称不同,属性名称区分大小写。

The for property is an example of a case where the property and attribute names differ. for属性是属性和属性名称不同的情况的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM