Form

A plain Form helper (not a custom element) that builds a native <form> and binds inputs to a target object, providing submit hooks.

Example

JS
const el = Form.create(player, {
    content: f => [
        f.label('name'),
        f.text('name'),
        f.submit('Save')
    ],
    onSubmitted: () => doSomething()
})
document.body.append(el)

Constructor

new Form(target, options)
target : Object

the record object to bind inputs to

options : Object optional
content : function optional

function receiving the Form instance, returns content for the form. The Form instance provides type helpers (e.g. f.text(), f.select(), f.checkbox()) for each input type, including types added with Input.register before the Form is built (e.g. f.color('color')).

onSubmit : function optional

event listener for submit (fires before saving). Call event.preventDefault() to prevent target.save() and the submitted event from firing.

onSubmitted : function optional

event listener fired after successful submit

Instance Methods

Generate a bound Input element for the given attribute

Parameters

attribute : string

the target attribute name to bind to

options : Object optional
type : string optional

input type (text, select, checkbox, date, etc., or any type added with Input.register)

Returns

HTMLElement

the created input element

Build a <label> linked to the input for the given attribute

Parameters

attribute : string

the attribute name (auto-titleized if no content given)

contentOrOptions : string | Object optional

label text, or options object if no text

maybeOptions : Object optional

options when first arg is content string

Returns

HTMLLabelElement

Create a submit <button>

Parameters

labelOrOptions : string | Object optional

button label text, or options object

maybeOptions : Object optional

options when first arg is label string

Returns

HTMLButtonElement

Static Methods

Create a Form and return the native <form> element

Parameters

target : Object

the record object to bind inputs to

options : Object optional

see Form constructor options

Returns

HTMLFormElement

the built <form> element

Create and return a Form instance

Parameters

target : Object

the record object to bind inputs to

options : Object optional

see Form constructor options

Returns

Form

the Form instance (access the element via .el)