@domx/dataelement
Version:
A DataElement base class for handling data state changes
39 lines • 1.44 kB
JavaScript
import { EventMapListenAt } from "@domx/eventmap";
import { customDataElements, DataElement } from "./DataElement";
export { customDataElement, dataProperty };
export { event } from "@domx/eventmap/decorators";
export { EventMapListenAt };
/**
* A class decorator that defines the custom element with
* `window.customElements.define` and tags the element name
* for use in RootState.
*
* Options allow for setting `stateIdProperty` and `eventsListenAt`.
* @param elementName {string}
* @param options {CustomDataElementOptions}
*/
const customDataElement = (elementName, options = {}) => (ctor) => {
if (options.stateIdProperty) {
ctor["stateIdProperty"] = options.stateIdProperty;
}
if (options.eventsListenAt) {
ctor["eventsListenAt"] = options.eventsListenAt;
}
customDataElements.define(elementName, ctor);
};
/**
* A property decorator that tags a class property
* as a state property.
*
* Options allow for setting the change event name.
* @param options
*/
const dataProperty = (options) => (prototype, propertyName) => {
if (prototype.constructor.dataProperties === DataElement.dataProperties) {
prototype.constructor.dataProperties = {};
}
prototype.constructor.dataProperties[propertyName] = {
changeEvent: options ? options.changeEvent : `${propertyName}-changed`
};
};
//# sourceMappingURL=decorators.js.map