3

Is there an idiomatic way for me trigger my formlet's submit action when a keydown event is pressed?

Should I drop back down to DOM manipulation, or is there some Enhancement that I can use?

Khanzor
  • 4,830
  • 3
  • 25
  • 41

1 Answers1

4

Unfortunately, at the moment there is no standard way to do this. We do intend to add it in a future version though, either as an Enhance combinator or as a new option to Enhance.WithCustomSubmit*.

We actually encountered the same problem when creating FPish, and we use the following workaround:

[<JavaScript>]
let TriggerOnEnter (formlet : Formlet<'T>) =
    formlet
    |> Formlet.MapElement (fun elem ->
        let e = JQuery.JQuery.Of(elem.Body)
        e.Keypress(fun _ k ->
            // Opera uses charCode
            if k?keyCode = 13 || k?charCode = 13 then
                JavaScript.SetTimeout (fun _ ->
                    e.Find("input[type=button]").Trigger("click").Ignore
                ) 100 |> ignore
            k.StopPropagation()
        ).Ignore
        elem
    )

Note that it triggers the first button in the form, so you might need adjustments to the jQuery selector to make it actually trigger the submit button.

Tarmil
  • 11,177
  • 30
  • 35
  • Your solution works like a charm. To find a submit button, you may enhance the query: `e.Find("input[type=button][class='submitButton']")`. However, if a `WithCustomSubmitButton` was used, the style name may vary. I'm not that familiar with W# to tell how to find the actual style name in a button layout. – Be Brave Be Like Ukraine May 19 '13 at 08:30