From 220ae01ab783fe05541806ddfb2eb929b1e9d9ae Mon Sep 17 00:00:00 2001 From: wenincode Date: Fri, 13 Oct 2023 15:42:44 -0600 Subject: [PATCH] Update validate modifier for new structure --- .../consul-ui/app/modifiers/validate.js | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/ui/packages/consul-ui/app/modifiers/validate.js b/ui/packages/consul-ui/app/modifiers/validate.js index a840ba8cba..18ccb7f7c5 100644 --- a/ui/packages/consul-ui/app/modifiers/validate.js +++ b/ui/packages/consul-ui/app/modifiers/validate.js @@ -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; @@ -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: {}, @@ -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); + } + } } }