Verified Commit 55db080a authored by Merzough Münker's avatar Merzough Münker
Browse files

feat(forms): add reactive model-based inputs and injectable properties

Introduced `model` for reactive handling of `initial` and `context` inputs in the `FormDirective`. Added dependency-injected support for `RXAP_FORM_CONTEXT` and `RXAP_FORM_INITIAL_STATE`. Enhanced initial state loading and form context management for more flexible and efficient configurations.
parent 4ccea20e
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import {
  input,
  Input,
  isDevMode,
  model,
  OnChanges,
  OnDestroy,
  OnInit,
@@ -39,6 +40,10 @@ import {
import { RxapFormBuilder } from '../form-builder';
import { RxapFormGroup } from '../form-group';
import { FormDefinition } from '../model';
import {
  RXAP_FORM_CONTEXT,
  RXAP_FORM_INITIAL_STATE,
} from '../tokens';
import {
  FormLoadFailedMethod,
  FormLoadMethod,
@@ -115,10 +120,9 @@ export class FormDirective<T = any>
  implements OnInit, OnChanges, OnDestroy {
  public override form!: RxapFormGroup<T>;

  @Input()
  public initial?: T;
  public readonly initial = model<T | null>(null);

  public readonly context = input<Record<string, unknown>>({});
  public readonly context = model<unknown | null>(null);

  /**
   * Emits when the submit method is executed without errors. The result of the
@@ -245,6 +249,12 @@ export class FormDirective<T = any>
    @Optional()
    @Inject(LoadingIndicatorService)
    protected readonly loadingIndicatorService: LoadingIndicatorService | null = null,
    @Optional()
    @Inject(RXAP_FORM_CONTEXT)
    context: unknown = null,
    @Optional()
    @Inject(RXAP_FORM_INITIAL_STATE)
    initial: T | null = null,
  ) {
    super([], []);
    this.submitMethod = submitMethod ?? this.submitMethod;
@@ -262,6 +272,12 @@ export class FormDirective<T = any>
    }
    this.loadingIndicatorService?.attachLoading(this.loading$);
    this.loadingIndicatorService?.attachLoading(this.submitting$);
    if (context) {
      this.context.set(context);
    }
    if (initial && typeof initial === 'object' && !Array.isArray(initial)) {
      this.initial.set(initial);
    }
  }

  public override ngOnChanges(changes: SimpleChanges) {
@@ -402,17 +418,18 @@ export class FormDirective<T = any>
  }

  protected loadInitialState(form: RxapFormGroup): void {
    if (this.initial) {
    const initial = this.initial();
    if (initial && typeof initial === 'object' && !Array.isArray(initial)) {
      if (isDevMode()) {
        console.log('use the value from input initial');
      }
      form.patchValue(this.initial);
      form.patchValue(initial);
    } else {
      if (this.loadMethod) {
        this.loading$.enable();

        try {
          const resultOrPromise = this.loadMethod.call();
          const resultOrPromise = this.loadMethod.call({ context: this.context(), initial: this.initial() });
          if (isPromise(resultOrPromise)) {
            resultOrPromise
              .then((value) => {
+2 −2
Original line number Diff line number Diff line
@@ -4,11 +4,11 @@ import {
} from '@rxap/pattern';

export interface FormSubmitMethod<T> extends MethodWithParameters<any, T> {
  call(parameters: T, context?: Record<string, unknown>): any | Promise<any>;
  call(parameters: T, context?: unknown): any | Promise<any>;
}

export interface FormLoadMethod<T = any> extends Method<T> {
  call(): T | Promise<T>;
  call(data: { context: unknown | null, initial: unknown | null }): T | Promise<T>;
}

export interface FormLoadFailedMethod extends MethodWithParameters {