Input
A generator for an input element that binds the value of the input to an object. This is not an element, but a generator that adds listeners to an input element. If one day, Safari adds support for extending built-in elements, this can be converted.
Example
Input.create('number', {})
// <input ...>
Input.new('number', {})
// Input()Constructor
new Input(options)
options
:
Object
optionaltarget
:
Object
optionalObject to bind to
record
:
Object
optionalDeprecated; use target instead
attribute
:
string
optionalAttribute of the Object to bind to
dump
:
function
optionaltransform textContent to value on change
load
:
function
optionaltransform value for input's content
Static Properties
Registry of input classes by type, consulted by Input.new.
Types with no entry fall back to a plain <input type="…">.
Instance Methods
Load the target's value into the input.
A nil value clears the input, so an attribute set back to null shows as
empty rather than keeping the last value. The very first load is exempt:
an input built with a value keeps it when the target has nothing yet.
Parameters
e
:
Event
optionalv
:
*
optionalValue to load instead of reading the target.
clear()
Empty the input. Called by Input#_load when the loaded value is
nil; override for inputs where empty isn't an empty value.
Static Methods
Create a bound input and return the DOM element
Parameters
type
:
string
input type (text, number, checkbox, select, date, textarea, button, etc.)
Returns
HTMLElement
the created input element
Create and return an Input instance (access the element via .input)
Parameters
type
:
string
input type (text, number, checkbox, select, date, textarea, button, etc., or any type added with Input.register)
Returns
Input
the Input instance
Register a class for an input type, so Input.create(type) and
Form#input(attr, { type }) build it. Registering a built-in type
name (e.g. select) overrides it.
klass is either an Input subclass, used as-is, or a custom element
class (e.g. a FormField), which is wrapped so the element is the
input. The element is constructed with the input options and must have a
value getter/setter and fire a change event when the user edits it.
Input#clear sets value = ''.
Example
Input.register('color', ColorField)
Form.create(target, { content: f => f.input('color', { type: 'color' }) })Parameters
type
:
string
klass
:
function
an Input subclass or an HTMLElement subclass
Returns
function
the class registered for type
Remove the class registered for type, including built-ins.
Parameters
type
:
string