Form
A plain Form helper (not a custom element) that builds a native <form>
and binds inputs to a target object, providing before/after submit hooks.
Example
const el = Form.create(player, {
content: f => [
f.label('name'),
f.text('name'),
f.submit('Save')
],
afterSubmit: rec => doSomething(rec)
})
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.
beforeSubmit
:
function
optionalcalled before submit with target. Return false to cancel.
afterSubmit
:
function
optionalcalled after successful submit with target
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.)
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)