@roqueform/ref-plugin
Version:
Associates Roqueform fields with DOM elements.
38 lines (36 loc) • 1.19 kB
JavaScript
/**
* Enables field-element association and simplifies focus control.
*/
function refPlugin() {
return refPluginInjector;
}
var refPluginInjector = function (field) {
var ref = field.ref;
field.element = null;
Object.defineProperty(field, 'isFocused', {
configurable: true,
get: function () { return field.element !== null && field.element === field.element.ownerDocument.activeElement; },
});
field.ref = function (element) {
ref === null || ref === void 0 ? void 0 : ref(element);
field.element = element instanceof Element ? element : null;
};
field.scrollIntoView = function (options) {
var _a;
(_a = field.element) === null || _a === void 0 ? void 0 : _a.scrollIntoView(options);
};
field.focus = function (options) {
if (isFocusable(field.element)) {
field.element.focus(options);
}
};
field.blur = function () {
if (isFocusable(field.element)) {
field.element.blur();
}
};
};
function isFocusable(element) {
return element !== null && 'tabIndex' in element && typeof element.tabIndex === 'number';
}
export { refPlugin };