UNPKG

@descope/sdk-mixins

Version:
58 lines (54 loc) 2.26 kB
'use strict'; var sdkHelpers = require('@descope/sdk-helpers'); var loggerMixin = require('./loggerMixin/loggerMixin.js'); const formMixin = sdkHelpers.createSingletonMixin((superclass) => class FormMixinClass extends loggerMixin.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(); } }); exports.formMixin = formMixin; //# sourceMappingURL=formMixin.js.map