@descope/sdk-mixins
Version:
Descope JavaScript SDK mixins
56 lines (53 loc) • 2.23 kB
JavaScript
import { createSingletonMixin } from '@descope/sdk-helpers';
import { loggerMixin } from './loggerMixin/loggerMixin.js';
const formMixin = createSingletonMixin((superclass) => class FormMixinClass extends loggerMixin(superclass) {
validateForm(rootEle) {
return this.getFormInputs(rootEle).every((input) => {
var _a, _b;
(_a = input.reportValidity) === null || _a === void 0 ? void 0 : _a.call(input);
return (_b = input.checkValidity) === null || _b === void 0 ? void 0 : _b.call(input);
});
}
// eslint-disable-next-line class-methods-use-this
getFormInputs(rootEle) {
if (!rootEle) {
this.logger.debug('cannot get form inputs, no root element was received');
return [];
}
return Array.from(rootEle.querySelectorAll('[name]'));
}
getFormData(rootEle) {
return this.getFormInputs(rootEle).reduce((acc, input) => Object.assign(acc, { [input.getAttribute('name')]: input.value }), {});
}
setFormData(rootEle, data) {
this.getFormInputs(rootEle).forEach((input) => {
// eslint-disable-next-line no-prototype-builtins
if (data.hasOwnProperty(input.getAttribute('name'))) {
// eslint-disable-next-line no-param-reassign
input.value = data[input.getAttribute('name')];
}
});
}
resetFormData(rootEle) {
this.getFormInputs(rootEle).forEach((input) => {
// eslint-disable-next-line no-param-reassign
input.value = '';
input.checked = false;
});
}
getFormFieldNames(rootEle) {
return this.getFormInputs(rootEle).map((ele) => ele.name);
}
disableFormField(rootEle, name) {
var _a;
(_a = this.getFormInputs(rootEle)
.find((input) => input.name === name)) === null || _a === void 0 ? void 0 : _a.setAttribute('disabled', 'true');
}
removeFormField(rootEle, name) {
var _a;
(_a = this.getFormInputs(rootEle)
.find((input) => input.name === name)) === null || _a === void 0 ? void 0 : _a.remove();
}
});
export { formMixin };
//# sourceMappingURL=formMixin.js.map