CustomControl
A base class for custom controls based on CustomElement.
import { CustomControl } from "@schukai/monster/source/dom/customcontrol.mjs";Introduction
CustomControl extends CustomElement and adds form association through attachInternals(). It is the base class for Monster form controls like selects, inputs, and composite widgets.
Key Features
- Form association: Uses
ElementInternalsto integrate with native forms. - Validation hooks: Provides standard form APIs such as
setValidity()andreportValidity(). - Value contract: Derived classes must implement
valuegetter and setter.
Minimal Setup
import { CustomControl } from "@schukai/monster/source/dom/customcontrol.mjs";
import { registerCustomElement } from "@schukai/monster/source/dom/customelement.mjs";
class MyControl extends CustomControl {
static getTag() {
return "my-control";
}
get value() {
return "";
}
set value(next) {
// update internal state
}
}
registerCustomElement(MyControl);
Exported
CustomControlDerived from
CustomElementOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomElement.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the CustomElement.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomElement.
Constructor
constructor(){Error}the ElementInternals is not supported and a polyfill is necessary
attachInternals() and initializes an internal form-associated element if supported. Additionally, it initializes a MutationObserver to watch for attribute changes. See the links below for more information: {@link https://html.spec.whatwg.org/multipage/custom-elements.html#dom-customelementregistry-define|CustomElementRegistry.define()} {@link https://html.spec.whatwg.org/multipage/custom-elements.html#dom-customelementregistry-get|CustomElementRegistry.get()} and {@link https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals|ElementInternals}State query methods
value(){Error}the value getter must be overwritten by the derived class
value(value)value{*}: The value to set.
{Error}the value setter must be overwritten by the derived class
willValidate()- {boolean}
{Error}the ElementInternals is not supported and a polyfill is necessary
Structural methods
setFormValue(value,state)value{file|string|formdata}: valuestate{file|string|formdata}: state
- {undefined}
{DOMException}NotSupportedError{Error}the ElementInternals is not supported and a polyfill is necessary
// Use the control's name as the base name for submitted data const n = this.getAttribute('name'); const entries = new FormData(); entries.append(n + '-first-name', this.firstName_); entries.append(n + '-last-name', this.lastName_); this.setFormValue(entries);``setValidity(flags,message,anchor)flags{object}: flagsmessage{string|undefined}: messageanchor{htmlelement}: anchor
- {undefined}
{DOMException}NotSupportedError{Error}the ElementInternals is not supported and a polyfill is necessary
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.observedAttributes()1.15.0- {string[]}
attributeChangedCallback().Other methods
checkValidity()- {boolean}
{DOMException}NotSupportedError{Error}the ElementInternals is not supported and a polyfill is necessary
form()- {HTMLFontElement|null}
{Error}the ElementInternals is not supported and a polyfill is necessary
formAssociatedCallback(form)form{htmlformelement}: - The form element to associate with the control
form attribute of the custom control to the id of the passed form element. If no form element is passed, removes the form attribute.formDisabledCallback(disabled)disabled{boolean}: - Whether or not the control should be disabled
disabled attribute of the custom control based on the passed value.formResetCallback()formStateRestoreCallback(state,mode)state{string}: statemode{string}: mode
labels()- {NodeList}
{Error}the ElementInternals is not supported and a polyfill is necessary
name()- {string|null}
reportValidity()- {boolean}
{Error}the ElementInternals is not supported and a polyfill is necessary{DOMException}NotSupportedError
states()- {boolean}
{Error}the ElementInternals is not supported and a polyfill is necessary
type()- {string}
validationMessage()- {string}
{Error}the ElementInternals is not supported and a polyfill is necessary
validity()- {ValidityState}
{Error}the ElementInternals is not supported and a polyfill is necessary
Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.