Skip to content

Commit

Permalink
Update validate modifier for new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wenincode authored and chris-hut committed Oct 16, 2023
1 parent 50cbd00 commit 838a8a9
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions ui/packages/consul-ui/app/modifiers/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@

import Modifier from 'ember-modifier';
import { action } from '@ember/object';
import { registerDestructor } from '@ember/destroyable';

class ValidationError extends Error {}

function cleanup(instance) {
if (instance && instance?.element) {
instance?.element?.removeEventListener('input', instance?.listen);
instance?.element?.removeEventListener('blur', instance?.reset);
}
}

export default class ValidateModifier extends Modifier {
item = null;
hash = null;
Expand Down Expand Up @@ -70,37 +78,24 @@ export default class ValidateModifier extends Modifier {
}
}

async connect([value], _hash) {
this.element.addEventListener('input', this.listen);
this.element.addEventListener('blur', this.reset);
if (this.element.value.length > 0) {
await Promise.resolve();
if (this && this.element) {
this.validate(this.element.value, this.hash.validations);
}
}
}

@action
listen(e) {
this.validate(e.target.value, this.hash.validations);
}

disconnect() {
this.item = null;
this.hash = null;
this.element.removeEventListener('input', this.listen);
this.element.removeEventListener('blur', this.reset);
constructor(owner, args) {
super(owner, args);
registerDestructor(this, cleanup);
}

didReceiveArguments() {
const [value] = this.args.positional;
const _hash = this.args.named;
async modify(element, positional, named) {
cleanup.call(this);

this.item = value;
this.hash = _hash;
this.element = element;
this.hash = named;
this.item = positional[0];

if (typeof _hash.chart === 'undefined') {
if (typeof this.hash.chart === 'undefined') {
this.hash.chart = {
state: {
context: {},
Expand All @@ -117,13 +112,15 @@ export default class ValidateModifier extends Modifier {
},
};
}
}

didInstall() {
this.connect(this.args.positional, this.args.named);
}
this.element.addEventListener('input', this.listen);
this.element.addEventListener('blur', this.reset);

willRemove() {
this.disconnect();
if (this.element.value.length > 0) {
await Promise.resolve();
if (this && this.element) {
this.validate(this.element.value, this.hash.validations);
}
}
}
}

0 comments on commit 838a8a9

Please sign in to comment.