@gavant/ember-validations
Version:
A form validator library using ember-changeset and ember-changeset-validations
131 lines (102 loc) • 4.39 kB
JavaScript
import { _ as _applyDecoratedDescriptor } from '../../applyDecoratedDescriptor-e87190e7.js';
import { setComponentTemplate } from '@ember/component';
import { hbs } from 'ember-cli-htmlbars';
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { scheduleOnce } from '@ember/runloop';
import Component from '@glimmer/component';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
var TEMPLATE = hbs("<div class=\"form-group input-validator {{if this.showError \'is-invalid\'}} {{if this.label \'has-label\'}}\" ...attributes {{did-insert this.onInsert}} {{did-update this.changesetHasChanged @parent.changeset}}>\n {{#if this.label}}\n <label for={{this.inputId}} class=\"{{this.labelClass}} input-validator-label\">\n {{this.fieldLabel}}\n </label>\n {{/if}}\n {{yield}}\n {{#unless @hideErrorText}}\n <div class=\"input-validator-error\">\n {{#if this.showError}}\n <div class={{this.errorClass}}>\n {{if this.msg this.msg this.formattedErrors}}\n </div>\n {{/if}}\n </div>\n {{/unless}}\n</div>");
var _class;
let InputValidator = (_class = class InputValidator extends Component {
get isInvalid() {
return this.showError;
}
get hasLabel() {
return this.label;
}
get hasError() {
return !!this.args.errors && this.args.errors.length !== 0;
}
get showError() {
return this.hasError && (this.hasFocusedOut || this.args.parent.showAllValidationFields);
}
get fieldLabel() {
return this.args.text;
}
get formattedErrors() {
const errors = this.args.errors;
if (Array.isArray(errors)) {
return errors.join(', ') ?? '';
} else {
return errors ?? '';
}
}
/**
* Creates an instance of InputValidator.
* @param {unknown} owner
* @param {InputValidatorArgs<T>} args
* @memberof InputValidator
*/
constructor(owner, args) {
super(owner, args);
_defineProperty(this, "labelClass", 'control-label');
_defineProperty(this, "errorClass", 'invalid-feedback');
_defineProperty(this, "hasFocusedOut", false);
_defineProperty(this, "label", false);
assert('input validators must be inside a form-validator block and invoked using the yielded <Validator.input> contextual component', this.args.parent.constructor.name === 'FormValidator' || this.args.parent.constructor.name === 'FormValidatorChild');
}
/**
* On insert, set the label for attribute
*
* @param {HTMLElement} element
* @memberof InputValidator
*/
onInsert(element) {
scheduleOnce('afterRender', this, this.setLabelForAttribute, element);
}
/**
* Set focused out value
*
* @param {boolean} value
* @memberof InputValidator
*/
setFocusedOut(value) {
this.hasFocusedOut = value;
}
/**
* After the changeset is updated, set the focused value
*
* @memberof InputValidator
*/
changesetHasChanged() {
scheduleOnce('afterRender', this, this.setFocusedOut, false);
}
/**
* Set label for attribute
*
* @param {HTMLElement} element
* @memberof InputValidator
*/
setLabelForAttribute(element) {
const input = element.querySelector('input, select, textarea');
const label = element.querySelector('label.input-validator-label');
if (input && label) {
label.setAttribute('for', input.id);
}
}
}, (_applyDecoratedDescriptor(_class.prototype, "onInsert", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onInsert"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setFocusedOut", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setFocusedOut"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "changesetHasChanged", [action], Object.getOwnPropertyDescriptor(_class.prototype, "changesetHasChanged"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setLabelForAttribute", [action], Object.getOwnPropertyDescriptor(_class.prototype, "setLabelForAttribute"), _class.prototype)), _class);
setComponentTemplate(TEMPLATE, InputValidator);
export { InputValidator as default };