Skip to content

Commit

Permalink
refactor(validate-input.directive.ts): rewrite directive making it ag…
Browse files Browse the repository at this point in the history
…nostic
  • Loading branch information
Helias committed Jan 23, 2025
1 parent 4ded5c1 commit 7021a3a
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@
</div>
<div class="form-group col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">
<label for="maxLevel">Max Level</label>
<input
[keiraInputValidation]="editorService.form.get('maxlevel')"
[formControlName]="'maxlevel'"
id="maxlevel"
type="number"
class="form-control form-control-sm"
/>
<keira-validation-feedback [control]="editorService.form.get('maxlevel')"></keira-validation-feedback>
<input keiraInputValidation [formControlName]="'maxlevel'" id="maxlevel" type="number" class="form-control form-control-sm" />
</div>
<div class="form-group col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">
<label class="control-label" for="exp">expansion</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterLink } from '@angular/router';
import {
CREATURE_AI_NAME,
CREATURE_CLASS,
Expand All @@ -25,6 +26,7 @@ import {
} from '@keira/shared/acore-world-model';
import { SingleRowEditorComponent } from '@keira/shared/base-abstract-classes';
import { QueryOutputComponent, TopBarComponent } from '@keira/shared/base-editor-components';
import { InputValidationDirective } from '@keira/shared/directives';
import {
BooleanOptionSelectorComponent,
CreatureSelectorBtnComponent,
Expand All @@ -39,9 +41,6 @@ import { TranslateModule } from '@ngx-translate/core';
import { TooltipModule } from 'ngx-bootstrap/tooltip';
import { CreatureHandlerService } from '../creature-handler.service';
import { CreatureTemplateService } from './creature-template.service';
import { RouterLink } from '@angular/router';
import { InputValidationDirective } from '@keira/shared/directives';
import { ValidationFeedbackComponent } from '@keira/shared/error-templates';

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
Expand All @@ -65,7 +64,6 @@ import { ValidationFeedbackComponent } from '@keira/shared/error-templates';
IconSelectorComponent,
RouterLink,
InputValidationDirective,
ValidationFeedbackComponent,
],
})
export class CreatureTemplateComponent extends SingleRowEditorComponent<CreatureTemplate> {
Expand Down
45 changes: 35 additions & 10 deletions libs/shared/directives/src/validate-input.directive.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
import { Directive, ElementRef, HostBinding, Input, OnInit } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { Directive, ElementRef, inject, OnInit, Renderer2 } from '@angular/core';
import { AbstractControl, NgControl } from '@angular/forms';
import { SubscriptionHandler } from '@keira/shared/utils';

@Directive({
selector: '[keiraInputValidation]',
standalone: true,
})
export class InputValidationDirective implements OnInit {
@Input('keiraInputValidation') control!: AbstractControl | null;
export class InputValidationDirective extends SubscriptionHandler implements OnInit {
private readonly el: ElementRef = inject(ElementRef);
private readonly renderer: Renderer2 = inject(Renderer2);
private readonly ngControl: NgControl = inject(NgControl);

constructor(private el: ElementRef) {}
private errorDiv: HTMLElement | null = null;

ngOnInit(): void {
if (!this.control) {
console.warn('ValidationDirective: No control provided.');
const control = this.ngControl.control;

if (!control) {
return;
}

this.subscriptions.push(
control.statusChanges?.subscribe(() => {
this.updateErrorMessage(control);
}),
);
}

@HostBinding('class.is-invalid')
get isInvalid(): boolean {
return !!this.control?.invalid && !!this.control?.touched;
private updateErrorMessage(control: AbstractControl): void {
if (this.errorDiv) {
this.renderer.removeChild(this.el.nativeElement.parentNode, this.errorDiv);
this.errorDiv = null;
}

if (control?.touched && control?.invalid) {
this.errorDiv = this.renderer.createElement('div');
this.renderer.addClass(this.errorDiv, 'error-message');

const errorMessage = control?.errors?.['required'] ? 'This field is required' : 'Invalid field';

const text = this.renderer.createText(errorMessage);
this.renderer.appendChild(this.errorDiv, text);

const parent = this.el.nativeElement.parentNode;
this.renderer.appendChild(parent, this.errorDiv);
}
}
}
33 changes: 0 additions & 33 deletions libs/shared/error-templates/.eslintrc.json

This file was deleted.

3 changes: 0 additions & 3 deletions libs/shared/error-templates/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions libs/shared/error-templates/karma.conf.js

This file was deleted.

24 changes: 0 additions & 24 deletions libs/shared/error-templates/project.json

This file was deleted.

1 change: 0 additions & 1 deletion libs/shared/error-templates/src/index.ts

This file was deleted.

24 changes: 0 additions & 24 deletions libs/shared/error-templates/src/input-validation-error.ts

This file was deleted.

29 changes: 0 additions & 29 deletions libs/shared/error-templates/tsconfig.json

This file was deleted.

12 changes: 0 additions & 12 deletions libs/shared/error-templates/tsconfig.lib.json

This file was deleted.

9 changes: 0 additions & 9 deletions libs/shared/error-templates/tsconfig.spec.json

This file was deleted.

0 comments on commit 7021a3a

Please sign in to comment.