Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(input): migrate to signals #475

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions libs/ui/form-field/helm/src/lib/hlm-form-field.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, type Signal, computed, contentChild, contentChildren, effect } from '@angular/core';

import { Component, computed, contentChild, contentChildren, effect } from '@angular/core';
import { BrnFormFieldControl } from '@spartan-ng/ui-formfield-brain';
import { HlmErrorDirective } from './hlm-error.directive';

Expand All @@ -23,13 +22,13 @@ import { HlmErrorDirective } from './hlm-error.directive';
},
})
export class HlmFormFieldComponent {
control = contentChild(BrnFormFieldControl);
public readonly control = contentChild(BrnFormFieldControl);

errorChildren = contentChildren(HlmErrorDirective);
public readonly errorChildren = contentChildren(HlmErrorDirective);

hasDisplayedMessage: Signal<'error' | 'hint'> = computed(() => {
return this.errorChildren() && this.errorChildren().length > 0 && this.control()?.errorState() ? 'error' : 'hint';
});
protected readonly hasDisplayedMessage = computed<'error' | 'hint'>(() =>
this.errorChildren() && this.errorChildren().length > 0 && this.control()?.errorState() ? 'error' : 'hint',
);

constructor() {
effect(() => {
Expand Down
28 changes: 14 additions & 14 deletions libs/ui/input/helm/src/lib/hlm-input.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, type DoCheck, Injector, Input, computed, effect, inject, input, signal } from '@angular/core';
import { Directive, type DoCheck, Injector, computed, effect, inject, input, signal } from '@angular/core';
import { FormGroupDirective, NgControl, NgForm } from '@angular/forms';
import { hlm } from '@spartan-ng/ui-core';
import { BrnFormFieldControl } from '@spartan-ng/ui-formfield-brain';
Expand Down Expand Up @@ -43,28 +43,24 @@ type InputVariants = VariantProps<typeof inputVariants>;
],
})
export class HlmInputDirective implements BrnFormFieldControl, DoCheck {
private readonly _size = signal<InputVariants['size']>('default');
@Input()
set size(value: InputVariants['size']) {
this._size.set(value);
}
public readonly size = input<InputVariants['size']>('default');

private readonly _error = signal<InputVariants['error']>('auto');
@Input()
set error(value: InputVariants['error']) {
this._error.set(value);
}
public readonly error = input<InputVariants['error']>('auto');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goetzrobin should be fixed now. Somehow I forgot to change error to input which caused the type error in storybook.

protected readonly state = computed(() => ({
error: signal(this.error()),
}));

public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected _computedClass = computed(() =>
hlm(inputVariants({ size: this._size(), error: this._error() }), this.userClass()),
hlm(inputVariants({ size: this.size(), error: this.state().error() }), this.userClass()),
);

private injector = inject(Injector);

ngControl: NgControl | null = this.injector.get(NgControl, null);

errorStateTracker: ErrorStateTracker;
private errorStateTracker: ErrorStateTracker;

private defaultErrorStateMatcher = inject(ErrorStateMatcher);
private parentForm = inject(NgForm, { optional: true });
Expand All @@ -83,7 +79,7 @@ export class HlmInputDirective implements BrnFormFieldControl, DoCheck {
effect(
() => {
if (this.ngControl) {
this.error = this.errorStateTracker.errorState();
this.setError(this.errorStateTracker.errorState());
}
},
{ allowSignalWrites: true },
Expand All @@ -93,4 +89,8 @@ export class HlmInputDirective implements BrnFormFieldControl, DoCheck {
ngDoCheck() {
this.errorStateTracker.updateErrorState();
}

setError(error: InputVariants['error']) {
this.state().error.set(error);
}
}
Loading