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
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
optionalcontent
:
function
optionalfunction 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
optionalevent listener for submit (fires before saving). Call event.preventDefault() to prevent target.save() and the submitted event from firing.
onSubmitted
:
function
optionalevent 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
optionaltype
:
string
optionalinput 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
optionallabel text, or options object if no text
maybeOptions
:
Object
optionaloptions when first arg is content string
Returns
HTMLLabelElement
Create a submit <button>
Parameters
labelOrOptions
:
string
|
Object
optionalbutton label text, or options object
maybeOptions
:
Object
optionaloptions 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
Returns
HTMLFormElement
the built <form> element
Create and return a Form instance
Parameters
target
:
Object
the record object to bind inputs to
Returns
Form
the Form instance (access the element via .el)